Skip to content

Unable to Correctly Parse :has() Pseudo-Classes with More than a Singular Child #190

Description

@Alexhuszagh

Issue

When attempting to parse a CSS selector using a :has pseudo-class, it works but only if a singular tag name is provided.

For example, if selecting for a child with attributes:

>>> from cssselect import GenericTranslator
>>> GenericTranslator().css_to_xpath('h4.my-class:has(a[href^="/rel"])')
*** cssselect.parser.SelectorSyntaxError: Expected an argument, got <DELIM '[' at 17>

Likewise, if attempting to select for a tag with a descendant tag, we get:

>>> GenericTranslator().css_to_xpath('h4.my-class:has(div a')
*** cssselect.parser.SelectorSyntaxError: Expected an argument, got <IDENT 'a' at 20>

However, selecting for a singular tag name works:

>>> GenericTranslator().css_to_xpath('h4.my-class:has(a)')
"descendant-or-self::h4[(@class and contains(concat(' ', normalize-space(@class), ' '), ' my-class ')) and (descendant::a)]"

Diagnosing it further, this also does not affect combinators inside the :has(...):

>>> GenericTranslator().css_to_xpath('h4.my-class:has(> a)')
"descendant-or-self::h4[(@class and contains(concat(' ', normalize-space(@class), ' '), ' my-class ')) and (./a)]"

Information

Running cssselect version 1.5.0.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions