Predicate composition helpers
Predicate composition helpers preserve selector meaning while the compiler turns parsed selector rules into functions. They identify traversal and scope-sensitive rules, order checks by likely cost, and combine the resulting predicates so a selector matches only when all required rules pass or when one selector alternative passes.
Selector token classification
Selector token classification tells compilation which rules depend on movement through the element tree or on an explicit scope. This information guides relative-selector handling and prevents context-dependent nested selectors from being treated as reusable independent tests; see Selector compilation pipeline.
Traversal recognition
Traversal recognition treats both standard css-what traversal tokens and the compiler's flexible descendant token as movement between related nodes. A traversal at the start of a selector can therefore trigger relative-selector handling, while traversal inside a nested selector can require the current candidate as its context.
hasScopePseudo also searches nested pseudo-selector data. This lets the compiler detect scope use inside selectors such as :is() or :not() instead of checking only the outer token.
References: src/…/selectors.ts• L11-17 src/…/selectors.ts• L126-137 src/compile.ts• L29-61 src/…/subselects.ts• L20-34
Predicate ordering
Predicate ordering arranges checks so cheap and selective rules run before expensive checks. The order changes execution cost without changing the meaning of a compound selector because every rule must still pass for the same element.
Quality ranking
getQuality assigns a relative score to selector rules. Simple universal and tag checks receive fixed scores. Attribute checks vary by operation, with exact ID checks favored over broader comparisons. Content and relationship pseudos rank as expensive, while nested selector pseudos use the least favorable quality of their alternatives.
sortRules applies these scores in place. It leaves traversal boundaries in the selector structure and uses insertion order to keep the normal selector path inexpensive without adding a new representation.
References: src/…/selectors.ts• L19-46 src/…/selectors.ts• L48-124 src/…/selectors.spec.ts• L23-57
Predicate combination
Predicate combination turns a sequence of selector rules into a single match function. Each rule narrows the candidate set through logical AND, while separate selector alternatives are joined through logical OR.
Rule chaining
Compilation passes the current predicate into the next selector-rule compiler. The resulting function must satisfy the new rule and the prior function. If a rule can never match, compilation can discard that selector alternative; if the chain remains an unconditional true function, compilation can return it at once.
The compiler marks a rule with quality zero as expensive. Later selector work can use that boundary to avoid unnecessary expensive subselector evaluation when an earlier rule already rejects the candidate.
Alternative joining
The final predicate represents all selector alternatives. It returns true when any alternative matches, which preserves the OR semantics of a selector list. A sibling flag travels with the predicate when a leading scope is followed by an adjacent or general sibling relation, so query execution can include the required next siblings.
The following diagram shows how rule predicates and selector alternatives form the final executable query.
flowchart LR
A[Parsed selector alternatives] --> B[Order rules by quality]
B --> C[Chain each rule with AND]
C --> D{Alternative can match?}
D -->|No| E[Discard alternative]
D -->|Yes| F[Join alternatives with OR]
F --> G[Compiled predicate]
References: src/compile.ts• L63-75 src/compile.ts• L97-152 src/compile.ts• L157-159 src/types.ts• L185-190
Nested predicate composition
Nested selector pseudos reuse the same compilation and composition rules inside a parent predicate. They add the nested result as another condition for :is(), :where(), and :not(), while :has() evaluates the nested predicate against related descendants or siblings.
The nested compiler copies options and passes context when the nested selector depends on the current element. Cache use is allowed only when the nested selector does not start with traversal and does not contain a scope pseudo. This keeps reuse from changing the result for a different candidate.
References: src/…/subselects.ts• L20-44 src/…/subselects.ts• L46-71 src/…/subselects.ts• L72-139
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the quality ranking aligned with the expensive-subselector check in src/compile.ts; a changed score can alter when nested work runs.
- Preserve the special flexible-descendant token when changing traversal detection because relative selectors and array contexts depend on it.
- Keep scope detection recursive so nested selector pseudos retain their context rules.
- Preserve
shouldTestNextSiblingswhen changing alternative combination because query traversal uses it to expand the search set. - Recheck nested-selector cache eligibility when changing scope or traversal classification.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A traversal token is not recognized | A relative or nested selector can run without the required context | isTraversal includes standard traversal tokens and the flexible descendant token |
A nested :scope is missed | A context-dependent selector can be reused for the wrong candidate | hasScopePseudo searches nested selector data |
| Selector alternatives are joined with AND | A selector list matches only elements that satisfy every alternative | compileToken joins surviving alternatives with OR |
| Sibling metadata is dropped from the compiled predicate | Queries can omit elements required by sibling combinators | shouldTestNextSiblings is set during predicate assembly and read by query execution |
Key files
| File | Why to read it |
|---|---|
| src/…/selectors.ts | Defines traversal and scope classification plus rule quality and ordering. |
| src/compile.ts | Shows how ordered rules become chained predicates and how alternatives are joined. |
| src/…/subselects.ts | Shows how nested selectors compose with parent predicates and context-sensitive cache rules. |
| src/…/selectors.spec.ts | Demonstrates the intended ordering of selector rules without changing their match meaning. |
| src/types.ts | Defines the predicate and compiled-query contracts used by composition. |