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
2 changes: 1 addition & 1 deletion cssselect/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ def parse_relative_selector(stream: TokenStream) -> tuple[Token, Selector]:
# else it would be a descendant combinator, which is not
# supported in :has() arguments.
seen_whitespace = True
elif next_.type == "IDENT" or next_.is_delim(".", "*"):
elif next_.type in ("IDENT", "HASH") or next_.is_delim(".", "*"):
if seen_whitespace:
raise SelectorSyntaxError(f"Expected an argument, got {next_}")
subselector_tokens.append(next_)
Expand Down
11 changes: 11 additions & 0 deletions tests/test_cssselect.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ def parse_many(first: str, *others: str) -> list[str]:
assert parse_many("div:has(~ div.foo)") == [
"Relation[Element[div]:has(~ Selector[Class[Element[div].foo]])]"
]
assert parse_many("div:has(#foo)") == [
"Relation[Element[div]:has(Selector[Hash[Element[*]#foo]])]"
]
assert parse_many("div:has(> a#foo.bar)") == [
"Relation[Element[div]:has(> Selector[Class[Hash[Element[a]#foo].bar]])]"
]
assert parse_many("div:is(.foo, #bar)") == [
"Matching[Element[div]:is(Class[Element[*].foo], Hash[Element[*]#bar])]"
]
Expand Down Expand Up @@ -363,6 +369,7 @@ def specificity(css: str) -> tuple[int, int, int]:
assert specificity(":has(foo)") == (0, 0, 1)
assert specificity(":has(.foo)") == (0, 1, 0)
assert specificity(":has(> foo)") == (0, 0, 1)
assert specificity(":has(#foo)") == (1, 0, 0)

assert specificity(":is(.foo, #bar)") == (1, 0, 0)
assert specificity(":is(:hover, :visited)") == (0, 1, 0)
Expand Down Expand Up @@ -428,6 +435,7 @@ def css2css(css: str, res: str | None = None) -> None:
css2css(":has(~ foo)")
css2css(":has(+ foo)")
css2css("div:has(> div.foo)")
css2css("div:has(> #foo)")
css2css(":is(#bar, .foo)")
css2css(":is(:focused, :visited)")
css2css(":where(:focused, :visited)")
Expand Down Expand Up @@ -625,6 +633,7 @@ def get_error(css: str) -> str | None:
assert get_error(":has()") == ("Expected selector, got <EOF at 5>")
assert get_error(":has(a b)") == ("Expected an argument, got <IDENT 'b' at 7>")
assert get_error(":has(a .b)") == ("Expected an argument, got <DELIM '.' at 7>")
assert get_error(":has(a #b)") == ("Expected an argument, got <HASH 'b' at 7>")
# '-' is not a valid relative combinator
assert get_error(":has(- p)") == ("Expected an argument, got <DELIM '-' at 5>")
# Strings and numbers are not selectors
Expand Down Expand Up @@ -745,6 +754,8 @@ def xpath(css: str) -> str:
"e[./*[@class and contains("
"concat(' ', normalize-space(@class), ' '), ' bar ')]]"
)
assert xpath("e:has(> #bar)") == "e[./*[@id = 'bar']]"
assert xpath("e:has(#bar)") == "e[descendant::*[@id = 'bar']]"
assert xpath("e:has(~ f.bar)") == (
"e[following-sibling::f[@class and contains("
"concat(' ', normalize-space(@class), ' '), ' bar ')]]"
Expand Down