Skip to content

Parse user-action pseudo-classes after pseudo-elements - #224

Closed
Aschenbath wants to merge 1 commit into
scrapy:masterfrom
Aschenbath:fix/pseudo-element-states-144
Closed

Aschenbath wants to merge 1 commit into
scrapy:masterfrom
Aschenbath:fix/pseudo-element-states-144

Conversation

@Aschenbath

Copy link
Copy Markdown

.something::before:hover is valid Selectors Level 4 syntax, but parsing it currently raises SelectorSyntaxError. Support user-action pseudo-classes after pseudo-elements, keeping their state separate from the originating element so canonical output and specificity preserve the difference between ::before:hover and :hover::before.

Fixes #144.

The parser accepts :hover, :active, :focus, :focus-visible and :focus-within, plus their :is(), :where() and :not() combinations within the existing limits of those functions. The same restrictions apply inside their arguments. Classes, IDs, attributes, explicit type/universal selectors, combinators and further pseudo-elements remain rejected in this position. Pseudo-element-specific additional states are outside this change.

Newly accepted selectors expose a QualifiedPseudoElement with the original pseudo-element and a separate state tree. Previously accepted selectors keep their existing string or FunctionalPseudoElement representation. Default XPath translation still rejects pseudo-elements; selector_to_xpath with its default options ignores the entire qualified pseudo-element. Custom translators receive the wrapper and can explicitly handle or reject its state.

Validation on Linux, CPython 3.12.12:

  • The new regression file fails 20 cases on the unchanged base; all 61 cases pass with the change.
  • Full tests and documentation examples: 111 passed, 1 expected failure, 76 subtests passed; source statement and branch coverage are both 100%.
  • Ruff lint/format, strict mypy, pylint, Sphinx with warnings treated as errors, sdist/wheel builds and Twine checks pass.
  • 2,500 generated valid combinations preserve canonical output, calculated specificity and originating-element XPath behavior, both from source and from the installed wheel.
  • All 1,884 selectors accepted by the base in a 2,345-input compatibility corpus retain identical representation, canonical output, specificity and XPath results.

An independent Codex reviewer also passed 5,591 assertions against source and the installed wheel, checking real XPath results against a separate Boolean oracle and confirming pseudo-element state stays separate from host state.

Other Python versions and platforms were not run locally. OpenAI Codex implemented and tested this change; a separate Codex agent reviewed it. No human review is claimed.

@wRAR wRAR added the spam label Nov 8, 2026
@wRAR wRAR closed this Nov 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pseudo-element not at the end of a selector

2 participants