-
Notifications
You must be signed in to change notification settings - Fork 1
/
first.go
146 lines (133 loc) · 3.6 KB
/
first.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
package scrappy
import (
"golang.org/x/net/html"
)
// First, group of methods that return only one occurrence
type F struct {
*Scrappy
index int
}
func decrease(i int) int {
if i > 0 {
i--
}
return i
}
func (f *F) Index(index int) *F {
f.index = index
return f
}
// Return first parent node that matches
func (f *F) Parent(root *html.Node, filters ...FilterFunc) *html.Node {
for node := root.Parent; node != nil; node = node.Parent {
if f.Validate(node, filters...) {
if f.index = decrease(f.index); f.index == 0 {
return node
}
}
}
return nil
}
// Depth return a node using first depth algorithm, scan all nodes
func (f *F) Depth(node *html.Node, filters ...FilterFunc) *html.Node {
if f.Validate(node, filters...) {
if f.index = decrease(f.index); f.index == 0 {
return node
}
}
for node := node.FirstChild; node != nil; node = node.NextSibling {
result := f.Depth(node, filters...)
if result != nil {
return result
}
}
return nil
}
// Breadth return a node using first breadth algorithm, scan all nodes
func (f *F) Breadth(node *html.Node, filters ...FilterFunc) *html.Node {
var breadth func(nodes []*html.Node, filters ...FilterFunc) *html.Node
breadth = func(nodes []*html.Node, filters ...FilterFunc) *html.Node {
var next []*html.Node
for _, elm := range nodes {
for node := elm.FirstChild; node != nil; node = node.NextSibling {
if f.Validate(node, filters...) {
if f.index = decrease(f.index); f.index == 0 {
return node
}
}
next = append(next, node)
}
}
if len(next) > 0 {
return breadth(next, filters...)
}
return nil
}
return breadth([]*html.Node{node}, filters...)
}
// Return last child, it's like a last sibling with the first child
func (f *F) LastChild(node *html.Node, filters ...FilterFunc) *html.Node {
for node := node.LastChild; node != nil; node = node.PrevSibling {
if f.Validate(node, filters...) {
if f.index = decrease(f.index); f.index == 0 {
return node
}
}
}
return nil
}
// Return first child node that matches
func (f *F) FirstChild(node *html.Node, filters ...FilterFunc) *html.Node {
for node := node.FirstChild; node != nil; node = node.NextSibling {
if f.Validate(node, filters...) {
if f.index = decrease(f.index); f.index == 0 {
return node
}
}
}
return nil
}
// LastSibling, return last sibling node that matches
func (f *F) LastSibling(node *html.Node, filters ...FilterFunc) (result *html.Node) {
for node := node.NextSibling; node != nil; node = node.NextSibling {
if f.Validate(node, filters...) {
if f.index = decrease(f.index); f.index == 0 {
result = node
}
}
}
return result
}
// FirstSibling, return first sibling node that matches
func (f *F) FirstSibling(node *html.Node, filters ...FilterFunc) (result *html.Node) {
for node := node.PrevSibling; node != nil; node = node.PrevSibling {
if f.Validate(node, filters...) {
if f.index = decrease(f.index); f.index == 0 {
result = node
}
}
}
return result
}
// NextSibling, return next sibling that matches
func (f *F) NextSibling(node *html.Node, filters ...FilterFunc) *html.Node {
for node := node.NextSibling; node != nil; node = node.NextSibling {
if f.Validate(node, filters...) {
if f.index = decrease(f.index); f.index == 0 {
return node
}
}
}
return nil
}
// PrevSibling, return prev sibling that matches
func (f *F) PrevSibling(node *html.Node, filters ...FilterFunc) *html.Node {
for node := node.PrevSibling; node != nil; node = node.PrevSibling {
if f.Validate(node, filters...) {
if f.index = decrease(f.index); f.index == 0 {
return node
}
}
}
return nil
}