-
Notifications
You must be signed in to change notification settings - Fork 13
/
utility.go
157 lines (141 loc) · 3.31 KB
/
utility.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"strings"
"unicode"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
func removeUtf8Bom(data []byte) []byte {
if len(data) > 3 && data[0] == 0xEF && data[1] == 0xBB && data[2] == 0xBF {
data = data[3:]
}
return data
}
func containsField(str, field string) bool {
for _, f := range strings.Fields(str) {
if f == field {
return true
}
}
return false
}
func isBlankNode(node *html.Node) bool {
if node.Type == html.CommentNode {
return true
}
if node.Type != html.TextNode {
return false
}
isNonSpace := func(r rune) bool { return !unicode.IsSpace(r) }
return strings.IndexFunc(node.Data, isNonSpace) == -1
}
func findFirstDirectChild(parent *html.Node, a atom.Atom) *html.Node {
for node := parent.FirstChild; node != nil; node = node.NextSibling {
if node.Type == html.ElementNode && node.DataAtom == a {
return node
}
}
return nil
}
func findDirectChildren(parent *html.Node, a atom.Atom) (result []*html.Node) {
for node := parent.FirstChild; node != nil; node = node.NextSibling {
if node.Type == html.ElementNode && node.DataAtom == a {
result = append(result, node)
}
}
return
}
func findFirstChild(parent *html.Node, a atom.Atom) *html.Node {
for node := parent.FirstChild; node != nil; node = node.NextSibling {
if node.Type != html.ElementNode {
continue
}
if node.DataAtom == a {
return node
}
if n := findFirstChild(node, a); n != nil {
return n
}
}
return nil
}
func findChildren(parent *html.Node, a atom.Atom) (result []*html.Node) {
for node := parent.FirstChild; node != nil; node = node.NextSibling {
if node.Type != html.ElementNode {
continue
}
if node.DataAtom == a {
result = append(result, node)
}
if r := findChildren(node, a); len(r) > 0 {
result = append(result, r...)
}
}
return
}
func findAttribute(node *html.Node, name string) *html.Attribute {
for i := 0; i < len(node.Attr); i++ {
if node.Attr[i].Key == name {
return &node.Attr[i]
}
}
return nil
}
func removeAttribute(node *html.Node, name string) {
attr := node.Attr
for i := len(attr) - 1; i >= 0; i-- {
if attr[i].Key == name {
attr = append(attr[:i], attr[i+1:]...)
}
}
node.Attr = attr
}
func getAttributeValue(node *html.Node, name string, dflt string) string {
if attr := findAttribute(node, name); attr != nil {
return attr.Val
}
return dflt
}
func hasClass(node *html.Node, class string) bool {
if attr := findAttribute(node, "class"); attr != nil {
return containsField(attr.Val, class)
}
return false
}
func addClass(node *html.Node, class string) {
if attr := findAttribute(node, "class"); attr != nil {
if !containsField(attr.Val, class) {
attr.Val = class + " " + attr.Val
}
} else {
node.Attr = append(node.Attr, html.Attribute{Key: "class", Val: class})
}
}
func removeClass(node *html.Node, class string) {
attr := findAttribute(node, "class")
if attr == nil {
return
}
classes := ""
for _, c := range strings.Fields(attr.Val) {
if c == class {
continue
}
if len(classes) == 0 {
classes = c
} else {
classes = classes + " " + c
}
}
attr.Val = classes
}
func cloneNode(node *html.Node) *html.Node {
n := &html.Node{
Type: node.Type,
DataAtom: node.DataAtom,
Data: node.Data,
Attr: make([]html.Attribute, len(node.Attr)),
}
copy(n.Attr, node.Attr)
return n
}