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
- src/index.ts -> docs/selector-api.md
- src/index.ts -> docs/selection-and-matching-api.md
- src/types.ts -> docs/type-contracts.md
- src/option.ts -> docs/selector-options.md
- src/compile.ts -> docs/selector-compilation.md
- src/compile.ts -> docs/compilation-orchestration.md
- src/…/selectors.ts -> docs/predicate-composition.md
- src/general.ts -> docs/selector-matching.md
- src/general.ts -> docs/general-selectors-and-traversal.md
- src/attributes.ts -> docs/attribute-selectors.md
- src/pseudos.ts -> docs/pseudo-selectors.md
- src/…/aliases.ts -> docs/filter-and-structural-pseudos.md
- src/…/filters.ts -> docs/filter-and-structural-pseudos.md
- src/…/subselects.ts -> docs/nested-selector-pseudos.md
- src/…/querying.ts -> docs/query-execution-and-caching.md
- src/…/querying.ts -> docs/query-traversal.md
- src/…/cache.ts -> docs/selector-cache.md