-
Notifications
You must be signed in to change notification settings - Fork 0
/
distill.go
86 lines (80 loc) · 1.7 KB
/
distill.go
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
75
76
77
78
79
80
81
82
83
84
85
86
package autonews
import (
"golang.org/x/net/html"
)
var usefulattrs = map[string]bool{
"src": true,
"href": true,
"alt": true,
"title": true,
"role": true,
"aria-label": true,
"aria-hidden": true,
"aria-atomic": true,
"focusable": true,
"class": true,
"id": true,
"name": true,
"type": true,
"value": true,
"placeholder": true,
"checked": true,
"disabled": true,
"readonly": true,
"selected": true,
"required": true,
"for": true,
"tabindex": true,
"maxlength": true,
"minlength": true,
"pattern": true,
"size": true,
"min": true,
"max": true,
"step": true,
"multiple": true,
"autocomplete": true,
"autofocus": true,
"form": true,
"formaction": true,
"formenctype": true,
"formmethod": true,
"formtarget": true,
"formnovalidate": true,
}
func distillPipeline(n *html.Node) {
if n == nil {
return
}
unusefulE := false
for i := range n.Attr {
if !usefulattrs[n.Attr[i].Key] {
unusefulE = true
break
}
}
if unusefulE {
newAttr := make([]html.Attribute, 0, len(n.Attr))
for i := range n.Attr {
if usefulattrs[n.Attr[i].Key] {
newAttr = append(newAttr, n.Attr[i])
}
}
n.Attr = newAttr
}
L:
for c := n.FirstChild; c != nil; c = c.NextSibling {
if c.Type == html.ElementNode {
switch c.Data {
case "svg":
c.FirstChild = nil
c.LastChild = nil
continue L
case "script", "style", "link", "noscript":
defer n.RemoveChild(c)
continue L
}
}
distillPipeline(c)
}
}