Selector compilation pipeline
Selector compilation turns parsed CSS selector tokens into a predicate: a function that returns whether one element matches. The pipeline preserves selector meaning while joining local tests, tree traversal, nested selectors, and selector alternatives into one executable query for the search API.
Compilation model
The compiler accepts one or more parsed selector alternatives. Each alternative is an ordered token list that describes the tests and relationships needed for one match path. The public compile entry point parses selector text when needed, normalizes options, and adds the element check expected by callers; the lower-level compiler builds the predicate from the parsed form (src/index.ts• L42-79, src/compile.ts• L63-74).
The resulting CompiledQuery is both a predicate and a small piece of query metadata. Its optional sibling flag tells the query stage that it must include following siblings in its search context for selectors that begin with sibling relationships (src/types.ts• L185-190, src/compile.ts• L97-117).
Token evaluation
Each alternative is compiled from its end toward its start. The compiler keeps a next predicate for the part already compiled, then wraps it with the current token. This lets a local test lead into a parent, child, descendant, or sibling relationship without exposing those steps to the caller (src/compile.ts• L119-142).
Before wrapping, the compiler orders tokens by their estimated cost and selectivity. Simple tests can reject an element before expensive subselectors run. A token that can never match discards its whole alternative, while a predicate that always succeeds can end compilation early (src/…/selectors.ts• L22-45, src/…/selectors.ts• L78-123, src/compile.ts• L74-76, src/compile.ts• L132-147).
The general token layer maps selector meaning to predicate behavior. Attribute and tag tokens inspect the current element. Traversal tokens inspect related nodes through the adapter. Pseudo selectors are delegated to the pseudo-selector layer, which supports direct checks, compile-time filters, aliases, and nested selector expressions (src/general.ts• L16-32, src/general.ts• L44-63, src/general.ts• L65-81, src/general.ts• L84-213).
Selector alternatives
Selector alternatives represent the comma-separated branches of a selector. The compiler builds one predicate for each viable branch, then combines the branches with logical OR. A node matches when at least one complete branch matches; an empty or impossible branch does not make the whole query fail (src/compile.ts• L98-154, src/compile.ts• L157-159).
The following diagram shows how alternatives become one predicate and how that predicate is used by evaluation.
flowchart LR
A[Parsed selector alternatives] --> B[Normalize context and relative paths]
B --> C[Order tokens in each alternative]
C --> D[Wrap predicates from right to left]
D --> E{Viable alternative}
E -->|No| F[Discard branch]
E -->|Yes| G[Combine branches with OR]
G --> H[Executable predicate]
H --> I[Query evaluator tests elements]
Context and nested selectors
Context gives a compiled selector a scope in which :scope and relative selectors are evaluated. Relative paths are made explicit before token compilation, so a selector that starts with a traversal can be evaluated from the supplied context or rejected when relative selectors are disabled (src/compile.ts• L33-60, src/compile.ts• L78-95).
Some pseudo selectors contain another selector, such as :is, :not, :has, and :where. These subselectors enter the same compilation pipeline with copied options. The copy prevents the parent query's context and final callback from leaking into the nested query (src/…/index.ts• L31-45, src/…/subselects.ts• L36-70, src/…/options.ts• L3-15).
Nested matching can inspect a candidate's descendants or use the candidate as the scope for a relative selector. The compiler therefore carries context and sibling requirements through the nested predicate. It also uses result caching only when the selector does not depend on the current context and caching remains safe for the document (src/…/subselects.ts• L20-34, src/…/subselects.ts• L72-139, src/…/cache.ts• L4-24).
Predicate composition
Predicate composition is the boundary between selector meaning and query execution. A token wrapper must call the predicate for the already compiled suffix only when its own condition passes. An alternative combiner must preserve OR semantics across independent branches. These rules allow the final function to represent both sequential selector constraints and comma-separated choices (src/compile.ts• L119-150, src/attributes.ts• L81-112, src/…/index.ts• L66-86).
See Compilation orchestration for the broader lifecycle from public query input to search, and Predicate composition for the shared rules used to wrap and combine executable matchers.
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the
shouldTestNextSiblingsmetadata aligned with context preparation in src/index.ts; otherwise sibling selectors can compile correctly but search the wrong node set. - Preserve the distinction between a predicate for one alternative and the OR predicate for all alternatives in src/compile.ts; changing one can alter empty, impossible, or always-true selector behavior.
- Keep nested selector option copies isolated from
contextandrootFunc; leakage changes the meaning of:scope,:has, and positional subselectors. - Keep token ordering compatible with the expensive-subselector signal; moving a costly token can remove a safe early rejection or change when caching is enabled.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
A relative selector is used while relativeSelector is disabled | Compilation throws instead of producing a predicate | Validate the option before accepting traversal-leading alternatives (src/compile.ts• L89-95) |
| A selector uses an unsupported pseudo-element, namespace, or column combinator | Compilation throws with an unsupported-selector error | Keep unsupported token kinds rejected by the general token layer (src/general.ts• L35-48, src/general.ts• L65-71) |
| A custom or unknown pseudo has invalid arguments | Compilation throws or returns a predicate that cannot represent the query | Resolve pseudo categories and validate argument shape before wrapping next (src/…/index.ts• L40-89) |
| Cached results are used after the document changes | A compiled query can return stale matches | Disable result caching for mutable documents (src/types.ts• L165-171, src/…/cache.ts• L18-24) |
Key files
| File | Why to read it |
|---|---|
| src/compile.ts | Shows the main pipeline: context handling, token ordering, right-to-left wrapping, and OR composition. |
| src/general.ts | Shows how token categories become element tests and tree relationships. |
| src/…/index.ts | Shows how pseudo selectors choose between direct checks, filters, aliases, and nested selectors. |
| src/…/subselects.ts | Shows how nested selectors such as :is, :not, and :has re-enter compilation. |
| src/…/selectors.ts | Defines traversal detection and the cost order used before compilation. |
| src/types.ts | Defines the parsed-token boundary, executable predicate shape, and sibling metadata. |