/
byatom.go
43 lines (39 loc) · 979 Bytes
/
byatom.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
package html
import (
"golang.org/x/net/html/atom"
)
// FindByAtom finds element nodes by their atom across multiple levels.
func (node *Node) FindByAtom(atoms ...atom.Atom) (nodes []*Node) {
nodes = make([]*Node, 1)
nodes[0] = node
for _, a := range atoms {
children := make([]*Node, 0)
for _, n := range nodes {
for _, c := range n.ElementChildren() {
if c.DataAtom == a {
children = append(children, c)
}
}
}
nodes = children
}
return
}
// NextByAtom finds the next sibling element by its atom.
func (node *Node) NextByAtom(atom atom.Atom) *Node {
for node = node.NextElement(); node != nil; node = node.NextElement() {
if node.DataAtom == atom {
return node
}
}
return nil
}
// PrevByAtom finds the previous sibling element by its atom.
func (node *Node) PrevByAtom(atom atom.Atom) *Node {
for node = node.PrevElement(); node != nil; node = node.PrevElement() {
if node.DataAtom == atom {
return node
}
}
return nil
}