-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.go
More file actions
74 lines (64 loc) · 1.55 KB
/
Copy pathscript.go
File metadata and controls
74 lines (64 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package pageseo
import (
"errors"
"net/url"
"strings"
"testing"
"github.com/dkotik/pageseo/internal"
"golang.org/x/net/html"
)
func NewScriptNodeTester() NodeTester {
return script{}
}
type script struct{}
func (s script) Match(t testing.TB, node *html.Node) bool {
return node.Type == html.ElementNode && node.Data == "script"
}
func (s script) ListResourcesForPreloading(origin *url.URL, node *html.Node) (URLs []string) {
for _, attr := range node.Attr {
if attr.Key == "src" {
if strings.TrimSpace(attr.Val) != "" {
URLs = append(URLs, joinRelativePath(origin, attr.Val))
}
break // only take the first attribute
}
}
return URLs
}
func (s script) TestNode(t testing.TB, origin *url.URL, node *html.Node, loader Loader) {
source := ""
for _, attr := range node.Attr {
switch attr.Key {
case "src":
if source != "" {
t.Error("duplicate <script[src]> attribute:", source)
} else {
source = attr.Val
}
case "language":
t.Log(internal.WP, "<script[language]> attribute is deprecated:", attr.Val)
}
}
if source != "" {
script, contentType, err := loader.Load(
t.Context(),
joinRelativePath(origin, source),
)
if err != nil {
if errors.Is(err, Skip) {
return
}
t.Errorf("unable to load script %q: %v", source, err)
}
switch contentType {
case "":
t.Error("empty Content-Type for the script file")
case "text/javascript", "application/javascript":
default:
t.Log("strange script Content-Type:", contentType)
}
if len(script) == 0 {
t.Error("empty script file")
}
}
}