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

overview

Knowledge Base updated: 2026-10-25

css-select Knowledge Base

Orientation

css-select is a CSS selector engine for adapter-provided DOM-like trees. Its public API accepts selectors and elements, compiles selectors into predicates, and executes those predicates during traversal.

The public integration boundary lives in src/index.ts, src/types.ts, and src/option.ts. Compilation and matching are separated into selector compilation, general/attribute matching, pseudo-selector dispatch, and query execution helpers.

Critical reusable state is the compiled-selector cache in src/…/cache.ts; DOM-specific behavior is supplied through the configured adapter rather than owned by the engine.

flowchart TD
  API[src/index.ts public API] --> Options[src/option.ts options]
  API --> Query[src/helpers/querying.ts query traversal]
  API --> Compile[src/compile.ts compilation]
  Options --> Compile
  Compile --> Compose[src/helpers/selectors.ts predicate composition]
  Compile --> General[src/general.ts general selectors]
  Compile --> Attributes[src/attributes.ts attribute selectors]
  Compile --> Pseudos[src/pseudos.ts pseudo dispatch]
  Pseudos --> Filters[src/pseudo-selectors/filters.ts filters and structural pseudos]
  Pseudos --> Subselects[src/pseudo-selectors/subselects.ts nested selectors]
  Compile --> Cache[src/helpers/cache.ts compiled selector cache]
  Query --> Adapter[DOM adapter]
  General --> Adapter
  Attributes --> Adapter
  Filters --> Adapter

Glossary

  • Adapter: The domhandler/domutils-style contract through which the engine reads a DOM-like tree.
  • Compiled predicate: A function produced from selector tokens that determines whether an element matches.
  • Context: The element or element set that bounds context-sensitive selector evaluation.
  • Subselect: A nested selector evaluated by pseudo-selectors such as selector-list pseudos.
  • Strict mode: A compilation mode that rejects selector behavior incompatible with the requested matching operation.

Invariants

  • All element-tree access goes through the configured adapter contract (src/types.ts, src/option.ts); selector logic must not assume a concrete DOM implementation.
  • Public selection and matching APIs compile selectors before applying them (src/index.ts, src/compile.ts).
  • Selector alternatives preserve OR semantics while token sequences preserve combinator ordering (src/compile.ts, src/…/selectors.ts).
  • Query results are produced by traversal and predicate filtering rather than by mutating the input tree (src/…/querying.ts).
  • Cached compiled selectors are keyed and reused only through the cache helper lifecycle (src/…/cache.ts).

Routing table