Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 27 additions & 7 deletions cssselect/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -826,19 +840,25 @@ 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(")"):
break
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(
Expand Down
3 changes: 2 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions tests/test_cssselect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <DELIM '>' at 0>")

assert get_error(":has()") == ("Expected selector, got <DELIM ')' at 5>")
Expand Down
Loading