@@ -8,9 +8,22 @@ package html
8
8
9
9
import "iter"
10
10
11
+ // Parents returns an sequence yielding the node and its parents.
12
+ //
13
+ // Mutating a Node or its parents while iterating may have unexpected results.
14
+ func (n * Node ) Parents () iter.Seq [* Node ] {
15
+ return func (yield func (* Node ) bool ) {
16
+ for p := n ; p != nil ; p = p .Parent {
17
+ if ! yield (p ) {
18
+ return
19
+ }
20
+ }
21
+ }
22
+ }
23
+
11
24
// ChildNodes returns a sequence yielding the immediate children of n.
12
25
//
13
- // Mutating a Node while iterating through its ChildNodes may have unexpected results.
26
+ // Mutating a Node or its ChildNodes while iterating may have unexpected results.
14
27
func (n * Node ) ChildNodes () iter.Seq [* Node ] {
15
28
return func (yield func (* Node ) bool ) {
16
29
if n == nil {
@@ -25,22 +38,9 @@ func (n *Node) ChildNodes() iter.Seq[*Node] {
25
38
26
39
}
27
40
28
- func (n * Node ) all (yield func (* Node ) bool ) bool {
29
- if ! yield (n ) {
30
- return false
31
- }
32
-
33
- for c := range n .ChildNodes () {
34
- if ! c .all (yield ) {
35
- return false
36
- }
37
- }
38
- return true
39
- }
40
-
41
41
// All returns a sequence yielding all descendents of n in depth-first pre-order.
42
42
//
43
- // Mutating a Node while iterating through it or its descendents may have unexpected results.
43
+ // Mutating a Node or its descendents while iterating may have unexpected results.
44
44
func (n * Node ) All () iter.Seq [* Node ] {
45
45
return func (yield func (* Node ) bool ) {
46
46
if n == nil {
@@ -50,15 +50,15 @@ func (n *Node) All() iter.Seq[*Node] {
50
50
}
51
51
}
52
52
53
- // Parents returns an sequence yielding the node and its parents.
54
- //
55
- // Mutating a Node while iterating through it or its parents may have unexpected results.
56
- func (n * Node ) Parents () iter.Seq [* Node ] {
57
- return func (yield func (* Node ) bool ) {
58
- for p := n ; p != nil ; p = p .Parent {
59
- if ! yield (p ) {
60
- return
61
- }
53
+ func (n * Node ) all (yield func (* Node ) bool ) bool {
54
+ if ! yield (n ) {
55
+ return false
56
+ }
57
+
58
+ for c := range n .ChildNodes () {
59
+ if ! c .all (yield ) {
60
+ return false
62
61
}
63
62
}
63
+ return true
64
64
}
0 commit comments