-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml_node.go
279 lines (263 loc) · 5.87 KB
/
html_node.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package hcj
import (
"fmt"
"sort"
"strconv"
"strings"
"golang.org/x/net/html"
)
type ParsedNode struct {
Raw *html.Node
Tag string
ID string
Classes []string
Style map[string]string
FirstChild *ParsedNode
NextSibling *ParsedNode
State InteractiveState
}
type InteractiveState struct {
VisitedAddresses map[string]struct{}
}
func (pn *ParsedNode) String() string {
return pn.NestedString(0, 1)
}
func (pn *ParsedNode) NestedString(indent int, siblingNumber int) string {
out := strings.Repeat("\t", indent) + strconv.Itoa(siblingNumber) + ": "
nest := strings.Repeat("\t", indent) + " "
switch pn.Raw.Type {
case html.TextNode:
out += fmt.Sprintf("Text:%q", strings.Replace(pn.Tag, "\n", "\\n", -1)) + "\n"
default:
out += fmt.Sprintf("Tag:'%s' ", pn.Tag) + "\n"
}
if pn.ID != "" {
out += nest + fmt.Sprintf("ID:'%s' ", pn.ID) + "\n"
}
if len(pn.Classes) != 0 {
out += nest + fmt.Sprintf("Classes: %v ", pn.Classes) + "\n"
}
if len(pn.Style) != 0 {
out += nest + fmt.Sprintf("Style: %v ", pn.Style) + "\n"
}
if pn.FirstChild != nil {
out += pn.FirstChild.NestedString(indent+1, 1)
}
if pn.NextSibling != nil {
out += pn.NextSibling.NestedString(indent, siblingNumber+1)
}
if len(out) == 0 {
return out
}
return out
}
type ParseNodeOptions struct {
CSS CSS
ParentStyle map[string]string
InteractiveState InteractiveState
}
func ParseNode(node *html.Node, opts ...ParseNodeOption) *ParsedNode {
cfg := ParseNodeOptions{}
for _, o := range opts {
cfg = o(cfg)
}
pn := &ParsedNode{
Raw: node,
ID: getAttribute(node, "id"),
Tag: node.Data,
Classes: getAttributes(node, "class"),
Style: make(map[string]string),
State: cfg.InteractiveState,
}
// todo: what properties are inherited?
// find a table
inheritedProps := map[string]struct{}{
"color": {},
"opacity": {},
"background": {},
"background-color": {},
"font-style": {},
"display": {},
"font-size": {},
"font-weight": {},
"width": {},
"list-style-type": {},
}
pn.CalculateStyle(cfg.CSS)
INHERIT_LOOP:
for k, v := range cfg.ParentStyle {
if _, ok := inheritedProps[k]; ok {
switch pn.Style[k] {
case "inherit":
case "currentColor":
if k != "color" {
continue INHERIT_LOOP
}
case "":
default:
continue INHERIT_LOOP
}
pn.Style[k] = v
}
}
if node.FirstChild != nil {
pn.FirstChild = ParseNode(node.FirstChild, WithCSS(cfg.CSS), WithParentStyle(pn.Style), WithInteractiveState(pn.State))
//pn.FirstChild.setParent(pn)
}
if node.NextSibling != nil {
pn.NextSibling = ParseNode(node.NextSibling, WithCSS(cfg.CSS), WithParentStyle(cfg.ParentStyle), WithInteractiveState(pn.State))
}
return pn
}
// func (pn *ParsedNode) setParent(parent *ParsedNode) {
// pn.Parent = parent
// if pn.NextSibling != nil {
// pn.NextSibling.setParent(parent)
// }
// }
type styleWithPriority struct {
priority int16 // max ~1000
style map[string]string
}
func (pn *ParsedNode) SelectorPriority(sel Selector) int16 {
priority := int16(-1)
if sel.Global {
priority = 1
}
for _, id := range sel.IDs {
if pn.ID == id {
priority += 100
} else {
return -1000
}
}
if sel.Tag == pn.Tag {
priority += 2
} else if sel.Tag != "" {
return -1000
}
for _, c2 := range sel.Classes {
match := false
for _, c := range pn.Classes {
if c == c2 {
match = true
priority += 10
break
}
}
if !match {
priority = -1000
}
}
if sel.Attribute != "" {
matcher, err := ParseAttributeSelector(sel.Attribute)
if err != nil {
return -1000
}
if matcher.Match(pn) {
priority += 10
} else {
return -1000
}
}
for _, pc := range sel.PseudoClasses {
// TODO: implement more pseudo class support
switch pc.Type {
case PseudoClassTypeActive:
// TODO
return -1000
case PseudoClassTypeLink:
href := getAttribute(pn.Raw, "href")
if _, ok := pn.State.VisitedAddresses[href]; ok {
return -1000
} else {
// todo: how much?
priority += 1
}
case PseudoClassTypeVisited:
href := getAttribute(pn.Raw, "href")
if _, ok := pn.State.VisitedAddresses[href]; ok {
// todo: how much?
priority += 1
} else {
return -1000
}
case PseudoClassTypeEmpty:
empty := true
WalkChildren(pn.Raw, func(n *html.Node) bool {
if n.Data != "" {
empty = false
return false
}
return true
})
if empty {
// todo: how much?
priority += 1
} else {
return -1000
}
case PseudoClassTypeNot:
pcPriority := pn.SelectorPriority(pc.SubSelector)
// TODO: this is not sufficient
if pcPriority > 0 {
return -1000
} else {
// todo: how much?
priority += 1
}
}
}
return priority
}
func WalkChildren(root *html.Node, fn func(n *html.Node) bool) (cont bool) {
if root.FirstChild != nil {
next := root.FirstChild
siblings := []*html.Node{next}
for {
next = next.NextSibling
if next == nil {
break
}
siblings = append(siblings, next)
}
for _, s := range siblings {
cont = fn(s)
if !cont {
return
}
cont = WalkChildren(s, fn)
if !cont {
return
}
}
}
return true
}
func (pn *ParsedNode) CalculateStyle(css CSS) {
styles := []styleWithPriority{}
for selStr, style := range css.Selectors {
sel, err := ParseSelector(selStr)
if err != nil {
continue
}
priority := pn.SelectorPriority(sel)
if priority > 0 {
styles = append(styles, styleWithPriority{
priority: priority,
style: style,
})
}
}
sort.Slice(styles, func(i, j int) bool {
return styles[i].priority < styles[j].priority
})
for _, style := range styles {
pn.Style = mergeMaps(pn.Style, style.style)
}
// inline always has highest priority
if style := getMapAttribute(pn.Raw, "style"); style != nil {
pn.Style = mergeMaps(pn.Style, style)
}
// !important, not supported
}