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
|
package ibd
import (
"strings"
"golang.org/x/net/html"
)
func findChildren(node *html.Node, f func(node *html.Node) bool) (found []*html.Node) {
for c := node.FirstChild; c != nil; c = c.NextSibling {
if f(c) {
found = append(found, c)
}
}
return
}
func findChildrenRecursive(node *html.Node, f func(node *html.Node) bool) (found []*html.Node) {
if f(node) {
found = append(found, node)
}
for c := node.FirstChild; c != nil; c = c.NextSibling {
found = append(found, findChildrenRecursive(c, f)...)
}
return
}
func findClass(node *html.Node, className string) (found *html.Node) {
if isClass(node, className) {
return node
}
for c := node.FirstChild; c != nil; c = c.NextSibling {
if found = findClass(c, className); found != nil {
return
}
}
return
}
func isClass(node *html.Node, className string) bool {
if node.Type == html.ElementNode {
for _, attr := range node.Attr {
if attr.Key != "class" {
continue
}
classes := strings.Fields(attr.Val)
for _, class := range classes {
if class == className {
return true
}
}
}
}
return false
}
func extractText(node *html.Node) string {
var result strings.Builder
extractTextInner(node, &result)
return result.String()
}
func extractTextInner(node *html.Node, result *strings.Builder) {
if node.Type == html.TextNode {
result.WriteString(node.Data)
}
for c := node.FirstChild; c != nil; c = c.NextSibling {
extractTextInner(c, result)
}
}
func findId(node *html.Node, id string) (found *html.Node) {
if isId(node, id) {
return node
}
for c := node.FirstChild; c != nil; c = c.NextSibling {
if found = findId(c, id); found != nil {
return
}
}
return
}
func isId(node *html.Node, id string) bool {
if node.Type == html.ElementNode {
for _, attr := range node.Attr {
if attr.Key == "id" && attr.Val == id {
return true
}
}
}
return false
}
|