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 / attribute-selectors

Attribute selector matching

Attribute selector matching checks whether an element has a named attribute and whether its value follows a requested rule. It supports presence checks and CSS value comparisons while using the active adapter to read the element. This page focuses on attribute rules within selector matching; see general selectors and traversal for the wider query flow.

Matching model

Selector forms

An attribute selector adds one predicate to the current element match. A presence selector checks only that the attribute exists. A value selector reads the attribute value and applies one comparison before it passes control to the other selector rules.

FormMatch condition
[name]The named attribute exists.
[name=value]The full value equals value.
[name~=value]value is one whitespace-separated word.
[name|=value]The value equals value or starts with value-.
[name^=value]The value starts with value.
[name$=value]The value ends with value.
[name*=value]The value contains value.
[name!=value]The value does not equal value.

The matcher treats a missing value as no match for positive value comparisons. The not-equal form normally accepts a missing attribute, except when its comparison value is empty. Empty operands also make the word, prefix, suffix, and substring forms fail.

References: src/attributes.ts• L84-169 src/attributes.ts• L171-274 test/qwery.ts• L182-236

Matching flow

The matching flow turns a parsed attribute token into a predicate that can be joined with the rest of a selector.

flowchart LR
    A[Parsed attribute token] --> B[Attribute rule]
    B --> C{Presence or value test}
    C --> D[Adapter attribute access]
    D --> E{Predicate passes}
    E -->|yes| F[Next selector rule]
    E -->|no| G[Element rejected]

The selector compiler orders rules before it builds the predicate. The attribute rule then evaluates the element and calls the next rule only after its own condition succeeds. This keeps attribute checks as one part of a larger selector rather than as a separate query stage.

References: src/compile.ts• L69-76 src/compile.ts• L119-149 src/general.ts• L25-55

Case handling

Comparison rules

Case behavior is decided for each attribute token. An explicit case flag takes priority. The i form makes value comparison case-insensitive, while the s form keeps it case-sensitive. Without an explicit flag, HTML rules apply to a defined set of attributes when XML mode is off.

The comparison changes the attribute value and selector value only for the value test. Presence checks remain existence checks. XML mode disables the HTML default unless the selector explicitly asks for case-insensitive matching.

References: src/attributes.ts• L17-79 src/attributes.ts• L92-113 src/attributes.ts• L114-166

Name normalization

Attribute names and attribute values follow different case rules. In HTML mode, or when lowerCaseAttributeNames is enabled, the selector name is normalized to lower case before the adapter reads it. XML mode preserves the selector name unless that option requests lower-casing.

This means a custom adapter must use attribute names that agree with the selected name-normalization mode. Value case handling does not change the name passed to the adapter.

References: src/general.ts• L44-55 src/types.ts• L97-110

Adapter access

Attribute contract

The adapter is the boundary between selector matching and the backing node model. getAttributeValue supplies a string value or undefined when no value is available. hasAttrib supplies the separate presence check used by [name].

The public compiler first confirms that the candidate is an element. Attribute rules therefore depend on the adapter for element identity and attribute data rather than reading DOM fields directly. The default adapter uses domutils, while callers can provide an adapter for another node model.

References: src/types.ts• L14-28 src/types.ts• L60-64 src/types.ts• L143-146 src/index.ts• L20-23 src/index.ts• L51-63

Attribute data changes

Compiled matching reads attribute data through the adapter when the predicate runs. If a caller changes an attribute after compiling a selector, a cached compiled query can keep an earlier result. Disable result caching when the backing document can change between uses of the same compiled query.

References: src/attributes.ts• L92-112 src/types.ts• L166-171 test/api.ts• L413-436

Change checklist

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

  • Keep every AttributeAction supported by the parser mapped to one rule in attributeRules.
  • Keep attribute-name normalization aligned with the names returned by custom adapters.
  • Preserve the difference between hasAttrib for presence and getAttributeValue for value comparison.
  • Recheck cached-query behavior when attribute reads or mutation handling changes.
  • Recheck HTML, XML, explicit i/s, and quirks-mode case contracts when changing case logic.

Important failure modes

TriggerConsequenceGuard
A custom adapter returns a value under a different attribute-name case than the selected mode expects.A valid selector does not match.Align adapter names with xmlMode and lowerCaseAttributeNames.
A presence rule uses value lookup instead of the presence method.An existing attribute with an empty value can be treated as absent.Keep [name] on hasAttrib.
A compiled query is reused after attribute mutation with caching enabled.The query can return a stale match.Set cacheResults: false for mutable backing data.
HTML default case rules are applied in XML mode.Case-sensitive XML names or values match incorrectly.Preserve XML mode unless the selector explicitly requests case folding.

Key files

FileWhy to read it
src/attributes.tsDefines the presence and value-comparison rules and their case behavior.
src/general.tsConnects parsed attribute tokens to the matching rules and normalizes names.
src/types.tsDefines the adapter contract and matching options that affect attributes.
src/compile.tsShows how attribute predicates join the ordered selector matcher.
src/index.tsShows the public element check and default adapter boundary.
test/qwery.tsDemonstrates the supported CSS attribute selector forms.
test/api.tsDemonstrates XML mode, caching, and option behavior that affect matching.