-
Notifications
You must be signed in to change notification settings - Fork 11
/
chain.go
95 lines (79 loc) · 1.71 KB
/
chain.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
// Copyright 2014, Hǎiliàng Wáng. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package query
import (
"regexp"
. "h12.io/html-query/expr"
)
func (n *Node) Satisfy(cs ...Checker) bool {
return And(cs...)(n.InternalNode()) != nil
}
func (n *Node) Find(cs ...Checker) *Node {
if n == nil {
return nil
}
return NewNode(Find(cs...)(&n.n))
}
func (n *Node) FindNext(cs ...Checker) *Node {
if n == nil {
return nil
}
return NewNode(FindSibling(cs...)(&n.n))
}
func (n *Node) FindChild(cs ...Checker) *Node {
return NewNode(FindChild(cs...)(&n.n))
}
func (n *Node) find(c Checker, cs []Checker) *Node {
if n == nil {
return nil
}
return n.Find(append([]Checker{c}, cs...)...)
}
func (n *Node) NextSibling() *Node {
if n == nil {
return nil
}
return NewNode(NextSibling(&n.n))
}
func (n *Node) PrevSibling() *Node {
if n == nil {
return nil
}
return NewNode(PrevSibling(&n.n))
}
func (n *Node) Parent() *Node {
if n == nil {
return nil
}
return NewNode(Parent(&n.n))
}
func (n *Node) Children(cs ...Checker) NodeIter {
if n == nil {
return NodeIter{nil}
}
return NodeIter{Children(&n.n, cs...)}
}
func (n *Node) Descendants(cs ...Checker) NodeIter {
if n == nil {
return NodeIter{nil}
}
return NodeIter{Descendants(&n.n, cs...)}
}
func (n *Node) Ahref(cs ...Checker) *Node {
if n == nil {
return nil
}
return n.find(Ahref, cs)
}
func (n *Node) TextNode(pat string) *TextNodeNode {
if n == nil {
return nil
}
rx := regexp.MustCompile(pat)
cs := []Checker{Text_(rx)}
return NewTextNodeNode(n.find(TextNode, cs), rx)
}
func also(c Checker, cs []Checker) []Checker {
return append([]Checker{c}, cs...)
}