Skip to content

Commit

Permalink
Fix a bad bug in findAll (affecting FindAll, FindAllChildren, FindAll…
Browse files Browse the repository at this point in the history
…Els, etc). Increase test coverage.
  • Loading branch information
bobg committed Jan 12, 2025
1 parent bc26546 commit f32d22b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
4 changes: 1 addition & 3 deletions htree.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ func FindAll(tree *html.Node, pred func(*html.Node) bool) Seq {

func findAll(node *html.Node, pred func(*html.Node) bool, yield func(*html.Node) bool) bool {
if pred(node) {
if !yield(node) {
return false
}
return yield(node)
}
return findAllChildren(node, pred, yield)
}
Expand Down
25 changes: 25 additions & 0 deletions htree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,29 @@ func TestHTML(t *testing.T) {
t.Errorf("got %v, want %v", fields, want)
}
})

t.Run("FindAllChildEls", func(t *testing.T) {
bodyEl := FindEl(root, func(n *html.Node) bool { return n.DataAtom == atom.Body })
if bodyEl == nil {
t.Fatal("no body")
}

var (
seq = FindAllChildEls(bodyEl, func(n *html.Node) bool { return n.DataAtom == atom.Div })
got []string
)
for n := range seq {
got = append(got, ElAttr(n, "class"))
}

want := []string{
"vector-header-container",
"mw-page-container",
"vector-settings",
}

if !slices.Equal(got, want) {
t.Errorf("got %v, want %v", got, want)
}
})
}

0 comments on commit f32d22b

Please sign in to comment.