From b7ed396a7795078b02e6fb09a5a1a9c224cf8477 Mon Sep 17 00:00:00 2001 From: Siraj saheb Date: Thu, 29 Oct 2026 12:29:24 +0530 Subject: [PATCH] reject nested :has() in parse_simple_selector --- cssselect/parser.py | 34 +++++++++++++++++++++++++++------- docs/index.rst | 3 ++- tests/test_cssselect.py | 7 +++++++ 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/cssselect/parser.py b/cssselect/parser.py index 4c5349a..9aaeee0 100644 --- a/cssselect/parser.py +++ b/cssselect/parser.py @@ -730,7 +730,9 @@ def parse_simple_selector( if inside_negation: raise SelectorSyntaxError("Got nested :not()") argument, argument_pseudo_element = parse_simple_selector( - stream, inside_negation=True + stream, + inside_negation=True, + inside_relative_selector=inside_relative_selector, ) while 1: # Whitespace before the closing parenthesis is not a @@ -753,19 +755,31 @@ def parse_simple_selector( else: argument_combinator = " " next_selector, argument_pseudo_element = parse_simple_selector( - stream, inside_negation=True + stream, + inside_negation=True, + inside_relative_selector=inside_relative_selector, ) argument = CombinedSelector( argument, argument_combinator, next_selector ) result = Negation(result, argument) elif ident.lower() == "has": + # :has() cannot be nested (Selectors 4, 14.3), not even + # through :is(), :where() or :not(). Each level would also + # nest an XPath predicate whose evaluation cost multiplies + # with the size of the document. + if inside_relative_selector: + raise SelectorSyntaxError("Got nested :has()") result = Relation(result, parse_relative_selector(stream)) elif ident.lower() in ("matches", "is"): - selectors = parse_simple_selector_arguments(stream) + selectors = parse_simple_selector_arguments( + stream, inside_relative_selector + ) result = Matching(result, selectors) elif ident.lower() == "where": - selectors = parse_simple_selector_arguments(stream) + selectors = parse_simple_selector_arguments( + stream, inside_relative_selector + ) result = SpecificityAdjustment(result, selectors) else: result = Function(result, ident, parse_arguments(stream)) @@ -826,7 +840,9 @@ def parse_relative_selector(stream: TokenStream) -> list[tuple[Token, Selector]] stream.skip_whitespace() else: argument_combinator = " " - next_selector, pseudo_element = parse_simple_selector(stream) + next_selector, pseudo_element = parse_simple_selector( + stream, inside_relative_selector=True + ) result = CombinedSelector(result, argument_combinator, next_selector) arguments.append((combinator, Selector(result))) if stream.next().is_delim(")"): @@ -834,11 +850,15 @@ def parse_relative_selector(stream: TokenStream) -> list[tuple[Token, Selector]] return arguments -def parse_simple_selector_arguments(stream: TokenStream) -> list[Tree]: +def parse_simple_selector_arguments( + stream: TokenStream, inside_relative_selector: bool = False +) -> list[Tree]: arguments = [] while 1: result, pseudo_element = parse_simple_selector( - stream, inside_selector_list=True + stream, + inside_selector_list=True, + inside_relative_selector=inside_relative_selector, ) if pseudo_element: raise SelectorSyntaxError( diff --git a/docs/index.rst b/docs/index.rst index 1d5b49c..76a8607 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -114,7 +114,8 @@ be implemented): also rejected inside them, while ``:has()`` is supported (e.g. ``:is(:has(> a))``). * The ``:has()`` pseudo-class, e.g. ``:has(> a.important, + p b)``. Limitation: - ``:scope`` is rejected inside it. + ``:scope`` is rejected inside it, and like in the specification it cannot + be nested (e.g. ``:has(a:has(b))`` or ``:has(:is(:has(b)))``). * The ``i`` and ``s`` attribute selector flags, e.g. ``[href^="HTTP" i]``. ``i`` makes the value comparison ASCII case-insensitive. ``s`` is accepted but has no effect, since attribute values are already compared diff --git a/tests/test_cssselect.py b/tests/test_cssselect.py index 266bbca..2553d6e 100644 --- a/tests/test_cssselect.py +++ b/tests/test_cssselect.py @@ -677,6 +677,13 @@ def get_error(css: str) -> str | None: assert get_error("a:has(b, :scope)") == ( 'Got pseudo-class ":scope" not at the start of a selector' ) + assert get_error("a:has(b:has(c))") == ("Got nested :has()") + assert get_error("a:has(b c:has(d))") == ("Got nested :has()") + assert get_error("a:has(> b, ~ c:has(d))") == ("Got nested :has()") + assert get_error("a:has(:is(b, c:has(d)))") == ("Got nested :has()") + assert get_error("a:has(:where(:has(d)))") == ("Got nested :has()") + assert get_error("a:has(:not(b:has(d)))") == ("Got nested :has()") + assert get_error("a:has(b :not(c > d:has(e)))") == ("Got nested :has()") assert get_error("> div p") == ("Expected selector, got ' at 0>") assert get_error(":has()") == ("Expected selector, got ")