Pseudo-selector evaluation
Pseudo-selector evaluation turns a parsed CSS pseudo-selector into part of an executable element query. The system supports built-in aliases, direct element checks, selector filters, and pseudos that compile nested selectors. It also lets a query replace built-in names with user-defined string or function pseudos.
Evaluation model
Pseudo-selector categories
Each pseudo-selector belongs to one of three evaluation forms. A filter builds a matcher during selector compilation. A direct pseudo checks the current element when the query runs. A subselect compiles another selector and uses its result as part of the outer match. Filter and structural pseudos describes the first two forms in more detail. Nested selector pseudos describes the third form.
| Form | Main purpose | Result |
|---|---|---|
| Alias | Express a named pseudo as a selector | The alias selector is parsed and matched as a nested selector |
| User pseudo | Add or replace a pseudo for one query | A string becomes a selector; a function becomes an element predicate |
| Filter | Prepare a matcher with a value or rule | A compiled matcher continues the outer query when it succeeds |
| Direct pseudo | Test the current element and its nearby structure | A boolean check gates the outer query |
| Subselect | Evaluate one or more nested selectors | The nested result gates or searches from the current element |
References: src/…/index.ts• L4-14 src/types.ts• L123-136
Dispatch order
The dispatcher receives the parsed pseudo name, its optional data, the query options, and the next matcher in the selector chain. It chooses the evaluation form before returning a matcher that preserves the rest of the selector.
When the pseudo has selector data, the dispatcher sends it to the nested-selector registry. Otherwise it checks a query-local user pseudo first. A user string or built-in alias is parsed as a selector. A user function becomes a direct predicate. If neither applies, the dispatcher checks built-in filters and then direct pseudos. An unknown name or invalid argument count raises an error.
The following flow shows the main dispatch path.
flowchart TD
A[Parsed pseudo] --> B{Selector data?}
B -- Yes --> C[Nested-selector registry]
B -- No --> D{User pseudo?}
D -- String --> E[Parse and match selector]
D -- Function --> F[Run user predicate]
D -- No --> G{Built-in alias?}
G -- Yes --> E
G -- No --> H{Built-in filter?}
H -- Yes --> I[Build filter matcher]
H -- No --> J{Direct pseudo?}
J -- Yes --> K[Run element check]
J -- No --> L[Throw unknown pseudo error]
C --> M[Continue outer matcher]
E --> M
F --> M
I --> M
K --> M
References: src/…/index.ts• L23-89 src/general.ts• L25-63
Alias registration
Selector aliases
Aliases give common pseudo names a selector meaning instead of a separate element-check routine. The alias is parsed when it is used, so the active parser and query options apply to the expanded selector. This lets aliases reuse nested-selector behavior such as matching, negation, and structural conditions.
The built-in aliases cover link state, form state, common input types, element groups, and several jQuery-style names. Query options can replace an alias with a user string under the same pseudo name. Arguments are rejected for aliases because an alias already defines its complete selector.
References: src/…/aliases.ts• L11-63 src/…/index.ts• L48-64
Element and structural evaluation
Filter and direct-pseudo registration
Filters and direct pseudos use separate registries because they have different roles in the query lifecycle. Filters receive the next matcher while the selector is compiled. Direct pseudos receive the candidate element while the compiled query runs. Both must pass before the outer selector can match.
Filters cover value-based checks, position rules, scope and root behavior, language inheritance, and optional dynamic element state. Direct pseudos cover empty elements and checks based on element siblings or element type. These checks use the adapter as the source of tree structure and node state. See Filter and structural pseudos for the supported behavior.
References: src/…/filters.ts• L38-44 src/…/filters.ts• L112-136 src/…/filters.ts• L138-231 src/…/pseudos.ts• L18-115
Argument validation
The dispatcher validates whether a pseudo accepts an argument before it creates the matcher. A missing required argument and an unexpected argument produce distinct errors. This keeps malformed selectors from reaching the element or sibling checks.
References: src/…/pseudos.ts• L117-136 src/…/index.ts• L66-86
Nested selector evaluation
Subselect registration
Subselects are pseudos whose value is one or more parsed selectors. The built-in set includes :is, :matches, :where, :not, and :has. The first three share matching behavior, while :not reverses the nested result and :has searches from the current element.
Nested selectors compile with copied options so the inner selector does not inherit the parent query's context or final root check. The :has form enables relative selectors and uses the current element as the search boundary when the nested selector refers to it. Context-dependent nested selectors avoid result caching when their match can change with the current element.
The diagram shows how a nested selector becomes a matcher and how :has changes the search from a candidate element to its descendants.
flowchart LR
A[Outer candidate] --> B[Compile nested selector]
B --> C{Subselect kind}
C -- is, matches, where --> D[Nested match on candidate]
C -- not --> E[Negate nested match]
C -- has --> F[Search candidate's descendants]
D --> G[Gate outer matcher]
E --> G
F --> G
References: src/…/subselects.ts• L20-44 src/…/subselects.ts• L46-139 src/…/options.ts• L3-15
Structural subselects
The of form of an nth pseudo also compiles a nested selector. It first limits which element siblings count, then applies the positional rule from the start or end of the sibling list. This connects structural position checks to the same nested compilation path without making the outer selector lose its context.
References: src/…/filters.ts• L34-109
Query integration
Matcher composition
The general selector compiler composes pseudo matchers with tag, attribute, and traversal matchers. Each pseudo receives the matcher for the rules that follow it and must pass its own condition before that matcher runs. The compiled query therefore keeps the order and meaning of the full selector while allowing the compiler to place cheaper rules before expensive subselectors.
The public compile path parses a selector, supplies the adapter and query options, and returns a predicate for element nodes. Search functions then apply that predicate to the selected context. A compiled query may also signal that following siblings must be included when a nested or sibling-dependent rule needs them.
References: src/compile.ts• L63-154 src/general.ts• L25-64 src/index.ts• L42-79 src/index.ts• L82-109
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the dispatch order aligned with the supported user override rules. A new built-in name can change whether a query uses an alias, filter, or direct pseudo.
- Keep selector-valued aliases valid for the parser and compatible with nested-selector dispatch. An alias can depend on other aliases or subselects.
- Keep argument arity checks aligned with each registered function. A change from a simple pseudo to a parameterized pseudo changes the public selector contract.
- Keep
:hasand:nth-child(... of S)aligned with context copying and relative-selector handling. These paths can silently change which nodes a nested selector can see. - Keep adapter requirements aligned with registered filters and direct pseudos. Dynamic state and sibling checks depend on optional or structural adapter methods.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A selector uses an unknown pseudo name | Compilation throws an unknown pseudo-class error | Register the name as a user pseudo or add it to the correct built-in registry |
| A selector passes data to an alias or no data to a required function | Compilation throws an argument error | Match the selector form to the registered pseudo's argument contract |
| A dynamic-state pseudo uses an adapter without its state method | The pseudo matches no elements | Provide the required adapter method when using that state |
| A relative nested selector runs without its intended context | :has or :scope can inspect the wrong search boundary | Preserve relative-selector and context handling when changing nested compilation |
| A context-dependent nested selector is cached as if it were independent | Results can be reused for the wrong candidate element | Keep context detection and cache bypass rules aligned |
Key files
| File | Why to read it |
|---|---|
| src/…/index.ts | Defines the registration groups and the dispatch order |
| src/…/aliases.ts | Shows how named pseudos map to selector expressions |
| src/…/filters.ts | Defines value, position, scope, language, and dynamic-state filters |
| src/…/pseudos.ts | Defines direct element and sibling predicates and argument checks |
| src/…/subselects.ts | Defines nested selector behavior for matching, negation, and descendant search |
| src/compile.ts | Shows how pseudo matchers join the full selector query |
| src/types.ts | Defines user pseudo options and the adapter contract |