-
Notifications
You must be signed in to change notification settings - Fork 55
/
query.go
159 lines (146 loc) · 2.8 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
package zhttp
import (
"bytes"
"golang.org/x/net/html"
)
type (
QueryHTML struct {
node *html.Node
}
)
func HTMLParse(HTML []byte) (doc QueryHTML, err error) {
var n *html.Node
n, err = html.Parse(bytes.NewReader(HTML))
if err != nil {
return
}
for n.Type != html.ElementNode {
switch n.Type {
case html.DocumentNode:
n = n.FirstChild
case html.DoctypeNode:
n = n.NextSibling
case html.CommentNode:
n = n.NextSibling
}
}
doc = QueryHTML{node: n}
return
}
func (r QueryHTML) SelectChild(el string, args ...map[string]string) QueryHTML {
var (
node *html.Node
exist bool
)
forChild(r.node, func(n *html.Node) bool {
elArr := matchEl(n, el, arr2Attr(args))
exist = elArr != nil
if exist {
node = elArr
return false
}
return true
})
if !exist {
return QueryHTML{node: &html.Node{}}
}
return QueryHTML{node: node}
}
func (r QueryHTML) Child() (child []QueryHTML) {
forChild(r.node, func(n *html.Node) bool {
child = append(child, QueryHTML{node: n})
return true
})
return
}
func (r QueryHTML) Select(el string, args ...map[string]string) QueryHTML {
n := findChild(r.node, el, args, false)
if len(n) == 0 {
return QueryHTML{node: &html.Node{}}
}
return QueryHTML{node: n[0]}
}
func (r *QueryHTML) SelectAll(el string, args ...map[string]string) (arr []QueryHTML) {
n := findChild(r.node, el, args, true)
l := len(n)
if l == 0 {
return
}
arr = make([]QueryHTML, l)
for i := range n {
arr[i] = QueryHTML{node: n[i]}
}
return arr
}
func (r QueryHTML) SelectParent(el string, args ...map[string]string) QueryHTML {
n := r.node
attr := arr2Attr(args)
for {
n = n.Parent
if n == nil {
break
}
p := matchEl(n, el, attr)
if p != nil {
return QueryHTML{node: p}
}
}
return QueryHTML{node: &html.Node{}}
}
func (r QueryHTML) Find(el string) QueryHTML {
level := parseSelector(el)
if len(level) == 0 {
return QueryHTML{node: &html.Node{}}
}
n := r
for i := range level {
l := level[i]
if l.Child {
n = n.SelectChild(l.Name, l.Attr)
} else {
n = n.Select(l.Name, l.Attr)
}
if !n.Exist() {
return QueryHTML{node: &html.Node{}}
}
}
return n
}
func parseSelector(el string) []*selector {
var (
ss []*selector
s *selector
)
key, l := "", len(el)
if l > 0 {
s = &selector{i: 0, Attr: make(map[string]string, 0)}
}
for i := 0; i < l; {
v := el[i]
add := 0
switch v {
case '#':
s.appendAttr(key, el, i)
key = "id"
case '.':
s.appendAttr(key, el, i)
key = "class"
case ' ', '>':
s.appendAttr(key, el, i)
if s.Name != "" || len(s.Attr) != 0 {
ss = append(ss, s)
s = &selector{i: i + 1, Attr: make(map[string]string, 0)}
key = ""
}
if v == '>' {
s.Child = true
}
}
i = i + 1 + add
}
if s != nil {
s.appendAttr(key, el, l)
ss = append(ss, s)
}
return ss
}