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 / general-selectors-and-traversal

General selectors and traversal

General selectors and traversal are the basic matching rules used by the selector engine. They identify an element by its tag name or by any tag at all, then follow tree relationships so that combinators can test parents, ancestors, children, or siblings.

Matching model

The matcher evaluates each candidate element through a chain of checks. A simple selector checks the candidate, while a combinator moves to a related element and applies the remaining checks there. The adapter supplies the tree view, so the same rules work with different DOM-like data models.

The main relationships are:

RelationshipMatching directionMeaning
DescendantCandidate to parent and higher ancestorsSome ancestor matches the remaining selector.
ChildCandidate to its parentThe direct parent matches the remaining selector.
ParentCandidate to its childrenAt least one direct child matches the remaining selector.
SiblingCandidate to earlier siblingsA prior sibling matches the remaining selector.
Adjacent siblingCandidate to the immediately previous element siblingThe previous element sibling matches the remaining selector.

Predicate flow

Each relationship returns success only when the related element passes the next part of the compiled selector. This keeps the local test and the tree walk as one match operation.

The following diagram shows how a candidate is tested by a local selector and then by a relationship check.

flowchart LR
    A[Candidate element] --> B[Tag or universal check]
    B --> C{Combinator present?}
    C -->|No| D[Final predicate result]
    C -->|Yes| E[Find related element or elements]
    E --> F[Apply remaining selector]
    F --> D

References: src/general.ts• L25-63 src/compile.ts• L119-154 src/types.ts• L9-11

Simple selectors

Simple selectors test the element currently under consideration. Tag selectors compare the adapter's element name with the selector name, while universal selectors accept every element that reaches that point in the predicate chain.

Tag selectors

Tag selectors match an element when its adapter-provided name equals the requested name. In non-XML matching, the engine can normalize the selector and element names to lower case; XML mode or explicit case settings preserve case-sensitive behavior.

The selector engine does not support namespaced tag names in this path. A namespaced tag request therefore fails as an unsupported selector rather than matching by a partial name.

References: src/general.ts• L65-82 src/types.ts• L101-115

Universal selectors

Universal selectors match any element without adding a name test. They still remain part of the predicate chain, so other simple selectors or combinators can decide the final result.

The engine accepts the ordinary universal form and the wildcard namespace form. Other namespaced universal selectors are not supported.

References: src/general.ts• L205-213

Combinator traversal

Combinator traversal changes the element on which the next selector part is tested. The traversal rules use only element relationships: non-element parent or sibling nodes do not satisfy a selector step.

Descendant traversal

Descendant traversal walks from the candidate to its parent and continues toward the root until it finds a matching ancestor. It succeeds at the first matching ancestor and fails when the element tree ends.

The engine can reuse descendant results when an expensive nested selector is involved. This changes the amount of repeated work, not the matching result, and callers must disable result caching when the backing tree can change between uses of the same compiled selector.

References: src/general.ts• L84-133 src/types.ts• L166-171

Child traversal

Child traversal checks the candidate's direct parent and applies the remaining selector to that parent. It does not continue to higher ancestors, so an indirect ancestor cannot satisfy a child relationship.

The parent lookup ignores non-element parents. This preserves the selector meaning when the DOM model includes document or fragment nodes above an element.

References: src/general.ts• L158-163 src/…/querying.ts• L140-151

Parent traversal

Parent traversal checks the candidate's direct children and succeeds when at least one child matches the remaining selector. It tests only child elements, not text, comment, document, or fragment nodes.

This direction is useful for selector forms that require a candidate to contain a matching child. It is distinct from child traversal, which starts at a candidate and tests its parent.

References: src/general.ts• L151-156 src/types.ts• L31-38

Sibling traversal

Sibling traversal examines the candidate's siblings before the candidate. It succeeds when any earlier element sibling matches the remaining selector. The adapter's equality rule determines where the candidate appears in the sibling list.

Adjacent sibling traversal narrows this rule to the nearest earlier element sibling. If the adapter provides a direct previous-element operation, the engine uses it; otherwise it derives the same relationship from the full sibling list.

References: src/general.ts• L164-203 src/…/querying.ts• L121-138 src/types.ts• L46-56

Query context

Selection begins with a set of nodes that can be expanded when sibling combinators need following siblings as candidates. The query then walks element descendants in depth-first order, while the traversal predicates inspect the relationships around each candidate.

Relative selectors also use context to establish their starting point. A relative selector without an explicit scope behaves as if it starts from the scope and then uses descendant traversal; disabling relative selectors rejects selectors that start with a traversal.

For the full public entry points and context contract, see Selection and matching operations and Selector configuration options. Attribute checks that run alongside these selectors are described in Attribute selector matching.

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

Change checklist

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

  • Changes to Adapter.getParent, getChildren, getSiblings, or isTag affect every combinator that walks the tree.
  • Changes to sibling equality or sibling-list ordering must be checked against both general and adjacent sibling matching.
  • Changes to prepareContext must preserve the extra following-sibling candidates needed by sibling-dependent queries.
  • Changes to tag-name normalization must stay aligned with xmlMode and lowerCaseTags behavior.
  • Changes to relative-selector context handling must preserve the distinction between explicit :scope and an implicit scope prefix.

Important failure modes

TriggerConsequenceGuard
getParent returns a non-element node as an element parentChild and descendant checks can match outside the element treegetElementParent accepts only adapter-recognized elements.
getSiblings omits the candidate or returns a different orderGeneral and adjacent sibling checks stop at the wrong nodeThe adapter contract includes the current node and uses stable sibling order.
A mutable tree reuses a compiled selector with caching enabledDescendant checks can return an old resultSet cacheResults to false when the tree changes between queries.
A selector uses an unsupported namespaced tag or universal selectorCompilation throws instead of producing a match predicateUse unnamespaced selectors or provide a supported selector form.
Relative selectors are disabled while a selector starts with traversalCompilation rejects the selectorKeep relativeSelector enabled or make the selector absolute.

Key files

FileWhy to read it
src/general.tsDefines tag, universal, ancestor, child, parent, sibling, and adjacent sibling matching.
src/…/querying.tsDefines element-parent lookup and sibling helpers used by traversal.
src/compile.tsConnects traversal tokens and simple selector checks into compiled predicates and handles relative context.
src/index.tsShows how public selection prepares the query context and invokes the compiled predicate.
src/types.tsDefines the adapter operations that form the tree and matching boundary.