[csswg-drafts] [selectors-4] spec refactoring idea: flat representation for pseudo-element interpretation (#9933)

qnighy has just created a new issue for https://github.com/w3c/csswg-drafts:

== [selectors-4] spec refactoring idea: flat representation for pseudo-element interpretation ==
The selectors-4 spec today is somewhat vague about pseudo-element selector interpretation. As I understand it, this indirectly blocks pseudo-elements-related improvements like #2284.

Here's my idea of refactoring the matching algorithm by introducing "flat representation":

- Require that every combinator is equivalent to its right-conjunctive form, i.e. for example for `>`, `A > B` is roughly equivalent to `:is(A > *):is(B)`.
  - This requirement is necessary to ensure that the flat representation is equivalent to the original complex selector in the absence of pseudo-element selectors.
- Define a _**flat representation**_ to be any sequence of simple selectors and combinators. For example, the flat representation of `p.foo ul.bar > li` is \[`p`, `.foo`, ` `, `ul`, `.bar`, `>`, `li`\]. Define transformation from complex selectors or relative complex selectors to their flat representation.
- Define _**the set of elements and pseudo-elements matched by the flat representation**_, given an _initial set_ (a set of elements and pseudo-elements, defaults to all the elements of the document in question). The algorithm is as follows:
  1. Initialize _current set_ as _initial set_.
  2. For each element _x_ in the flat representation, update the _current set_ according to the following rule:
     - If _x_ is a simple selector other than a pseudo-element selector, apply a corresponding filter to _current set_ to get a smaller set.
       - For example, `.foo` narrows the current set by selecting only the elements with the class `foo`.
     - If _x_ is a pseudo-element selector or a combinator, derive a new _current set_ value in relation to the original value of _current set_.
       - For example, `>` evaluates to the set of all the elements that are children of any element in the original set.
       - For example, `::before` evaluates to the set of all the "before" pseudo-elements referencing any element in the original set.
  3. Return the _current set_.
- Redefine to _**match a selector against an element**_, _**match a selector against a pseudo-element**_, and _**match a selector against a tree**_ in terms of the set of elements and pseudo-elements matched by the flat representation.

It would be beneficial in the following ways:

- Selectors and relative selectors can be described more uniformly.
- Pseudo-element selectors in any position gets well-defined semantics, as long as the selector is allowed to appear there.


Please view or discuss this issue at https://github.com/w3c/csswg-drafts/issues/9933 using your GitHub account


-- 
Sent via github-notify-ml as configured in https://github.com/w3c/github-notify-ml-config

Received on Monday, 12 February 2024 01:50:07 UTC