Type contracts
Type contracts define how a typed node tree connects to the selector engine. They describe which values can be queried, how the engine reads nodes, and how selector results become safe boolean functions. These contracts let the engine work with a DOM-like tree without owning that tree.
Contract model
The engine separates node storage from selector matching. A caller supplies a broad Node type for every tree value and an ElementNode type for values that can match a selector. The adapter turns those types into the structural and state data that matching needs.
The parent page, Selector API, describes the calls that consume these contracts. The Selection and matching API explains how a query uses them to return matches, while Selector options covers the settings that change matching behavior.
Node and element types
The type layer in src/types.ts uses two related generics: Node represents every value in the tree, and ElementNode extends Node represents tag-like values that can satisfy a selector. This split keeps text, document, and other non-element values in traversal while preventing element-only operations from receiving them.
Adapter.isTag is the type guard between the two types. Public compilation checks a candidate through this guard before it runs element matching, and query traversal uses the same distinction when it descends through a tree.
References: src/types.ts• L16-20 src/index.ts• L42-63 src/…/querying.ts• L11-18
Predicate and compiled query values
Predicate<Value> is the basic matching contract: it accepts one value and returns boolean. A CompiledQuery<ElementNode> is an element predicate with optional query metadata, so the same value can be used for direct matching or by a selection operation.
The public Query<ElementNode> union accepts a selector string, a compiled query, or parsed selector tokens. This lets callers choose readable selector input, reuse a compiled matcher, or pass parser output while keeping the result tied to the element type.
References: src/types.ts• L9-11 src/types.ts• L185-199 src/index.ts• L82-109
Selector compilation path
Selector compilation turns selector text or parsed tokens into a predicate that the engine can apply to element nodes. The public path adds the element check; the unsafe path is for code that already guarantees an element and returns the narrower compiled contract.
The returned query may carry shouldTestNextSiblings. Selection uses this marker to add following siblings to the candidate set when a selector can depend on them. A caller that supplies its own compiled query must treat the marker as part of the candidate-discovery contract rather than as a result value.
This diagram shows how typed input moves through the contract boundary to matching and selection.
flowchart LR
A[Node tree] --> B[Adapter]
C[Selector string or tokens] --> D[Compiled query]
D --> E[Element predicate]
B --> E
E --> F[selectAll or selectOne]
E --> G[is]
References: src/index.ts• L42-79 src/index.ts• L104-109 src/index.ts• L119-135
Adapter contract
The adapter is the integration boundary for a custom node tree. It supplies the engine with enough structure to move through the tree, inspect element names and attributes, compare nodes, and evaluate a small set of state-based selectors.
Structural access
An adapter must identify elements and provide their children, name, parent, siblings, text, and attributes. These operations support both candidate traversal and selector relations such as descendant, child, adjacent-sibling, and general-sibling matching.
The getSiblings result must include the current node. The engine uses that shared list to locate a node and to inspect nodes before it. removeSubsets must also remove duplicates and nodes whose ancestors are already present, because selection treats its input as a non-redundant search scope.
| Adapter operation | Contract purpose |
|---|---|
isTag | Narrows a Node to ElementNode. |
getChildren and getParent | Move through the tree. |
getSiblings | Supplies the current node and its sibling order. |
getName | Supplies the element name for tag matching. |
getAttributeValue and hasAttrib | Supply attribute selector data. |
getText | Supplies text for text-based selectors. |
removeSubsets | Normalizes overlapping query roots. |
References: src/types.ts• L16-70 src/general.ts• L84-203 src/…/querying.ts• L121-150
Identity and state hooks
equals is an optional identity rule for node systems where two objects can represent the same underlying node. Without it, the engine uses strict identity. This rule matters when sibling relations compare a candidate with nodes returned by the adapter.
The optional prevElementSibling, isHovered, isVisited, and isActive operations extend the adapter for faster sibling lookup or state-based pseudo-classes. A missing previous-sibling operation does not remove adjacent-sibling matching; the engine can use the full sibling list instead. Missing state hooks mean that the related state cannot be supplied by the custom tree.
References: src/types.ts• L46-49 src/types.ts• L73-94 src/general.ts• L164-203 src/…/pseudos.ts• L18-115
Selector and option contracts
Selector and option types describe the inputs that shape a query without exposing the tree itself. The selector value chooses what to match, while options supply the adapter, scope, case rules, cache policy, and custom pseudo behavior.
Selector inputs
The engine accepts selector text or parsed css-what token groups. It also accepts a compiled element query when the caller needs to reuse matching work. Parsed token groups are the structured form of a selector and must remain compatible with the parser version used by the engine.
Selector tokens describe element tests and relationships. The adapter remains responsible for supplying the data behind those tests; selector input does not replace or relax the adapter contract.
References: src/types.ts• L1-6 src/types.ts• L193-199 src/index.ts• L51-79
Query options
Options<Node, ElementNode> controls how the engine interprets selectors and where it searches. Its adapter connects the query to a node tree, context limits scope, and relativeSelector controls how relative selectors are handled. XML and quirks settings control name and attribute case behavior, while cacheResults controls reuse for some compiled work.
Options also support rootFunc as a final element test and pseudos as caller-defined pseudo-classes. The engine fills in the default adapter and node equality rule when the caller omits them. The full option behavior is described in Selector options, so this page treats options as the contract that carries those choices into compilation and selection.
References: src/types.ts• L97-171 src/index.ts• L20-39 src/compile.ts• L78-95
Custom pseudo predicates
The pseudos option maps a pseudo-class name to either a selector string or a function. A string defines a selector-based condition. A function receives an ElementNode and an optional string argument and must return true only when the element satisfies the condition.
The engine combines a custom predicate with the rest of the selector. The predicate must therefore be safe for every ElementNode accepted by the adapter and must not assume a narrower node shape unless the adapter guarantees that shape.
References: src/types.ts• L123-136 src/…/index.ts• L40-89
Contract boundaries
Safe integration depends on keeping each type boundary honest. The adapter owns node facts, the selector owns matching intent, and the predicate reports only whether one element satisfies that intent.
Public and internal contracts
Options, Query, Predicate, and CompiledQuery describe values that cross the public query boundary. InternalOptions adds the normalized adapter and equality function that compilation can rely on. InternalSelector and CompileToken support nested compilation inside the engine and are not substitutes for the public query contract.
This split prevents incomplete options or non-element values from reaching code that assumes normalized integration data. It also keeps nested selector work from inheriting unrelated query scope or root tests.
References: src/types.ts• L174-206 src/index.ts• L26-39 src/…/options.ts• L1-16
Change checklist
When these files or sections are changed, remember to consider these:
- If
Adapterchanges, check every traversal relation and attribute or text lookup that consumes the affected operation. - If
ElementNodeorCompiledQuerychanges, check the publiccompile,selectAll,selectOne, andissignatures together. - If sibling handling changes, preserve the
getSiblingsinclusion rule and theshouldTestNextSiblingscandidate-expansion contract. - If
Querychanges, keep string, parsed-token, and compiled-query inputs aligned with the parser and compiler boundary. - If custom pseudo arguments change, update both the option type and pseudo argument validation.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
isTag returns false for an element-like value | The value is skipped or fails public compiled matching. | Make the type guard agree with the tree's element model. |
getSiblings omits the current node | Adjacent and general sibling matching can stop at the wrong position. | Include the current node in the returned sibling list. |
removeSubsets keeps overlapping roots | A query can inspect redundant or out-of-scope descendants. | Remove duplicates and descendants of selected roots. |
| A custom matcher needs following siblings but has no sibling metadata | Selection may not add those siblings to its candidate set. | Preserve or set shouldTestNextSiblings for sibling-dependent compiled queries. |
| A custom pseudo returns a non-boolean value or assumes non-element input | The selector result can be incorrect or the predicate can fail at runtime. | Return a boolean and rely only on the ElementNode contract. |
Key files
| File | Why to read it |
|---|---|
| src/types.ts | Defines the node, adapter, predicate, query, option, and compiled-query contracts. |
| src/index.ts | Shows how public calls normalize options and connect predicates to selection. |
| src/compile.ts | Shows how selector tokens become compiled queries and sibling metadata. |
| src/…/querying.ts | Shows how predicates consume adapter traversal and produce matches. |
| src/general.ts | Shows which adapter operations support selector relations. |
| src/…/index.ts | Shows the contract for built-in and caller-defined pseudo-classes. |