8000 x/net/html: add Node.{Ancestors,ChildNodes,Descendants}() by earthboundkid · Pull Request #215 · golang/net · GitHub
[go: up one dir, main page]

Skip to content

x/net/html: add Node.{Ancestors,ChildNodes,Descendants}() #215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename Children to ChildNodes
  • Loading branch information
earthboundkid committed Oct 28, 2024
commit bb50757b28e40ee7557e061af8e00c5c5a9b207d
8 changes: 4 additions & 4 deletions html/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ func (n *Node) Ancestors() iter.Seq[*Node] {
}
}

// Children returns an iterator over the immediate children of n,
// ChildNodes returns an iterator over the immediate children of n,
// starting with n.FirstChild.
//
// Example:
//
// for child := range n.Children() { ... }
// for child := range n.ChildNodes() { ... }
//
// Mutating a Node or its children while iterating may have unexpected results.
func (n *Node) Children() iter.Seq[*Node] {
func (n *Node) ChildNodes() iter.Seq[*Node] {
_ = n.FirstChild // eager nil check

return func(yield func(*Node) bool) {
Expand All @@ -59,7 +59,7 @@ func (n *Node) Descendants() iter.Seq[*Node] {
}

func (n *Node) descendants(yield func(*Node) bool) bool {
for c := range n.Children() {
for c := range n.ChildNodes() {
if !yield(c) || !c.descendants(yield) {
return false
}
Expand Down
6 changes: 3 additions & 3 deletions html/iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"testing"
)

func TestNode_Children(t *testing.T) {
func TestNode_ChildNodes(t *testing.T) {
tests := []struct {
in string
want string
Expand All @@ -32,11 +32,11 @@ func TestNode_Children(t *testing.T) {
// Drill to <html><head></head><body>
n := doc.FirstChild.FirstChild.NextSibling
var results []string
for c := range n.Children() {
for c := range n.ChildNodes() {
results = append(results, c.Data)
}
if got := strings.Join(results, " "); got != test.want {
t.Errorf("unexpected children yielded by Children; want: %q got: %q", test.want, got)
t.Errorf("unexpected children yielded by ChildNodes; want: %q got: %q", test.want, got)
}
}
}
Expand Down
0