-
Notifications
You must be signed in to change notification settings - Fork 10
/
hdq.go
552 lines (463 loc) · 11.9 KB
/
hdq.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
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 (
"bytes"
"errors"
"io"
"github.com/goplus/hdq/stream"
"golang.org/x/net/html"
_ "github.com/goplus/hdq/stream/zip"
)
const (
GopPackage = true // to indicate this is a Go+ package
)
var (
ErrNotFound = errors.New("entity not found")
ErrBreak = errors.New("break")
ErrTooManyNodes = errors.New("too many nodes")
ErrInvalidNode = errors.New("invalid node")
// ErrEmptyText represents an `empty text` error.
ErrEmptyText = errors.New("empty text")
// ErrInvalidScanFormat represents an `invalid fmt.Scan format` error.
ErrInvalidScanFormat = errors.New("invalid fmt.Scan format")
)
// -----------------------------------------------------------------------------
type NodeEnum interface {
ForEach(filter func(node *html.Node) error)
}
type cachedGetter interface {
Cached() int
}
// NodeSet represents a set of nodes.
type NodeSet struct {
Data NodeEnum
Err error
}
// New creates a NodeSet object.
func New(r io.Reader) NodeSet {
doc, err := html.Parse(r)
if err != nil {
return NodeSet{Err: err}
}
return NodeSet{Data: oneNode{doc}}
}
// Source opens a stream (if necessary) to create a NodeSet object.
func Source(r interface{}) (ret NodeSet) {
switch v := r.(type) {
case string:
f, err := stream.Open(v)
if err != nil {
return NodeSet{Err: err}
}
return New(f)
case []byte:
r := bytes.NewReader(v)
return New(r)
case io.Reader:
return New(v)
case NodeSet: // input is a node set
return v
default:
panic("unsupport source type")
}
}
func (p NodeSet) Ok() bool {
return p.Err == nil
}
func (p NodeSet) All() NodeSet {
if _, ok := p.Data.(cachedGetter); ok {
return p
}
nodes, err := p.Collect()
if err != nil {
return NodeSet{Err: err}
}
return NodeSet{Data: &fixNodes{nodes}}
}
func (p NodeSet) Gop_Enum(callback func(node NodeSet)) {
if p.Err == nil {
p.Data.ForEach(func(node *html.Node) error {
t := NodeSet{Data: oneNode{node}}
callback(t)
return nil
})
}
}
func (p NodeSet) ForEach(callback func(node NodeSet)) {
p.Gop_Enum(callback)
}
// Render renders the node set to the given writer.
func (p NodeSet) Render(w io.Writer, suffix ...string) (err error) {
if p.Err != nil {
return p.Err
}
p.Data.ForEach(func(node *html.Node) error {
if e := html.Render(w, node); e != nil {
err = e
return ErrBreak
}
if suffix != nil {
io.WriteString(w, suffix[0])
}
return nil
})
return
}
// -----------------------------------------------------------------------------
type oneNode struct {
*html.Node
}
func (p oneNode) ForEach(filter func(node *html.Node) error) {
filter(p.Node)
}
func (p oneNode) Cached() int {
return 1
}
// -----------------------------------------------------------------------------
type fixNodes struct {
nodes []*html.Node
}
func (p *fixNodes) ForEach(filter func(node *html.Node) error) {
for _, node := range p.nodes {
if filter(node) == ErrBreak {
return
}
}
}
func (p *fixNodes) Cached() int {
return len(p.nodes)
}
// Nodes creates a node set from the given nodes.
func Nodes(nodes ...*html.Node) (ret NodeSet) {
return NodeSet{Data: &fixNodes{nodes}}
}
// -----------------------------------------------------------------------------
const (
unknownNumNodes = -1
)
type anyNodes struct {
data NodeEnum
}
func (p *anyNodes) ForEach(filter func(node *html.Node) error) {
p.data.ForEach(func(node *html.Node) error {
anyForEach(node, filter)
return nil
})
}
func (p *anyNodes) Cached() int {
return unknownNumNodes
}
func anyForEach(p *html.Node, filter func(node *html.Node) error) error {
if err := filter(p); err == nil || err == ErrBreak {
return err
}
for node := p.FirstChild; node != nil; node = node.NextSibling {
if anyForEach(node, filter) == ErrBreak {
return ErrBreak
}
}
return nil
}
// Any returns the all nodes as a node set.
func (p NodeSet) Any() (ret NodeSet) {
if p.Err != nil {
return p
}
return NodeSet{Data: &anyNodes{p.Data}}
}
// -----------------------------------------------------------------------------
type childLevelNodes struct {
data NodeEnum
level int
}
func (p *childLevelNodes) ForEach(filter func(node *html.Node) error) {
p.data.ForEach(func(node *html.Node) error {
return childLevelForEach(node, p.level, filter)
})
}
func childLevelForEach(p *html.Node, level int, filter func(node *html.Node) error) error {
if level == 0 {
return filter(p)
}
level--
for node := p.FirstChild; node != nil; node = node.NextSibling {
if childLevelForEach(node, level, filter) == ErrBreak {
return ErrBreak
}
}
return nil
}
type parentLevelNodes struct {
data NodeEnum
level int
}
func (p *parentLevelNodes) ForEach(filter func(node *html.Node) error) {
p.data.ForEach(func(node *html.Node) error {
return parentLevelForEach(node, p.level, filter)
})
}
func parentLevelForEach(p *html.Node, level int, filter func(node *html.Node) error) error {
for level < 0 {
if p = p.Parent; p == nil {
return ErrNotFound
}
level++
}
return filter(p)
}
// Child returns the child node set. It is equivalent to ChildN(1).
func (p NodeSet) Child() (ret NodeSet) {
return p.ChildN(1)
}
// ChildN returns the child node set at the given level.
func (p NodeSet) ChildN(level int) (ret NodeSet) {
if p.Err != nil || level == 0 {
return p
}
if level > 0 {
return NodeSet{Data: &childLevelNodes{p.Data, level}}
}
return NodeSet{Data: &parentLevelNodes{p.Data, level}}
}
// Parent returns the parent node set. It is equivalent to ParentN(1).
func (p NodeSet) Parent() (ret NodeSet) {
return p.ChildN(-1)
}
// ParentN returns the parent node set at the given level.
func (p NodeSet) ParentN(level int) (ret NodeSet) {
return p.ChildN(-level)
}
// One returns the first node as a node set.
func (p NodeSet) One() (ret NodeSet) {
if _, ok := p.Data.(oneNode); ok {
return p
}
node, err := p.CollectOne__1(false)
if err != nil {
return NodeSet{Err: err}
}
return NodeSet{Data: oneNode{node}}
}
// -----------------------------------------------------------------------------
type siblingNodes struct {
data NodeEnum
delta int
}
func (p *siblingNodes) ForEach(filter func(node *html.Node) error) {
p.data.ForEach(func(node *html.Node) error {
return siblingForEach(node, p.delta, filter)
})
}
func siblingForEach(p *html.Node, delta int, filter func(node *html.Node) error) error {
for delta > 0 {
if p = p.NextSibling; p == nil {
return ErrNotFound
}
delta--
}
for delta < 0 {
if p = p.PrevSibling; p == nil {
return ErrNotFound
}
delta++
}
return filter(p)
}
func (p NodeSet) NextSibling(delta int) (ret NodeSet) {
if p.Err != nil {
return p
}
return NodeSet{Data: &siblingNodes{p.Data, delta}}
}
func (p NodeSet) PrevSibling(delta int) (ret NodeSet) {
return p.NextSibling(-delta)
}
// -----------------------------------------------------------------------------
type prevSiblingNodes struct {
data NodeEnum
}
func (p *prevSiblingNodes) ForEach(filter func(node *html.Node) error) {
p.data.ForEach(func(node *html.Node) error {
for p := node.PrevSibling; p != nil; p = p.PrevSibling {
if filter(p) == ErrBreak {
return ErrBreak
}
}
return nil
})
}
func (p NodeSet) PrevSiblings() (ret NodeSet) {
if p.Err != nil {
return p
}
return NodeSet{Data: &prevSiblingNodes{p.Data}}
}
// -----------------------------------------------------------------------------
type nextSiblingNodes struct {
data NodeEnum
}
func (p *nextSiblingNodes) ForEach(filter func(node *html.Node) error) {
p.data.ForEach(func(node *html.Node) error {
for p := node.NextSibling; p != nil; p = p.NextSibling {
if filter(p) == ErrBreak {
return ErrBreak
}
}
return nil
})
}
func (p NodeSet) NextSiblings() (ret NodeSet) {
if p.Err != nil {
return p
}
return NodeSet{Data: &nextSiblingNodes{p.Data}}
}
// -----------------------------------------------------------------------------
type firstChildNodes struct {
data NodeEnum
nodeType html.NodeType
}
func (p *firstChildNodes) ForEach(filter func(node *html.Node) error) {
p.data.ForEach(func(node *html.Node) error {
child, err := firstChild(node, p.nodeType)
if err != nil {
return err
}
return filter(child)
})
}
func (p NodeSet) FirstChild(nodeType html.NodeType) (ret NodeSet) {
if p.Err != nil {
return p
}
return NodeSet{Data: &firstChildNodes{p.Data, nodeType}}
}
func (p NodeSet) FirstTextChild() (ret NodeSet) {
return p.FirstChild(html.TextNode)
}
func (p NodeSet) FirstElementChild() (ret NodeSet) {
return p.FirstChild(html.ElementNode)
}
// -----------------------------------------------------------------------------
type lastChildNodes struct {
data NodeEnum
nodeType html.NodeType
}
func (p *lastChildNodes) ForEach(filter func(node *html.Node) error) {
p.data.ForEach(func(node *html.Node) error {
child, err := lastChild(node, p.nodeType)
if err != nil {
return err
}
return filter(child)
})
}
func (p NodeSet) LastChild(nodeType html.NodeType) (ret NodeSet) {
if p.Err != nil {
return p
}
return NodeSet{Data: &lastChildNodes{p.Data, nodeType}}
}
func (p NodeSet) LastTextChild() (ret NodeSet) {
return p.LastChild(html.TextNode)
}
func (p NodeSet) LastElementChild() (ret NodeSet) {
return p.LastChild(html.ElementNode)
}
// -----------------------------------------------------------------------------
type matchedNodes struct {
data NodeEnum
filter func(node *html.Node) bool
}
func (p *matchedNodes) ForEach(filter func(node *html.Node) error) {
p.data.ForEach(func(node *html.Node) error {
if p.filter(node) {
return filter(node)
}
return ErrNotFound
})
}
// Match returns the matched node set.
func (p NodeSet) Match(filter func(node *html.Node) bool) (ret NodeSet) {
if p.Err != nil {
return p
}
return NodeSet{Data: &matchedNodes{p.Data, filter}}
}
// -----------------------------------------------------------------------------
type textNodes struct {
data NodeEnum
doReplace bool
}
func (p *textNodes) ForEach(filter func(node *html.Node) error) {
p.data.ForEach(func(t *html.Node) error {
node := &html.Node{
Parent: t,
Type: html.TextNode,
Data: textOf(t),
}
if p.doReplace {
t.FirstChild = node
t.LastChild = node
}
return filter(node)
})
}
func (p NodeSet) ChildrenAsText(doReplace bool) (ret NodeSet) {
if p.Err != nil {
return p
}
return NodeSet{Data: &textNodes{p.Data, doReplace}}
}
// -----------------------------------------------------------------------------
// CollectOne returns the first node.
// If `exactly` is true, it will return an error if there are more than one node.
func (p NodeSet) CollectOne__1(exactly bool) (item *html.Node, err error) {
if p.Err != nil {
return nil, p.Err
}
err = ErrNotFound
if exactly {
p.Data.ForEach(func(node *html.Node) error {
if err == ErrNotFound {
item, err = node, nil
return nil
}
err = ErrTooManyNodes
return ErrBreak
})
} else {
p.Data.ForEach(func(node *html.Node) error {
item, err = node, nil
return ErrBreak
})
}
return
}
// CollectOne returns the first node.
func (p NodeSet) CollectOne__0() (item *html.Node, err error) {
return p.CollectOne__1(false)
}
// Collect returns all nodes.
func (p NodeSet) Collect() (items []*html.Node, err error) {
if p.Err != nil {
return nil, p.Err
}
p.Data.ForEach(func(node *html.Node) error {
items = append(items, node)
return nil
})
return
}
// -----------------------------------------------------------------------------