Path: blob/main/vendor/golang.org/x/net/html/iter.go
2880 views
// Copyright 2024 The Go Authors. All rights reserved.1// Use of this source code is governed by a BSD-style2// license that can be found in the LICENSE file.34//go:build go1.2356package html78import "iter"910// Ancestors returns an iterator over the ancestors of n, starting with n.Parent.11//12// Mutating a Node or its parents while iterating may have unexpected results.13func (n *Node) Ancestors() iter.Seq[*Node] {14_ = n.Parent // eager nil check1516return func(yield func(*Node) bool) {17for p := n.Parent; p != nil && yield(p); p = p.Parent {18}19}20}2122// ChildNodes returns an iterator over the immediate children of n,23// starting with n.FirstChild.24//25// Mutating a Node or its children while iterating may have unexpected results.26func (n *Node) ChildNodes() iter.Seq[*Node] {27_ = n.FirstChild // eager nil check2829return func(yield func(*Node) bool) {30for c := n.FirstChild; c != nil && yield(c); c = c.NextSibling {31}32}3334}3536// Descendants returns an iterator over all nodes recursively beneath37// n, excluding n itself. Nodes are visited in depth-first preorder.38//39// Mutating a Node or its descendants while iterating may have unexpected results.40func (n *Node) Descendants() iter.Seq[*Node] {41_ = n.FirstChild // eager nil check4243return func(yield func(*Node) bool) {44n.descendants(yield)45}46}4748func (n *Node) descendants(yield func(*Node) bool) bool {49for c := range n.ChildNodes() {50if !yield(c) || !c.descendants(yield) {51return false52}53}54return true55}565758