-
Notifications
You must be signed in to change notification settings - Fork 0
/
neighbors.go
108 lines (96 loc) · 2.5 KB
/
neighbors.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
package dom
import "golang.org/x/net/html"
// Warning: It is not meant to be called directly and may change signature from release to release!
func UNSTABLE_initGetNeighbor(
firstChildFunc func(node *html.Node) *html.Node,
prevNextFunc func(node *html.Node) *html.Node,
goUpUntilFunc func(node *html.Node) bool,
) func(*html.Node) *html.Node {
return func(node *html.Node) *html.Node {
// First look at the children
if child := firstChildFunc(node); child != nil {
return child
}
// Otherwise my prev/next sibling
if sibling := prevNextFunc(node); sibling != nil {
return sibling
}
for {
// Finally, continously go upwards until we find an element with a sibling
node = node.Parent
if node == nil {
// We reached the top
return nil
}
if goUpUntilFunc(node) {
// Don't go too far up...
return nil
}
sibling := prevNextFunc(node)
if sibling != nil {
return sibling
}
}
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
var goUpForever = func(node *html.Node) bool { return false }
var skipFirstChild = func(node *html.Node) *html.Node { return nil }
func GetPrevNeighborNode(node *html.Node) *html.Node {
return UNSTABLE_initGetNeighbor(
FirstChildNode,
PrevSiblingNode,
goUpForever,
)(node)
}
func GetPrevNeighborElement(node *html.Node) *html.Node {
return UNSTABLE_initGetNeighbor(
FirstChildElement,
PrevSiblingElement,
goUpForever,
)(node)
}
func GetPrevNeighborNodeExcludingOwnChild(node *html.Node) *html.Node {
return UNSTABLE_initGetNeighbor(
skipFirstChild,
PrevSiblingNode,
goUpForever,
)(node)
}
func GetPrevNeighborElementExcludingOwnChild(node *html.Node) *html.Node {
return UNSTABLE_initGetNeighbor(
skipFirstChild,
PrevSiblingElement,
goUpForever,
)(node)
}
// - - - - - - - - //
func GetNextNeighborNode(node *html.Node) *html.Node {
return UNSTABLE_initGetNeighbor(
FirstChildNode,
NextSiblingNode,
goUpForever,
)(node)
}
func GetNextNeighborElement(node *html.Node) *html.Node {
return UNSTABLE_initGetNeighbor(
FirstChildElement,
NextSiblingElement,
goUpForever,
)(node)
}
func GetNextNeighborNodeExcludingOwnChild(node *html.Node) *html.Node {
return UNSTABLE_initGetNeighbor(
skipFirstChild,
NextSiblingNode,
goUpForever,
)(node)
}
func GetNextNeighborElementExcludingOwnChild(node *html.Node) *html.Node {
return UNSTABLE_initGetNeighbor(
skipFirstChild,
NextSiblingElement,
goUpForever,
)(node)
}