-
Notifications
You must be signed in to change notification settings - Fork 10
/
html_utils.go
179 lines (155 loc) · 4.44 KB
/
html_utils.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
/*
Copyright 2021 The GoPlus Authors (goplus.org)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package hdq
import (
"strings"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
// -----------------------------------------------------------------------------
// containsClass returns true if v is a class in source.
// eg. `ContainsClass("top current", "current")` returns true.
func containsClass(source string, v string) bool {
for {
pos := strings.IndexByte(source, ' ')
if pos < 0 {
return source == v
}
if source[:pos] == v {
return true
}
source = source[pos+1:]
}
}
// attributeVal returns value of the attribute `k`.
func attributeVal(node *html.Node, k string) (v string, err error) {
if node.Type != html.ElementNode {
return "", ErrInvalidNode
}
for _, attr := range node.Attr {
if attr.Key == k {
return attr.Val, nil
}
}
return "", ErrNotFound
}
// firstChild returns the first child with type `nodeType` of the node `node`.
func firstChild(node *html.Node, nodeType html.NodeType) (p *html.Node, err error) {
for p = node.FirstChild; p != nil; p = p.NextSibling {
if p.Type == nodeType {
return p, nil
}
}
return nil, ErrNotFound
}
// lastChild returns the last child with type `nodeType` of the node `node`.
func lastChild(node *html.Node, nodeType html.NodeType) (p *html.Node, err error) {
for p = node.LastChild; p != nil; p = p.PrevSibling {
if p.Type == nodeType {
return p, nil
}
}
return nil, ErrNotFound
}
// -----------------------------------------------------------------------------
const (
spaces = " \t\r\n"
)
// childEqualText returns true if the type of node's child is TextNode and it's Data equals `text`.
func childEqualText(node *html.Node, text string) bool {
p := node.FirstChild
if p == nil || p.NextSibling != nil {
return false
}
return equalText(p, text)
}
// equalText returns true if the type of node is TextNode and it's Data equals `text`.
func equalText(node *html.Node, text string) bool {
if node.Type != html.TextNode {
return false
}
return node.Data == text
}
// containsText returns true if the type of node is TextNode and it's Data contains `text`.
func containsText(node *html.Node, text string) bool {
if node.Type != html.TextNode {
return false
}
return strings.Contains(node.Data, text)
}
// hasPrefixText returns true if the type of node is TextNode and its Data has prefix `text`.
func hasPrefixText(node *html.Node, text string) bool {
if node.Type != html.TextNode {
return false
}
return strings.Contains(strings.TrimLeft(node.Data, spaces), text)
}
// exactText returns text of node if the type of node is TextNode.
func exactText(node *html.Node) (string, error) {
if node.Type != html.TextNode {
return node.Data, nil
}
return "", ErrInvalidNode
}
// textOf returns text data of node's all childs.
func textOf(node *html.Node) string {
var printer textPrinter
printer.printNode(node)
return string(printer.data)
}
type textPrinter struct {
data []byte
notLineStart bool
hasSpace bool
}
func (p *textPrinter) printText(v string, hasRightSpace bool) {
if v == "" {
return
}
if p.notLineStart && p.hasSpace {
p.data = append(p.data, ' ')
} else {
p.notLineStart = true
}
p.data = append(p.data, v...)
p.hasSpace = hasRightSpace
}
func (p *textPrinter) printNode(node *html.Node) {
if node == nil {
return
}
if node.Type == html.TextNode {
p.printText(textTrimRight(textTrimLeft(node.Data, &p.hasSpace)))
return
}
for child := node.FirstChild; child != nil; child = child.NextSibling {
p.printNode(child)
}
switch node.DataAtom {
case atom.P:
p.data = append(p.data, '\n')
p.notLineStart = false
}
}
func textTrimLeft(v string, hasSpace *bool) string {
ret := strings.TrimLeft(v, spaces)
if len(v) != len(ret) {
*hasSpace = true
}
return ret
}
func textTrimRight(v string) (string, bool) {
ret := strings.TrimRight(v, spaces)
return ret, len(v) != len(ret)
}
// -----------------------------------------------------------------------------