-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
342 lines (292 loc) · 7.62 KB
/
node.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package node
import (
"io"
"strings"
"golang.org/x/net/html"
)
var (
_ HtmlNode = &htmlNode{}
_ Node = &node{}
_ TextNode = &textNode{}
)
// Node is an interface representing an HTML node.
type Node interface {
HtmlNode
// String returns a TextNode if the node has only one child whose type is text, otherwise returns nil.
String() TextNode
// Strings return all of the text nodes inside this node.
Strings() []TextNode
// StrippedStrings return a list of strings generated by Strings, where strings consisting entirely of
// whitespace are ignored, and whitespace at the beginning and end of strings is removed.
StrippedStrings() []string
// GetText concatenates all of the text node's content.
GetText() string
}
// TextNode is an interface representing a text node.
type TextNode interface {
HtmlNode
// String returns content for text node.
String() string
}
// HtmlNode is an interface representing an HTML node.
type HtmlNode interface {
// Raw returns origin *html.Node.
Raw() *html.Node
// ToNode converts HtmlNode to Node.
ToNode() Node
// ToTextNode converts HtmlNode to TextNode.
// It will panic if the node type is not text node.
ToTextNode() TextNode
// Type returns a NodeType.
Type() html.NodeType
// Data returns tag name for element node or content for text node.
Data() string
// Attrs returns an Attributes interface for element node.
Attrs() Attributes
// HasAttr return whether node has an attribute.
HasAttr(string) bool
// HTML renders the node's parse tree as HTML code.
HTML() string
// Readable renders unescaped HTML code.
Readable() string
// Parent returns the parent of this node.
Parent() Node
// FirstChild returns the first child of this node.
FirstChild() Node
// LastChild returns the last child of this node.
LastChild() Node
// PrevSibling returns the previous node that are on the same level of the parse tree.
PrevSibling() Node
// NextSibling returns the next node that are on the same level of the parse tree.
NextSibling() Node
// PrevNode returns the node that was parsed immediately before this node.
PrevNode() Node
// NextNode returns the node that was parsed immediately after this node.
NextNode() Node
// Parents iterate over all of this node's parent recursively.
Parents() []Node
// Children return all of this node's direct children.
Children() []Node
// Descendants iterate over all of this node's children recursively.
Descendants() []Node
// PrevSiblings return all of this node's previous nodes that are on the same level of the parse tree.
PrevSiblings() []Node
// NextSiblings return all of this node's next nodes that are on the same level of the parse tree.
NextSiblings() []Node
// PrevNodes return all of the nodes that was parsed before this node.
PrevNodes() []Node
// NextNodes return all of the nodes that was parsed after this node.
NextNodes() []Node
// Finder includes a set of find methods.
Finder
}
type htmlNode struct {
*html.Node
}
// Parse returns the parse tree for the HTML from the given Reader.
func Parse(r io.Reader) (Node, error) {
return ParseWithOptions(r)
}
// ParseWithOptions is like Parse, with options.
func ParseWithOptions(r io.Reader, opts ...html.ParseOption) (Node, error) {
n, err := html.ParseWithOptions(r, opts...)
if err != nil {
return nil, err
}
return NewNode(n), nil
}
// ParseHTML returns the parse tree for the HTML from string.
func ParseHTML(s string) (Node, error) {
return Parse(strings.NewReader(s))
}
func (n *htmlNode) Raw() *html.Node {
return n.Node
}
func (n *htmlNode) ToNode() Node {
return &node{n}
}
func (n *htmlNode) ToTextNode() TextNode {
if n.Type() != html.TextNode {
panic("node is not TextNode")
}
return &textNode{n}
}
func (n *htmlNode) Type() html.NodeType {
return n.Raw().Type
}
func (n *htmlNode) Data() string {
return n.Raw().Data
}
func (n *htmlNode) Attrs() Attributes {
attrs := make(attributes)
for _, i := range n.Node.Attr {
if _, ok := attrs[i.Key]; !ok {
attrs[i.Key] = i.Val
}
}
return attrs
}
func (n *htmlNode) HasAttr(attr string) bool {
_, ok := n.Attrs().Get(attr)
return ok
}
func (n *htmlNode) HTML() string {
var b strings.Builder
html.Render(&b, n.Raw())
return b.String()
}
func (n *htmlNode) Readable() string {
return html.UnescapeString(n.HTML())
}
func (n *htmlNode) Parent() Node {
return NewNode(n.Node.Parent)
}
func (n *htmlNode) FirstChild() Node {
return NewNode(n.Node.FirstChild)
}
func (n *htmlNode) LastChild() Node {
return NewNode(n.Node.LastChild)
}
func (n *htmlNode) PrevSibling() Node {
return NewNode(n.Node.PrevSibling)
}
func (n *htmlNode) NextSibling() Node {
return NewNode(n.Node.NextSibling)
}
func (n *htmlNode) PrevNode() Node {
if prev := n.PrevSibling(); prev != nil {
for node := prev.LastChild(); node != nil; node = prev.LastChild() {
prev = node
}
return prev
}
return n.Parent()
}
func (n *htmlNode) NextNode() Node {
if child := n.FirstChild(); child != nil {
return child
}
if next := n.NextSibling(); next != nil {
return next
}
for node := n.Parent(); node != nil; node = node.Parent() {
if node := node.NextSibling(); node != nil {
return node
}
}
return nil
}
func (n *htmlNode) Parents() (parents []Node) {
parent := n.Parent()
for parent != nil {
parents = append(parents, parent)
parent = parent.Parent()
}
return
}
func (n *htmlNode) Children() (children []Node) {
child := n.FirstChild()
for child != nil {
children = append(children, child)
child = child.NextSibling()
}
return
}
func (n *htmlNode) Descendants() (nodes []Node) {
var f func(Node)
f = func(node Node) {
if node.Raw() != n.Raw() {
nodes = append(nodes, node)
}
for node := node.FirstChild(); node != nil; node = node.NextSibling() {
f(node)
}
}
f(n.ToNode())
return
}
func (n *htmlNode) PrevSiblings() (prevSiblings []Node) {
prevSibling := n.PrevSibling()
for prevSibling != nil {
prevSiblings = append(prevSiblings, prevSibling)
prevSibling = prevSibling.PrevSibling()
}
return
}
func (n *htmlNode) NextSiblings() (nextSiblings []Node) {
nextSibling := n.NextSibling()
for nextSibling != nil {
nextSiblings = append(nextSiblings, nextSibling)
nextSibling = nextSibling.NextSibling()
}
return
}
func (n *htmlNode) PrevNodes() (prevNodes []Node) {
prevNode := n.PrevNode()
for prevNode != nil {
prevNodes = append(prevNodes, prevNode)
prevNode = prevNode.PrevNode()
}
return
}
func (n *htmlNode) NextNodes() (nextNodes []Node) {
nextNode := n.NextNode()
for nextNode != nil {
nextNodes = append(nextNodes, nextNode)
nextNode = nextNode.NextNode()
}
return
}
type node struct {
*htmlNode
}
// NewNode returns a Node with the specified *html.Node.
func NewNode(n *html.Node) Node {
if n == nil {
return nil
}
return &node{&htmlNode{n}}
}
func (n *node) String() TextNode {
if n.Type() == html.TextNode {
return &textNode{n.htmlNode}
}
if children := n.Children(); len(children) == 1 {
if node := children[0].String(); node != nil {
return node
}
}
return nil
}
func (n *node) Strings() (strings []TextNode) {
if n.Type() == html.TextNode {
strings = append(strings, &textNode{n.htmlNode})
}
for _, node := range n.Descendants() {
if node.Type() == html.TextNode {
strings = append(strings, node.ToTextNode())
}
}
return
}
func (n *node) StrippedStrings() (stripped []string) {
for _, i := range n.Strings() {
if s := strings.TrimSpace(i.String()); s != "" {
stripped = append(stripped, s)
}
}
return
}
func (n *node) GetText() string {
var b strings.Builder
for _, i := range n.Strings() {
b.WriteString(i.String())
}
return b.String()
}
type textNode struct {
*htmlNode
}
func (n *textNode) String() string {
return n.Data()
}