Skip to content

Commit

Permalink
exclude text nodes from counting in :has() selector
Browse files Browse the repository at this point in the history
`descendants` function also includes text nodes,
which causes `name node` function to fail down the line,
because it only expects an ``Element of n`, not
``Text of string`, which a text node is.

In any case, :has() doesn't select text nodes. So,
excluding them beforehand is the way to go.
  • Loading branch information
jbhoot committed Jul 25, 2024
1 parent 3f0275a commit b2f6f06
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/soup.ml
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ struct
| Content s -> texts node |> String.concat "" |> has_substring s
| Has selector ->
descendants node
|> filter (fun descendant -> not (is_text descendant))
|> filter (fun descendant -> matches_simple_selector descendant selector)
|> count
|> fun count -> count > 0
Expand Down
2 changes: 2 additions & 0 deletions test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ let suites = [
test "ol:has([id=one])" 0;
test "li:has([id=one])" 0;
test ":has(.odd)" 4;
test ":has(ul)" 2;
test ":has(caption)" 0;
test
("html:root > body.lists[class~=lists] > ul > li#one:nth-child(1) " ^
"+ li#two")
Expand Down

0 comments on commit b2f6f06

Please sign in to comment.