@@ -2,6 +2,7 @@ package node
22
33import (
44 "io"
5+ "iter"
56 "strings"
67
78 "golang.org/x/net/html"
@@ -92,6 +93,16 @@ type HtmlNode interface {
9293 // NextNodes return all of the nodes that was parsed after this node.
9394 NextNodes () []Node
9495
96+ // AncestorNodes returns an iterator over the ancestors of n,
97+ // starting with n.Parent.
98+ AncestorNodes () iter.Seq [Node ]
99+ // ChildNodes returns an iterator over the immediate children of n,
100+ // starting with n.FirstChild.
101+ ChildNodes () iter.Seq [Node ]
102+ // DescendantNodes returns an iterator over all nodes recursively
103+ // beneath n, excluding n itself. Nodes are visited in depth-first preorder.
104+ DescendantNodes () iter.Seq [Node ]
105+
95106 // Finder includes a set of find methods.
96107 Finder
97108}
@@ -213,34 +224,23 @@ func (n *htmlNode) NextNode() Node {
213224}
214225
215226func (n * htmlNode ) Parents () (parents []Node ) {
216- parent := n .Parent ()
217- for parent != nil {
227+ for parent := range n .AncestorNodes () {
218228 parents = append (parents , parent )
219- parent = parent .Parent ()
220229 }
221230 return
222231}
223232
224233func (n * htmlNode ) Children () (children []Node ) {
225- child := n .FirstChild ()
226- for child != nil {
234+ for child := range n .ChildNodes () {
227235 children = append (children , child )
228- child = child .NextSibling ()
229236 }
230237 return
231238}
232239
233240func (n * htmlNode ) Descendants () (nodes []Node ) {
234- var f func (Node )
235- f = func (node Node ) {
236- if node .Raw () != n .Raw () {
237- nodes = append (nodes , node )
238- }
239- for node := node .FirstChild (); node != nil ; node = node .NextSibling () {
240- f (node )
241- }
241+ for node := range n .DescendantNodes () {
242+ nodes = append (nodes , node )
242243 }
243- f (n .ToNode ())
244244 return
245245}
246246
@@ -280,6 +280,36 @@ func (n *htmlNode) NextNodes() (nextNodes []Node) {
280280 return
281281}
282282
283+ func (n * htmlNode ) AncestorNodes () iter.Seq [Node ] {
284+ return func (yield func (Node ) bool ) {
285+ for c := range n .Ancestors () {
286+ if ! yield (NewNode (c )) {
287+ return
288+ }
289+ }
290+ }
291+ }
292+
293+ func (n * htmlNode ) ChildNodes () iter.Seq [Node ] {
294+ return func (yield func (Node ) bool ) {
295+ for c := range n .Node .ChildNodes () {
296+ if ! yield (NewNode (c )) {
297+ return
298+ }
299+ }
300+ }
301+ }
302+
303+ func (n * htmlNode ) DescendantNodes () iter.Seq [Node ] {
304+ return func (yield func (Node ) bool ) {
305+ for c := range n .Node .Descendants () {
306+ if ! yield (NewNode (c )) {
307+ return
308+ }
309+ }
310+ }
311+ }
312+
283313type node struct {
284314 * htmlNode
285315}
0 commit comments