Skip to main content

Module selector

Module selector 

Available on crate features html and http only.
Expand description

Native CSS selector parsing and matching.

This module provides a small, dependency-free CSS selector engine for the subset of selectors that can be evaluated while streaming HTML (no sibling lookahead, no full-document state). It is the foundation for rama’s streaming HTML parser/rewriter, but is also usable on its own to match selectors against any tree that implements SelectorSubject — including the in-memory Dom provided here.

§Supported selectors

PatternMeaning
*any element
Ean element of type E (ASCII case-insensitive)
E.clsan E with class cls
E#idan E with id id
E[a]an E with an a attribute
E[a=v]value exactly v
E[a~=v]whitespace-separated list containing v
E[a^=v] E[a$=v] E[a*=v]prefix / suffix / substring
E[a|=v]v or v- prefix
E[a=v i] / E[a=v s]case-insensitive / case-sensitive value
E:nth-child(An+B) E:first-childstructural position
E:nth-of-type(An+B) E:first-of-typestructural position by type
E:not(s)negation of a combinator-free compound s
E F / E > Fdescendant / child combinator
a, b, cselector list (matches if any matches)

Sibling combinators (+, ~), :has(), :is(), :where(), namespaces, pseudo-elements, and the non-streamable structural pseudos (:last-child, :only-child, :last-of-type, :only-of-type, :nth-last-*) are intentionally rejected with a SelectorError.

Structs§

Compound
A compound selector: an optional type/universal selector plus zero or more subclass selectors, all of which must match the same element.
Dom
A simple arena-backed element tree.
Element
A cheap, copyable view of an element in a Dom.
NodeId
Handle to an element within a Dom.
Selector
A parsed CSS selector string.

Enums§

SelectorError
Error returned when parsing a CSS selector string fails, or when it uses a construct outside the streaming-safe supported subset.

Traits§

SelectorSubject
A read-only view of an element, providing exactly what the selector matcher needs. Implement it for your own tree to match selectors against it, or use the in-memory Dom.