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 / filter-and-structural-pseudos

Filter and structural pseudo-selectors

Filter and structural pseudo-selectors add conditions to element matching. They let a query select elements by form state, text, language, position among siblings, or nearby structure. The selector engine combines these conditions with normal element, attribute, and relationship selectors before it returns matches.

Predicate groups

The pseudo-selector set groups conditions by the kind of element fact they test. A selector can combine groups, so a query can first identify a kind of element and then narrow it by state or position.

GroupMain purposeExamples
StateMatch form and interaction state:checked, :disabled, :required, :hover
ContentMatch text or empty content:contains(text), :icontains(text), :empty
PositionMatch an element's place among siblings:first-child, :nth-child(2n), :last-of-type
StructureMatch parent, root, scope, or language relationships:parent, :root, :scope, :lang(en)

The parent pseudo-selector page describes the full pseudo-selector family. The nested selector pseudo-selectors page covers predicates that evaluate another selector inside the current one.

State predicates

State predicates identify elements from their HTML state or from state supplied by the query adapter. Form aliases include :disabled, :enabled, :checked, :required, :optional, :read-only, :read-write, and :selected. Type aliases such as :checkbox, :radio, :file, :password, :submit, and :button select common control types.

Interaction predicates include :hover, :active, and :visited. They match only when the adapter can report that state. If the adapter does not provide the needed state, the predicate has no matches.

Content predicates

Content predicates select elements from their text or child content. :contains(text) uses a case-sensitive substring match, while :icontains(text) uses a case-insensitive match. :empty matches elements with no element children and no non-whitespace text.

The :parent alias is the inverse content check for ordinary use: it matches elements that are not empty. Text is evaluated through the active adapter, so the same query can work with different node models.

References: src/…/filters.ts• L115-130 src/…/pseudos.ts• L19-32 src/…/aliases.ts• L57-63

Position predicates

Position predicates compare an element with its element siblings. The child forms count all element types, while the of-type forms count only siblings with the same element name. The last forms count from the end.

The :nth-child() and :nth-last-child() forms accept an An+B rule. They can also limit the count to siblings that match an of selector, which supports positions within a filtered sibling group. :first-child, :last-child, :only-child, :first-of-type, :last-of-type, and :only-of-type provide common fixed positions.

The engine ignores non-element siblings for these checks. This keeps whitespace and text nodes from changing an element's structural position.

References: src/…/filters.ts• L34-109 src/…/filters.ts• L132-136 src/…/pseudos.ts• L34-115

Structural predicates

Structural predicates describe where an element sits in the queried tree. :root identifies an element with no element parent, while :scope identifies the current query context. With no explicit context, :scope follows the root behavior.

:lang() matches a language set on the element or inherited from an ancestor. It accepts one or more language ranges and recognizes both lang and xml:lang. This makes language matching depend on the element's ancestry rather than only on its own attributes.

Aliases also provide structural shorthand. :header covers heading elements, and :input covers common form controls. :any-link and :link select links by link state. Predicates that contain another selector, such as :has(), belong to the nested selector group; see nested selector pseudo-selectors for that boundary.

References: src/…/filters.ts• L138-205 src/…/aliases.ts• L16-18 src/…/aliases.ts• L57-63 src/…/subselects.ts• L47-72

Alias predicates

Aliases give frequently used conditions a short pseudo-selector name. They expand to normal selector rules, so their behavior follows the same element, attribute, structural, and nested-selector rules as any other selector.

The main alias families are:

  • Link state: :any-link and :link.
  • Form state: :disabled, :enabled, :checked, :required, :optional, :read-only, :read-write, and :selected.
  • Form type: :checkbox, :file, :password, :radio, :reset, :image, :submit, and :button.
  • Common structure: :parent, :header, :input, and :text.

An alias is a convenience name, not a separate matching model. For example, :enabled means a supported form control that is not disabled, and :button includes both button elements and button-type inputs.

References: src/…/aliases.ts• L11-63 src/…/index.ts• L48-64

Matching flow

The selector engine turns each pseudo-selector into a condition that runs as part of the complete query. A condition can inspect the current element, its content, its siblings, or its ancestors before the query returns the element.

This diagram shows how a selector moves from a pseudo name to a final element match.

flowchart LR
    A[Selector with pseudo] --> B[Recognize alias or predicate]
    B --> C[Evaluate element state or structure]
    C -->|matches| D[Apply remaining selector conditions]
    C -->|does not match| E[Reject element]
    D --> F[Return selected element]

Unknown pseudo-selectors fail instead of silently matching. Pseudo-functions also enforce whether an argument is required or forbidden, which keeps malformed queries separate from valid zero-argument predicates.

References: src/…/index.ts• L31-89 src/…/pseudos.ts• L117-136

Change checklist

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

  • Keep alias names aligned with the selector forms they expand to. A change to a nested alias can alter state or structural behavior without changing the alias API.
  • Keep An+B position rules aligned with the optional of selector. The two parts define one position contract.
  • Keep adapter-dependent state predicates aligned with the adapter methods for :hover, :active, and :visited.
  • Keep language matching aligned with both inherited lang and xml:lang values.

Important failure modes

TriggerConsequenceGuard
Adapter lacks a dynamic-state method:hover, :active, or :visited returns no matchesProvide the matching adapter capability when that state is needed
Invalid or unsupported An+B expressionThe selector cannot be compiledUse a valid An+B rule and a valid optional of selector
Unknown pseudo-selectorQuery evaluation throws an unknown-pseudo errorUse a registered alias or supported predicate
Non-whitespace text inside an element:empty does not matchTreat content, including text nodes, as part of the emptiness check

Key files

FileWhy to read it
src/…/aliases.tsDefines the short names for link, form, control-type, and common structural predicates.
src/…/filters.tsDefines content, position, language, root, scope, and dynamic-state matching.
src/…/pseudos.tsDefines direct content and sibling-relationship checks.
src/…/index.tsShows how a parsed pseudo-selector becomes part of a complete query.
src/…/subselects.tsDefines the boundary for predicates that evaluate nested selectors.