forked from foolin/pagser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin_selections.go
189 lines (177 loc) · 6.43 KB
/
builtin_selections.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
package pagser
import (
"fmt"
"github.com/PuerkitoBio/goquery"
"strconv"
"strings"
)
// BuiltinSelections builtin selection functions are registered with a lowercase initial, eg: Text -> text()
type BuiltinSelections struct {
}
// Child child(selector='') gets the child elements of each element in the Selection,
// Filtered by the specified selector if selector not empty,
// It returns Selection object containing these elements for nested struct..
// struct {
// SubStruct struct {
// Example string `pagser:".selector->text()"`
// } `pagser:".selector->child()"`
// }
func (builtin BuiltinSelections) Child(node *goquery.Selection, args ...string) (out interface{}, err error) {
selector := ""
if len(args) > 0 {
selector = strings.TrimSpace(args[0])
}
if selector != "" {
return node.ChildrenFiltered(selector), nil
}
return node.Children(), nil
}
// Eq eq(index) reduces the set of matched elements to the one at the specified index.
// If a negative index is given, it counts backwards starting at the end of the set.
// It returns a Selection object for nested struct, and an empty Selection object if the
// index is invalid.
// struct {
// SubStruct struct {
// Example string `pagser:".selector->text()"`
// } `pagser:".selector->eq(0)"`
// }
func (builtin BuiltinSelections) Eq(node *goquery.Selection, args ...string) (out interface{}, err error) {
if len(args) < 1 {
return "", fmt.Errorf("nodeEq(index) must has `index` value")
}
indexValue := strings.TrimSpace(args[0])
idx, err := strconv.Atoi(indexValue)
if err != nil {
return "", fmt.Errorf("index=`" + indexValue + "` is not number: " + err.Error())
}
return node.Eq(idx), nil
}
// First first() First reduces the set of matched elements to the first in the set.
// It returns a new Selection object, and an empty Selection object if the
// the selection is empty.
// It returns Selection object containing these elements for nested struct.
// struct {
// SubStruct struct {
// Example string `pagser:".selector->text()"`
// } `pagser:".selector->first()"`
// }
func (builtin BuiltinSelections) First(node *goquery.Selection, args ...string) (out interface{}, err error) {
return node.First(), nil
}
// Last last(selector='') reduces the set of matched elements to the last in the set.
// It returns a new Selection object, and an empty Selection object if
// the selection is empty.
// struct {
// SubStruct struct {
// Example string `pagser:".selector->text()"`
// } `pagser:".selector->last()"`
// }
func (builtin BuiltinSelections) Last(node *goquery.Selection, args ...string) (out interface{}, err error) {
return node.Last(), nil
}
// Next next(selector='') gets the immediately following sibling of each element in the Selection.
// Filtered by the specified selector if selector not empty,
// It returns Selection object containing these elements for nested struct.
// struct {
// SubStruct struct {
// Example string `pagser:".selector->text()"`
// } `pagser:".selector->next()"`
// }
func (builtin BuiltinSelections) Next(node *goquery.Selection, args ...string) (out interface{}, err error) {
selector := ""
if len(args) > 0 {
selector = strings.TrimSpace(args[0])
}
if selector != "" {
return node.NextFiltered(selector), nil
}
return node.Next(), nil
}
// Parent parent(selector='') gets the parent elements of each element in the Selection.
// Filtered by the specified selector if selector not empty,
// It returns Selection object containing these elements for nested struct.
// struct {
// SubStruct struct {
// Example string `pagser:".selector->text()"`
// } `pagser:".selector->parent()"`
// }
func (builtin BuiltinSelections) Parent(node *goquery.Selection, args ...string) (out interface{}, err error) {
selector := ""
if len(args) > 0 {
selector = strings.TrimSpace(args[0])
}
if selector != "" {
return node.ParentFiltered(selector), nil
}
return node.Parent(), nil
}
// Parents parents(selector='') gets the parent elements of each element in the Selection.
// Filtered by the specified selector if selector not empty,
// It returns Selection object containing these elements for nested struct.
// struct {
// SubStruct struct {
// Example string `pagser:".selector->text()"`
// } `pagser:".selector->parents()"`
// }
func (builtin BuiltinSelections) Parents(node *goquery.Selection, args ...string) (out interface{}, err error) {
selector := ""
if len(args) > 0 {
selector = strings.TrimSpace(args[0])
}
if selector != "" {
return node.ParentsFiltered(selector), nil
}
return node.Parents(), nil
}
// ParentsUntil parentsUntil(selector) gets the ancestors of each element in the Selection, up to but
// not including the element matched by the selector. It returns a new Selection
// object containing the matched elements.
// It returns Selection object containing these elements for nested struct.
// struct {
// SubStruct struct {
// Example string `pagser:".selector->text()"`
// } `pagser:".selector->parentsUntil('.wrap')"`
// }
func (builtin BuiltinSelections) ParentsUntil(node *goquery.Selection, args ...string) (out interface{}, err error) {
if len(args) < 1 {
return nil, fmt.Errorf("parentsUntil must has selector")
}
selector := strings.TrimSpace(args[0])
return node.ParentsUntil(selector), nil
}
// Prev prev() gets the immediately preceding sibling of each element in the Selection.
// Filtered by the specified selector if selector not empty,
// It returns Selection object containing these elements for nested struct.
// struct {
// SubStruct struct {
// Example string `pagser:".selector->text()"`
// } `pagser:".selector->prev()"`
// }
func (builtin BuiltinSelections) Prev(node *goquery.Selection, args ...string) (out interface{}, err error) {
selector := ""
if len(args) > 0 {
selector = strings.TrimSpace(args[0])
}
if selector != "" {
return node.PrevFiltered(selector), nil
}
return node.Prev(), nil
}
// Siblings siblings() gets the siblings of each element in the Selection.
// Filtered by the specified selector if selector not empty,
// It returns Selection object containing these elements for nested struct.
// struct {
// SubStruct struct {
// Example string `pagser:".selector->text()"`
// } `pagser:".selector->siblings()"`
// }
func (builtin BuiltinSelections) Siblings(node *goquery.Selection, args ...string) (out interface{}, err error) {
selector := ""
if len(args) > 0 {
selector = strings.TrimSpace(args[0])
}
if selector != "" {
return node.SiblingsFiltered(selector), nil
}
return node.Siblings(), nil
}