-
Notifications
You must be signed in to change notification settings - Fork 55
/
query_render.go
68 lines (57 loc) · 1.24 KB
/
query_render.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
package zhttp
import (
"bytes"
"strings"
"golang.org/x/net/html"
)
func (e Els) ForEach(f func(index int, el QueryHTML) bool) {
for i, v := range e {
if !f(i, v) {
break
}
}
}
func (r QueryHTML) String() string {
return r.HTML(true)
}
func (r QueryHTML) Exist() bool {
return r.node != nil && r.node.Data != ""
}
func (r QueryHTML) Attr(key string) string {
return r.Attrs()[key]
}
func (r QueryHTML) Attrs() map[string]string {
node := r.getNode()
if node.Type != html.ElementNode || len(node.Attr) == 0 {
return make(map[string]string)
}
return getAttrValue(node.Attr)
}
func (r QueryHTML) Name() string {
return r.getNode().Data
}
func (r QueryHTML) Text(trimSpace ...bool) string {
text := getElText(r, false)
if len(trimSpace) > 0 && trimSpace[0] {
text = strings.TrimSpace(text)
}
return text
}
func (r QueryHTML) FullText(trimSpace ...bool) string {
text := getElText(r, true)
if len(trimSpace) > 0 && trimSpace[0] {
text = strings.TrimSpace(text)
}
return text
}
func (r QueryHTML) HTML(trimSpace ...bool) string {
var b bytes.Buffer
if err := html.Render(&b, r.getNode()); err != nil {
return ""
}
text := b.String()
if len(trimSpace) > 0 && trimSpace[0] {
text = strings.TrimSpace(text)
}
return text
}