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 / selection-and-matching-api

Selection and matching operations

The selection and matching API turns CSS selector input into either a set of matching elements, one matching element, or a yes/no result for one element. Callers choose selectAll for collection queries, selectOne for the first result, is for a direct match test, and compile when they need to reuse a selector as a predicate. See the Selector API and integration contract for the wider public boundary, Type contracts for adapter and query types, and Selector configuration options for evaluation settings.

Operation selection

The public operations share the same selector input model and DOM adapter boundary. The main difference is the shape of the result and whether the caller wants to prepare a reusable query.

Caller needEntry pointResult
Find every matching descendant of one or more rootsselectAllAn array of elements, possibly empty
Find the first matching descendantselectOneOne element or null
Test one element directlyisA boolean
Reuse matching logic across callscompileA callable compiled query

Query collection operations

selectAll and selectOne search the descendants of a node or the supplied set of nodes. A supplied element acts as a search root whose children are examined. The operations accept either a selector string, parsed selector data, or an already compiled query, so callers can use the simple path for one query or share a prepared predicate across many queries. References: src/index.ts• L82-110, src/index.ts• L152-198

selectAll returns all matching elements in traversal order. selectOne stops at the first match and returns null when no match exists. Both operations use the same context preparation, adapter, and selector semantics, which makes the choice depend on result size rather than matching rules. References: src/index.ts• L161-198, src/…/querying.ts• L11-118

Direct match operation

is tests one element against a selector or compiled query without searching its descendants. Use it when the caller already has a candidate element and needs to know whether that candidate satisfies the selector. References: src/index.ts• L200-219

The operation compiles a selector string when needed and then invokes the resulting predicate on the element. A compiled query can therefore be passed to is when the same rule is applied to many candidates. References: src/index.ts• L51-63, src/index.ts• L211-219

Selector preparation

compile prepares selector input as a callable query. It is the right entry point when matching will happen more than once, when a caller needs to pass the query into selectAll, selectOne, or is, or when a caller needs selector context at compile time. References: src/index.ts• L42-63, src/types.ts• L184-199

The compiled query reports whether selection may need to inspect following siblings. The collection operations use that signal while preparing their search roots. This keeps a reusable selector compatible with sibling-dependent matching without making every query include extra roots. References: src/types.ts• L184-190, src/index.ts• L89-110, src/index.ts• L119-149

Search scope

Search scope defines which nodes can become candidates and how selector context affects that set. The API accepts one node or an array of nodes, then normalizes that input before traversal.

Root preparation

The search operations remove overlapping roots before they search. If an array contains a node and one of its descendants, the broader root represents the same search area and prevents duplicate work. A single root contributes its children as the initial candidates rather than matching the root itself. References: src/index.ts• L104-109, src/index.ts• L119-135, src/types.ts• L66-70

When a compiled query needs following siblings, root preparation adds those siblings before it removes overlapping roots. The result preserves the search order expected by sibling-aware selectors while keeping the caller's input unchanged. References: src/index.ts• L119-149, src/…/querying.ts• L121-138

Context-bound matching

A query context limits where selector matching can resolve scope-sensitive relationships. Callers can provide context through the public options or the compile call, then use the resulting query with the matching operations. Relative selector behavior and :scope rules are part of this contract rather than separate query entry points. References: src/index.ts• L51-79, src/types.ts• L143-164

The overall selection flow moves from caller input to a normalized search area, then to a selector predicate, and finally to the requested result shape. This diagram shows the shared path and the point where the operations diverge.

flowchart LR
    A[Selector input] --> B{Prepare or use query}
    B -->|compile| C[Compiled query]
    B -->|string or parsed selector| C
    C --> D[Normalize roots and scope]
    D --> E{Operation}
    E -->|selectAll| F[All matching elements]
    E -->|selectOne| G[First match or null]
    E -->|is| H[Boolean for one element]

Query inputs and results

The API keeps input forms aligned across operations so that callers can move from an inline selector to a compiled query without changing the surrounding flow. Result types remain distinct and signal how much work the caller expects from the operation.

Accepted query forms

Each selection operation accepts a CSS selector string, parsed selector data, or a compiled query function. Strings are useful for one-off calls. Parsed data and compiled functions are useful when another part of the system has already parsed or prepared the selector. References: src/types.ts• L184-199, src/index.ts• L51-79, src/index.ts• L161-219

The public options supply the adapter and matching behavior that all entry points share. A caller using a custom node model must provide the matching adapter contract described in Type contracts; callers changing case, context, relative selector, or cache behavior should use Selector configuration options. References: src/types.ts• L96-172, src/index.ts• L26-40

Result contracts

The result shape tells the caller how to handle absence and how to control later work:

  • selectAll always returns an array. No match produces an empty array.
  • selectOne returns the first element or null.
  • is returns true or false for the candidate passed to it.
  • compile returns a function that accepts an element and returns a boolean.

These contracts let callers avoid treating a collection query as a direct test or adding a second search after selectOne. References: src/index.ts• L161-219, src/types.ts• L184-199

Change checklist

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

  • Keep selectAll, selectOne, and is aligned when query input or option normalization changes.
  • Preserve the compiled-query sibling signal when changing compilation or root preparation.
  • Keep root de-duplication and child-root behavior aligned with the adapter's removeSubsets contract.
  • Check context and relative-selector behavior when changing how compiled queries are passed into search operations.

Important failure modes

TriggerConsequenceGuard
A custom adapter does not remove duplicate or nested rootsA query can repeat work or return duplicate matchesImplement the removeSubsets adapter contract
A reusable compiled query is used with a changed DOM while result caching remains enabledThe query can return stale match resultsDisable result caching for mutable documents; see Selector configuration options
A sibling-dependent query is evaluated without its following siblings in the search rootsMatches that depend on later siblings can be missedUse the public selection operations so they can prepare the context from the compiled query
A node is passed to is that is not an element for the active adapterThe direct match result is false rather than a descendant searchUse selectAll or selectOne when the caller needs to search below a node

Key files

FileWhy to read it
src/index.tsDefines the public selection, matching, compilation, and context-preparation operations.
src/types.tsDefines query, compiled-query, adapter, and option contracts used by every entry point.
src/…/querying.tsShows the shared descendant search behavior and first-match behavior.
src/…/selectors.tsShows selector traversal classification and the quality checks used while preparing selector rules.