-
Notifications
You must be signed in to change notification settings - Fork 0
/
html.go
66 lines (53 loc) · 1.22 KB
/
html.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
package webpage
import (
"golang.org/x/net/html"
)
func newNoscriptNode() *html.Node {
return &html.Node{
Type: html.ElementNode,
Data: "noscript",
}
}
func newHTMLDocument() *html.Node {
return &html.Node{
// Type: html.ElementNode,
// Data: "noscript",
}
}
func cloneNode(original *html.Node) (clone *html.Node) {
attr := make([]html.Attribute, len(original.Attr))
copy(attr, original.Attr)
clone = &html.Node{
Type: original.Type,
DataAtom: original.DataAtom,
Data: original.Data,
Namespace: original.Namespace,
Attr: attr,
}
for child := original.FirstChild; child != nil; child = child.NextSibling {
clone.AppendChild(cloneNode(child))
}
return
}
func findNode(node *html.Node, predicate func(*html.Node) bool) *html.Node {
if node == nil {
return nil
}
if predicate(node) {
return node
}
for child := node.FirstChild; child != nil; child = child.NextSibling {
if foundNode := findNode(child, predicate); foundNode != nil {
return foundNode
}
}
return nil
}
func findElement(node *html.Node, name string) *html.Node {
return findNode(node, func(node *html.Node) bool {
if node.Type == html.ElementNode && node.Data == name {
return true
}
return false
})
}