From b2f6f06846296658801bd0b713307aabbca3a1b4 Mon Sep 17 00:00:00 2001 From: Jayesh Bhoot Date: Thu, 25 Jul 2024 11:10:46 +0530 Subject: [PATCH] exclude text nodes from counting in :has() selector `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. --- src/soup.ml | 1 + test/test.ml | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/soup.ml b/src/soup.ml index 992edd1..2940c5b 100644 --- a/src/soup.ml +++ b/src/soup.ml @@ -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 diff --git a/test/test.ml b/test/test.ml index c3abcb3..1dcae66 100644 --- a/test/test.ml +++ b/test/test.ml @@ -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")