Selector configuration options
Selector options define how the engine reads a node tree, compares names and attributes, limits matching to a context, handles relative selectors, and reuses results. The public options are normalized before compilation so the same rules apply to compile, selectAll, selectOne, and is; see Selector API for the entry points and Selection and matching API for query behavior.
Engine configuration
Engine configuration sets the name and attribute rules that apply while a selector is compiled. The default mode is HTML-like: tag and attribute names are treated as lower case where the selector rules require it. XML mode preserves case unless the related lower-case option is enabled.
The main configuration options are:
| Option | Purpose | Default or effect |
|---|---|---|
xmlMode | Use case-sensitive tag behavior and XML-style matching rules. | false |
lowerCaseAttributeNames | Lower-case attribute names before lookup. | The inverse of xmlMode |
lowerCaseTags | Lower-case tag names before comparison. | The inverse of xmlMode |
quirksMode | Apply case-insensitive matching for .className and #id in quirks mode. | false |
pseudos | Add or replace pseudo-classes with a selector string or a predicate function. | No custom pseudo-classes |
rootFunc | Supply the final match condition in the compiled predicate chain. | Always returns true |
These settings are part of the matching contract. For example, changing xmlMode changes both tag-name handling and the default case behavior for HTML-defined attribute comparisons. Custom pseudo functions receive the matched element and an optional pseudo value; custom string pseudos are parsed as selectors under the active options.
References: src/types.ts• L96-141 src/compile.ts• L78-95 src/general.ts• L44-80 src/attributes.ts• L17-79 src/…/index.ts• L48-70
Adapter contract
The adapter connects the selector engine to the node tree. It supplies the basic operations needed to identify elements, read attributes and names, move through parents, children, and siblings, read text, and remove redundant query roots. The default adapter targets the domhandler tree through domutils; a custom adapter lets the same selector engine work with another tree model. See Type contracts for the TypeScript shape of this boundary.
An adapter must provide these core capabilities:
- Identify whether a node is an element.
- Read an element's attributes and tag name.
- Read children, the parent, and all siblings.
- Read text content.
- Check for an attribute.
- Remove duplicate nodes and nodes covered by another selected ancestor.
It may also provide equals when two different objects can represent the same node. If it does not, the engine uses reference equality. The optional prevElementSibling, isHovered, isVisited, and isActive methods improve sibling lookup or enable the matching of dynamic state pseudos. Without a dynamic-state method, the related pseudo matches nothing.
References: src/types.ts• L13-94 src/index.ts• L20-39 src/general.ts• L151-203 src/…/filters.ts• L208-231
Context handling
Context limits where a selector starts and gives :scope a concrete node or node set to match. A context can be one node or an array of nodes. Query functions also normalize their input roots: an element root is replaced by its children, while an array is reduced by the adapter so nested roots and duplicates do not cause repeated work.
The context affects relative selector behavior as follows:
- A selector that starts with a traversal is treated as relative when
relativeSelectoris enabled. - A relative selector without
:scopereceives an implicit scope followed by a descendant relation when the context supports it. - An explicit
:scopematches the context node through the adapter's equality rule. - A context array lets
:scopematch any supplied context node.
The compiled predicate records when a selector needs following siblings. Query setup then adds those siblings to the candidate roots before the search. This allows selectors such as :scope + sibling and :scope ~ sibling to see nodes outside the direct children of the supplied root without changing the selector's scope.
flowchart TD
A[Query roots and options] --> B[Normalize adapter and context]
B --> C{Relative selector enabled?}
C -->|Yes| D[Apply implicit or explicit scope]
C -->|No| E{Selector starts with traversal?}
E -->|Yes| F[Reject selector]
E -->|No| G[Compile selector]
D --> G
G --> H{Following siblings needed?}
H -->|Yes| I[Add next siblings to candidates]
H -->|No| J[Use normalized candidates]
I --> K[Evaluate matches]
J --> K
When relativeSelector is false, a selector that starts with a traversal is rejected. This setting is useful when callers need a selector to test parent relationships outside the supplied context rather than treating the context as the selector's starting scope.
References: src/types.ts• L147-164 src/index.ts• L82-135 src/index.ts• L137-149 src/compile.ts• L33-61 src/compile.ts• L78-117 src/…/filters.ts• L144-163
Option normalization
Public options are converted to an internal form before the engine compiles or runs a query. The normalized form always has an adapter and an equality function, which keeps selector evaluation independent of missing optional public fields.
Normalization follows this order:
- Use the caller's options when present; otherwise use the default adapter.
- Use the caller's adapter when present; otherwise use the default adapter.
- Use the adapter's
equalsmethod when present; otherwise compare nodes with===. - Pass the normalized options into compilation and search.
Nested selectors such as :is, :not, :has, and :nth-child(... of S) receive a copied option set. The copy keeps matching configuration and the adapter, but omits context and rootFunc so state from the outer compilation does not leak into the nested selector. A nested selector can then receive the context that its pseudo-class needs.
References: src/index.ts• L20-39 src/index.ts• L51-79 src/…/options.ts• L3-15 src/…/subselects.ts• L36-70 src/…/subselects.ts• L72-139 src/…/filters.ts• L57-66
Result caching
cacheResults controls caches used for expensive parent and descendant checks during selector evaluation. It is enabled by default and can reduce repeated work when the same compiled selector examines stable nodes.
Disable it when the backing tree can change between uses of one compiled selector. A cached result can otherwise describe an earlier tree state. Context-dependent nested selectors are excluded from some caches because their result depends on the current element or on :scope.
References: src/types.ts• L165-171 src/general.ts• L84-132 src/…/cache.ts• L1-62 src/…/subselects.ts• L20-34 src/…/subselects.ts• L88-139
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the public
Optionsfields and the normalization rules aligned so every query entry point gets the same defaults. - Keep adapter equality behavior aligned with
:scope, sibling checks, and positional pseudo-classes. - Keep context changes aligned with relative-selector absolutization and the sibling-candidate flag.
- Keep nested-selector option copying aligned with the context rules for
:is,:not,:has, andof Sselectors. - Keep cache invalidation guidance aligned with every new selector path that can read mutable tree state.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A custom adapter omits a required tree operation or returns inconsistent siblings. | Traversal, position, or attribute selectors can return wrong matches or fail during evaluation. | Implement the full Adapter contract and verify node identity with equals when references are not stable. |
A mutable tree is queried again with a compiled selector while cacheResults remains enabled. | Parent or descendant results can reflect the old tree. | Set cacheResults: false for selectors used across tree changes. |
A selector starts with a traversal while relativeSelector is false. | Compilation throws instead of evaluating the selector. | Enable relative selectors or rewrite the selector with an explicit supported scope. |
| A dynamic-state pseudo is used with an adapter that lacks its state method. | :hover, :visited, or :active matches no elements. | Provide the matching optional adapter method when that state is available. |
| HTML-like case rules are used for an XML tree. | Tag or attribute comparisons can match nodes with the wrong case. | Set xmlMode and the lower-case options to match the tree's name rules. |
Key files
| File | Why to read it |
|---|---|
| src/types.ts | Defines the public options, adapter contract, normalized options, and context types. |
| src/index.ts | Shows default option creation, option normalization, context preparation, and public query entry points. |
| src/compile.ts | Explains how context and relative-selector settings shape compiled queries. |
| src/…/options.ts | Shows how nested selectors receive isolated compilation options. |
| src/general.ts | Shows how adapter access, equality, case settings, and result caching affect matching. |
| src/…/filters.ts | Shows context matching and optional adapter methods for dynamic state. |