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-matching

Core selector matching

Core selector matching turns parsed CSS selector parts into predicates that test adapter-provided element trees. Each predicate checks the current element or follows the tree through a combinator, then passes successful matches to the next predicate so a full selector must satisfy every required part.

Matching model

The matching model treats an adapter as the boundary between selector rules and a DOM-like tree. The public compiler rejects non-element nodes before evaluation, while the internal matcher uses adapter operations to read names, attributes, parents, children, and siblings. This lets the same selector behavior work with different tree shapes when the adapter supplies the required view of the tree. src/index.ts and src/types.ts

The compiler processes selector alternatives separately. A selector with several alternatives matches when any compiled alternative matches; one alternative must pass all of its checks. Rule order is adjusted for faster checks without changing the required result because each rule still leads to the next predicate. src/compile.ts and src/…/selectors.ts

Element checks

Element checks apply simple tests to the candidate before the predicate continues. Tag selectors compare the adapter's element name after the configured case normalization. Universal selectors add no restriction beyond the element check, so they allow any element in the current candidate set. Attribute selectors read presence or value through the adapter and are covered in Attribute selector matching. src/general.ts and src/general.ts

The main element checks are:

Selector partMatching question
TagDoes the adapter report the requested tag name?
UniversalIs the candidate an element of any tag name?
AttributeDoes the adapter report the required attribute state or value?
Pseudo-selectorDoes the selected pseudo rule accept the candidate or its related nodes?

Tree relationships

Combinators change the candidate that the next predicate examines. A descendant check walks upward through element parents. A child check tests the direct parent. Parent and sibling checks inspect children or siblings in the other direction, so the adapter's relationship methods define the tree semantics seen by selectors. src/general.ts

Adjacent matching selects the nearest earlier element sibling. General sibling matching accepts any earlier element sibling that passes the next predicate. If the adapter provides a direct previous-sibling operation, adjacent matching uses it; otherwise it derives the same relationship from the sibling list. src/general.ts

This diagram shows how a candidate moves through a local check and then through a relationship check toward the rest of the selector.

flowchart LR
    A[Candidate element] --> B[Tag, universal, attribute, or pseudo check]
    B -->|pass| C[Combinator relationship]
    C --> D[Related element or elements]
    D --> E[Next compiled predicate]
    B -->|fail| F[No match]

For a query over a context array, a flexible descendant relation also tests the current element before its ancestors. This supports relative matching against an explicit context without changing ordinary descendant behavior. src/compile.ts and src/general.ts

Adapter boundary

The adapter supplies the facts that matching cannot infer from a generic node. It identifies elements, returns tag names and attribute values, and exposes parent, child, and sibling links. Its equality rule also matters when a matcher must find the current node inside a sibling list. src/types.ts

An adapter must keep these views consistent. For example, a node returned by getChildren must be discoverable through the related parent and sibling operations when those relationships exist. A mismatch can make a selector appear to fail even when the source tree looks correct to its owner.

Evaluation scope

Compilation can add a scope relation when a selector runs against a context. Relative selectors use that context as their starting boundary, and a context array can require later sibling nodes to be considered during selection. These rules affect which candidates reach the matcher; the matcher still evaluates each candidate through the same element and relationship predicates. src/compile.ts and src/index.ts

For the full relation rules, see General selectors and traversal. For attribute value and case rules, see Attribute selector matching.

Change checklist

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

  • Keep compileGeneralSelector relationship cases aligned with the adapter methods in Adapter; changing one side can silently alter child, parent, or sibling results.
  • Preserve the distinction between ordinary descendant matching and flexible descendant matching when changing context or relative-selector handling.
  • Keep prepareContext and shouldTestNextSiblings aligned so sibling-dependent selectors receive the candidates they need.
  • Keep tag and attribute name normalization aligned with the case rules used by their adapter lookups.

Important failure modes

TriggerConsequenceGuard
Adapter returns inconsistent parent, child, or sibling relationshipsCombinators skip valid matches or accept unrelated elementsValidate the adapter's relationship methods against the same tree model
A non-tag node reaches the unsafe compiled predicateMatching code may treat an invalid node as an elementUse compile for public matching or prove that _compileUnsafe receives elements
A context-array query omits required following siblingsAdjacent and general-sibling selectors miss candidatesLet compilation signal sibling needs and let context preparation append those siblings
Attribute names use a different case policy than selector compilationAttribute selectors fail only in some document modesKeep adapter attribute lookup consistent with XML, HTML, and lower-case options

Key files

FileWhy to read it
src/general.tsDefines the core predicate behavior for tags, universal selectors, attributes, and tree combinators.
src/compile.tsShows how selector rules become chained predicates and how context changes matching scope.
src/attributes.tsDefines attribute presence, value comparison, and case behavior used by element checks.
src/types.tsDefines the adapter and compiled-predicate contracts that connect matching to a tree.
src/index.tsShows the safe public boundary and the context preparation used before matching.
src/…/querying.tsDefines the parent and sibling views used by relationship checks and context expansion.