Introducing a new way to review your PRs.

Read the announcement

fb55 / css-select

a CSS selector compiler & engine

View on GitHub

17 documents · Updated Oct 26, 2026

docs / selector-api

Selector API and integration contract

css-select is a selector engine that turns CSS selector input into element matches over a caller-owned node tree. Its public contract has three parts: selection and matching operations, reusable compiled predicates, and an adapter that gives the engine a safe view of the caller's DOM.

Public API surface

The package exposes one default selection entry point and named operations for selecting many elements, selecting one element, testing one element, compiling a selector, and preparing a query context. Callers choose an operation based on whether they need a result set, a single result, or a reusable match test.

Selection and matching operations

selectAll, selectOne, and is accept either selector text, parsed selector tokens, or a compiled query function. selectAll returns matching elements, selectOne returns the first match or null, and is returns a boolean for one element. The detailed choice between these operations is described in Selection and matching operations.

References: src/index.ts• L152-219 src/types.ts• L185-199

Selector compilation

compile converts selector input into a function that accepts an element and returns whether it matches. The function can be reused with is, selectAll, or selectOne, but its results are tied to the options and selector context used at compile time.

The lower-level _compileUnsafe entry point returns a predicate without the public element-tag check. It is useful at the engine boundary, but callers that receive mixed node values should use compile so non-element nodes do not enter element matching.

References: src/index.ts• L42-80 src/types.ts• L185-199

Accepted selector inputs

The API accepts three query forms:

  • A CSS selector string, such as "article > p".
  • Parsed css-what selector tokens in the Selector[][] shape.
  • A compiled query function that returns a boolean for an element.

The string and token forms are compiled before selection. A compiled function skips that step and lets several operations share the same matching rule.

References: src/index.ts• L51-79 src/index.ts• L82-109 src/types.ts• L187-199

Query context

The query context defines the part of a node tree that the engine may inspect. A single node supplies its children as the search area. An array supplies candidate roots after the adapter removes duplicates and nodes already covered by another supplied root.

Relative selector scope

The context also gives meaning to relative selectors and :scope. By default, a relative selector is interpreted from the supplied context. Callers can disable that behavior with relativeSelector: false; traversal-first selectors then fail instead of being treated as relative queries.

When a selector can depend on following siblings, the engine expands the search context to include those siblings before it searches. This preserves sibling matching without changing the caller's node tree.

References: src/index.ts• L104-149 src/compile.ts• L33-61 src/compile.ts• L83-117 src/types.ts• L147-164

Query flow

This flow shows how selector input crosses the public boundary and becomes a result or match decision.

flowchart LR
    A[Selector text, parsed tokens, or compiled query] --> B[Public operation]
    C[Node or node array] --> B
    D[Options and DOM adapter] --> B
    B --> E{Query form}
    E -->|Text or tokens| F[Compile predicate]
    E -->|Compiled function| G[Reuse predicate]
    F --> H[Normalize context]
    G --> H
    H --> I[Traverse adapter-backed tree]
    I --> J[Elements or first element]
    I --> K[Boolean match]

References: src/index.ts• L82-109 src/index.ts• L161-219 src/…/querying.ts• L11-118

DOM adapter boundary

The adapter is the integration boundary between the selector engine and a node model. It supplies the operations needed to identify elements, inspect names and attributes, move through parents and siblings, read text, and normalize result roots. The engine does not require callers to use the domhandler tree when they provide an adapter that follows this contract.

Adapter responsibilities

An adapter must support the core tree and element operations. It may also provide custom node equality and state checks for selectors that need them.

Adapter areaRole in selector evaluation
Element identityDistinguishes element nodes from documents, text, and other nodes.
Tree navigationProvides children, parents, and siblings for combinators and context.
Element dataProvides tag names, attributes, and text content.
Result normalizationRemoves duplicate roots and roots hidden by another supplied root.
Optional identity and stateSupports custom equality and state-based pseudo-selectors.

The default integration uses domutils with domhandler tag detection. A custom integration must keep the same meaning for these operations or selector results can change across DOM models.

References: src/types.ts• L14-94 src/index.ts• L20-24

Element and node types

The API separates Node from ElementNode. Node is the full set of values that can occur in the tree. ElementNode is the subset that can match a selector. This distinction lets selection start from document or fragment roots while compiled predicates remain element-focused.

The public compile function checks that its input is an element before it runs the predicate. Selection also ignores non-element nodes while it walks the context.

References: src/types.ts• L16-20 src/types.ts• L187-199 src/index.ts• L56-63 src/…/querying.ts• L38-53

Configuration boundary

Options control how the engine interprets the selector and how it reaches the node tree. They define document mode, case rules, custom pseudo behavior, query scope, caching, and the adapter itself. The Selector configuration options page describes these controls and their default behavior.

Option normalization

Each public operation normalizes options before it compiles or searches. If no adapter is supplied, the default DOM adapter is used. If no equality function is supplied, node identity uses === unless the adapter provides its own equality rule.

Options are part of the compiled predicate's contract. Reuse a compiled function only when its adapter, case rules, context, custom pseudos, and document assumptions still apply.

References: src/index.ts• L20-39 src/index.ts• L94-109 src/types.ts• L96-182

The public API is easiest to use when each concern has one clear reference point. Use these pages for the contracts that sit below this overview:

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the accepted Query union aligned with every public operation and its compiled-function path.
  • Keep the adapter requirements aligned with context normalization, traversal, sibling checks, and result de-duplication.
  • Recheck the compile versus _compileUnsafe element-safety boundary when predicate types or entry points change.
  • Recheck relative-selector and sibling-context behavior when context or traversal rules change.
  • Recheck default option normalization when adapter equality, cache behavior, or document mode changes.

Important failure modes

TriggerConsequenceGuard
An adapter returns incomplete or inconsistent parent, child, or sibling dataCombinators and scoped selectors return wrong results or fail during evaluationValidate the adapter against the full type contract
A changing tree is queried with cached results enabledA reused compiled query can return stale matchesDisable cacheResults for mutable trees between matching calls
A traversal-first selector is used with relativeSelector: falseCompilation throws instead of treating the selector as relativeEnable relative selectors or provide an explicit :scope context
A pseudo-element, column combinator, or unsupported namespace is compiledCompilation throws because the selector is outside the engine contractRestrict input to supported selector forms before compilation
HTML and XML assumptions are mixedTag, attribute, template, or case matching differs from caller expectationsSet xmlMode and case options to match the source tree

Key files

FileWhy to read it
src/index.tsDefines the public operations, query dispatch, context preparation, and default adapter boundary.
src/types.tsDefines the adapter, options, compiled query, and accepted query contracts.
src/compile.tsShows how context and relative selectors affect a compiled predicate.
src/…/querying.tsShows how node roots become traversable search areas and results.
src/general.tsShows the adapter calls needed while a predicate evaluates selector relationships.