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 / nested-selector-pseudos

Nested selector pseudo-selectors

Nested selector pseudo-selectors use another selector as part of a match. They let the engine test whether the current candidate fits one of several patterns or contains a matching related element. This page explains the selector relationship and scope rules; see Pseudo-selector evaluation for the wider pseudo-selector model and Filter and structural pseudo-selectors for other pseudo-selector behavior.

Nested selector groups

The nested selector group is the set of pseudo-selectors whose argument is itself a selector. The engine compiles that argument with the same selector rules as a top-level query, then uses its result while testing the candidate element.

Candidate matching

:is(), :matches(), and :where() accept one or more selector alternatives. A candidate matches when at least one alternative matches that same candidate. :matches() and :where() provide the same behavior as :is() in this engine. :not() reverses the result, so the candidate must fail every nested alternative.

These pseudo-selectors keep the surrounding selector in the same candidate flow. For example, article:is(.news, .review) first identifies an article candidate and then asks whether that candidate also fits either nested class selector.

References: src/…/subselects.ts• L36-70 src/…/index.ts• L40-45

Relative candidate matching

:has() changes the question from “does this candidate match?” to “does this candidate have a related match?”. Its nested selector is evaluated below the candidate, so article:has(img) selects an article with a matching descendant. Relative selectors such as :has(> img) start from the candidate and can require a direct child or a sibling relationship.

The engine treats the candidate as the scope for a relative :has() selector. It tests the candidate's children and includes following siblings when the nested selector needs them. This makes the candidate the anchor for the relationship instead of treating the nested selector as an unrelated document query.

References: src/…/subselects.ts• L72-139 src/compile.ts• L29-60 src/…/querying.ts• L120-138

Context and scope

Context limits where a selector may match. A context node can also be named by :scope, and nested selectors inherit the compilation context unless a pseudo-selector supplies a new candidate scope for a relative relationship.

Scope anchoring

The selector compiler adds the required scope relationship when a selector is relative and has no explicit :scope. A selector that begins with a traversal therefore acts as a relationship from its context rather than as a free search. An explicit :scope keeps that anchor visible in the selector and also makes the nested selector dependent on its current context.

For :has(), the current candidate becomes the temporary scope when the nested selector starts with a traversal. The nested selector can then express child, descendant, adjacent-sibling, or later-sibling conditions from that candidate.

References: src/compile.ts• L29-60 src/types.ts• L147-164 src/…/subselects.ts• L81-90

Candidate traversal

Nested matching searches only the related nodes required by the pseudo-selector. :is() and :not() test the candidate through their compiled nested predicate. :has() searches the candidate's children and stops when it finds the first matching element.

This separation matters when a query has a context. The outer query chooses candidate elements within that context, while :has() uses each candidate as the boundary for its own relation. A nested selector does not turn a scoped query into a search across the whole document.

References: src/…/subselects.ts• L36-44 src/…/subselects.ts• L97-138 src/…/querying.ts• L70-117

Compilation and evaluation

Nested selector pseudo-selectors are compiled as part of the surrounding selector. The compiler combines the nested result with the next matcher, which preserves the order of the complete selector while allowing constant matches to be removed early.

Selector alternatives

Each selector alternative in a nested argument is an independent match path. The alternatives are combined as an “any” result for :is(), :matches(), and :where(). :not() uses the same compiled alternatives as a negative condition. An empty or impossible nested result can therefore make the surrounding condition always pass or always fail.

The compiler also orders selector rules for efficient evaluation. Expensive nested checks such as :has() are kept late so simple candidate checks can reject elements first. This changes evaluation cost, not selector meaning.

References: src/compile.ts• L69-154 src/…/selectors.ts• L78-123 src/…/subselects.ts• L36-70

Result reuse

The engine can reuse a nested result when that result does not depend on the current candidate or context. A nested selector that begins with a traversal or contains :scope depends on its current anchor, so the engine must evaluate it for each relevant candidate.

This distinction keeps reuse safe. If the backing document changes after a compiled query is created, the general cache setting also controls whether cached results remain valid; see Selector cache lifecycle when that page is available.

References: src/…/subselects.ts• L20-34 src/…/subselects.ts• L84-138 src/types.ts• L165-171

Change checklist

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

  • Keep the alias relationship between :is(), :matches(), and :where() aligned with nested selector dispatch.
  • Preserve the distinction between outer query context and the candidate scope created for relative :has() selectors.
  • Recheck cache safety when changing how nested selectors detect :scope or leading traversal.
  • Recheck sibling traversal when changing relative selector handling, because :has() may need nodes after the candidate.

Important failure modes

TriggerConsequenceGuard
A nested pseudo name is passed with selector data but has no nested-selector handlerCompilation throws an unknown pseudo-class errorKeep selector-bearing pseudo names registered in the nested selector dispatch table.
A relative nested selector is evaluated without the current candidate as scope:has() can match the wrong relationship or search the wrong part of the treePreserve candidate scope setup before evaluating relative nested selectors.
A nested selector with :scope or leading traversal is reused as if it were context-freeResults from one candidate can leak to anotherKeep context-dependent nested selectors out of parent-result reuse.
The document changes while a compiled nested query uses cached resultsThe query can return stale matchesDisable result caching for mutable documents or recompile after changes.

Key files

FileWhy to read it
src/…/subselects.tsDefines the nested selector behavior for :is(), :matches(), :where(), :not(), and :has().
src/…/index.tsShows how selector-bearing pseudo-selectors are dispatched during compilation.
src/compile.tsExplains context anchoring, relative selector handling, and composition of nested matchers.
src/…/selectors.tsShows how the engine identifies scope and estimates the cost of nested checks.
src/…/querying.tsShows the candidate search used when a nested selector looks through related nodes.
src/types.tsDefines query context, relative selector, and result-cache options.