-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinder.go
231 lines (194 loc) · 6.42 KB
/
finder.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
package node
import (
"context"
"github.com/antchfx/htmlquery"
"github.com/antchfx/xpath"
"github.com/ericchiang/css"
"golang.org/x/net/html"
)
// Finder represents a set of methods for finding nodes.
type Finder interface {
// Find searches for the first matched node in the parse tree based on the specified find method and filters.
Find(FindMethod, TagFilter, ...Filter) Node
// FindN searches for up to n nodes in the parse tree based on the specified find method and filters.
FindN(FindMethod, int, TagFilter, ...Filter) []Node
// FindAll searches for all nodes in the parse tree based on the specified find method and filters.
FindAll(FindMethod, TagFilter, ...Filter) []Node
// FindString searches for the first matched text node in the parse tree based on the specified find method and filters.
FindString(FindMethod, StringFilter) TextNode
// FindStringN searches for up to n text nodes in the parse tree based on the specified find method and filters.
FindStringN(FindMethod, int, StringFilter) []TextNode
// FindAllString searches for all text nodes in the parse tree based on the specified find method and filters.
FindAllString(FindMethod, StringFilter) []TextNode
// CSS selectors support
// Select searches for the first matched node in the parse tree based on the css selector.
// Will panics if the selector cannot be parsed.
Select(string) Node
// SelectAll searches for all nodes in the parse tree based on the css selector.
// Will panics if the selector cannot be parsed.
SelectAll(string) []Node
// xpath support
// XPath searches for all node that matches by the specified XPath expr. Will panics if the expression cannot be parsed.
XPath(string) []Node
// Evaluate returns the result of the xpath expression.
// The result type of the expression is one of the follow: bool, float64, string, *xpath.NodeIterator.
Evaluate(string) (any, error)
}
// FindMethod represents the method used to search for nodes in the parse tree.
type FindMethod int
const (
// Descendant represents a search for nodes that are descendants of the current node.
Descendant FindMethod = iota
// NoRecursive represents a search for nodes that are direct children of the current node.
NoRecursive
// Parent represents a search for the parent node of the current node.
Parent
// PrevSibling represents a search for the previous sibling node of the current node.
PrevSibling
// NextSibling represents a search for the next sibling node of the current node.
NextSibling
// Previous represents a search for the previous node in the parse tree.
Previous
// Next represents a search for the next node in the parse tree.
Next
)
func findTextNode(tag TagFilter, filters []Filter, strict bool) bool {
if strict || ((tag == nil || tag.Ignore()) && !isAttributeFilter(filters)) {
return true
}
return false
}
func isMatchType(node Node, findTextNode bool) bool {
if t := node.Type(); findTextNode {
return t == html.TextNode
} else {
return t == html.ElementNode
}
}
func (n *htmlNode) find(method FindMethod, text bool, limit int, tag TagFilter, filters ...Filter) (nodes []Node) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var f func(Node, ...Filter)
f = func(node Node, filters ...Filter) {
if ctx.Err() != nil || node == nil {
return
}
if raw := node.Raw(); n.Raw() != raw &&
isMatchType(node, findTextNode(tag, filters, text)) &&
(tag == nil || tag.IsMatch(node)) {
ok := true
for _, i := range filters {
if !i.IsMatch(node) {
ok = false
break
}
}
if ok {
nodes = append(nodes, node)
if len(nodes) == limit {
cancel()
}
}
}
switch method {
case Descendant:
for node := node.FirstChild(); node != nil; node = node.NextSibling() {
f(node, filters...)
}
case NoRecursive:
if raw := node.Raw(); n.Raw() == raw {
f(node.FirstChild(), filters...)
} else {
f(node.NextSibling(), filters...)
}
case Parent:
if node := node.Parent(); node != nil {
f(node, filters...)
}
case PrevSibling:
if node := node.PrevSibling(); node != nil {
f(node, filters...)
}
case NextSibling:
if node := node.NextSibling(); node != nil {
f(node, filters...)
}
case Previous:
if node := node.PrevNode(); node != nil {
f(node, filters...)
}
case Next:
if node := node.NextNode(); node != nil {
f(node, filters...)
}
}
}
f(n.ToNode(), filters...)
return
}
func (n *htmlNode) findOnce(method FindMethod, text bool, tag TagFilter, filters ...Filter) Node {
nodes := n.find(method, text, 1, tag, filters...)
if len(nodes) == 0 {
return nil
}
return nodes[0]
}
func (n *htmlNode) findN(method FindMethod, text bool, limit int, tag TagFilter, filters ...Filter) []Node {
if limit <= 0 {
return nil
}
return n.find(method, text, limit, tag, filters...)
}
func (n *htmlNode) Find(method FindMethod, tag TagFilter, filters ...Filter) Node {
return n.findOnce(method, false, tag, filters...)
}
func (n *htmlNode) FindN(method FindMethod, limit int, tag TagFilter, filters ...Filter) []Node {
return n.findN(method, false, limit, tag, filters...)
}
func (n *htmlNode) FindAll(method FindMethod, tag TagFilter, filters ...Filter) []Node {
return n.find(method, false, 0, tag, filters...)
}
func (n *htmlNode) FindString(method FindMethod, filter StringFilter) TextNode {
if node := n.findOnce(method, true, nil, filter); node != nil {
return node.ToTextNode()
}
return nil
}
func (n *htmlNode) FindStringN(method FindMethod, limit int, filter StringFilter) (res []TextNode) {
for _, i := range n.findN(method, true, limit, nil, filter) {
res = append(res, i.ToTextNode())
}
return
}
func (n *htmlNode) FindAllString(method FindMethod, filter StringFilter) (res []TextNode) {
for _, i := range n.find(method, true, 0, nil, filter) {
res = append(res, i.ToTextNode())
}
return
}
func (n *htmlNode) Select(sel string) Node {
nodes := n.SelectAll(sel)
if len(nodes) == 0 {
return nil
}
return nodes[0]
}
func (n *htmlNode) SelectAll(sel string) (res []Node) {
for _, i := range css.MustParse(sel).Select(n.Raw()) {
res = append(res, NewNode(i))
}
return
}
func (n *htmlNode) XPath(expr string) (res []Node) {
for _, i := range htmlquery.Find(n.Raw(), expr) {
res = append(res, NewNode(i))
}
return
}
func (n *htmlNode) Evaluate(expr string) (any, error) {
exp, err := xpath.Compile(expr)
if err != nil {
return nil, err
}
return exp.Evaluate(htmlquery.CreateXPathNavigator(n.Raw())), nil
}