-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
175 lines (147 loc) · 3.59 KB
/
query.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
package cborquery
import (
"fmt"
"github.com/antchfx/xpath"
)
var _ xpath.NodeNavigator = &NodeNavigator{}
// CreateXPathNavigator creates a new xpath.NodeNavigator for the specified html.Node.
func CreateXPathNavigator(top *Node) *NodeNavigator {
return &NodeNavigator{cur: top, root: top}
}
// Find is like QueryAll but will panics if `expr` cannot be parsed.
func Find(top *Node, expr string) []*Node {
nodes, err := QueryAll(top, expr)
if err != nil {
panic(err)
}
return nodes
}
// FindOne is like Query but will panics if `expr` cannot be parsed.
func FindOne(top *Node, expr string) *Node {
node, err := Query(top, expr)
if err != nil {
panic(err)
}
return node
}
// QueryAll searches the Node that matches by the specified XPath expr.
// Return an error if the expression `expr` cannot be parsed.
func QueryAll(top *Node, expr string) ([]*Node, error) {
exp, err := getQuery(expr)
if err != nil {
return nil, err
}
return QuerySelectorAll(top, exp), nil
}
// Query searches the Node that matches by the specified XPath expr,
// and returns first element of matched.
func Query(top *Node, expr string) (*Node, error) {
exp, err := getQuery(expr)
if err != nil {
return nil, err
}
return QuerySelector(top, exp), nil
}
// QuerySelectorAll searches all of the Node that matches the specified XPath selectors.
func QuerySelectorAll(top *Node, selector *xpath.Expr) []*Node {
t := selector.Select(CreateXPathNavigator(top))
var elems []*Node
for t.MoveNext() {
elems = append(elems, (t.Current().(*NodeNavigator)).cur)
}
return elems
}
// QuerySelector returns the first matched XML Node by the specified XPath selector.
func QuerySelector(top *Node, selector *xpath.Expr) *Node {
t := selector.Select(CreateXPathNavigator(top))
if t.MoveNext() {
return (t.Current().(*NodeNavigator)).cur
}
return nil
}
// NodeNavigator is for navigating the document.
type NodeNavigator struct {
root, cur *Node
}
func (a *NodeNavigator) Current() *Node {
return a.cur
}
func (a *NodeNavigator) NodeType() xpath.NodeType {
switch a.cur.Type {
case TextNode:
return xpath.TextNode
case DocumentNode:
return xpath.RootNode
case ElementNode:
return xpath.ElementNode
default:
panic(fmt.Sprintf("unknown node type %v", a.cur.Type))
}
}
func (a *NodeNavigator) LocalName() string {
return a.cur.Name
}
func (a *NodeNavigator) Prefix() string {
return ""
}
func (a *NodeNavigator) Value() string {
return fmt.Sprintf("%v", a.cur.value)
}
func (a *NodeNavigator) GetValue() interface{} {
return a.cur.value
}
func (a *NodeNavigator) Copy() xpath.NodeNavigator {
n := *a
return &n
}
func (a *NodeNavigator) MoveToRoot() {
a.cur = a.root
}
func (a *NodeNavigator) MoveToParent() bool {
if n := a.cur.Parent; n != nil {
a.cur = n
return true
}
return false
}
func (x *NodeNavigator) MoveToNextAttribute() bool {
return false
}
func (a *NodeNavigator) MoveToChild() bool {
if n := a.cur.FirstChild; n != nil {
a.cur = n
return true
}
return false
}
func (a *NodeNavigator) MoveToFirst() bool {
for n := a.cur.PrevSibling; n != nil; n = n.PrevSibling {
a.cur = n
}
return true
}
func (a *NodeNavigator) String() string {
return a.Value()
}
func (a *NodeNavigator) MoveToNext() bool {
if n := a.cur.NextSibling; n != nil {
a.cur = n
return true
}
return false
}
func (a *NodeNavigator) MoveToPrevious() bool {
if n := a.cur.PrevSibling; n != nil {
a.cur = n
return true
}
return false
}
func (a *NodeNavigator) MoveTo(other xpath.NodeNavigator) bool {
node, ok := other.(*NodeNavigator)
if !ok || node.root != a.root {
return false
}
a.cur = node.cur
return true
}