Donner implements CSS3 in its own CSS library, donner::css. The library has minimal dependencies and can be integrated into other projects with an adapter that satisfies the donner::ElementLike concept.
donner::css::CSS provides a high-level API for parsing CSS constructs. It is implemented on top of lower-level parser APIs that give finer-grained control over parsing and more detailed error handling.
The SVG runtime applies parsed CSS through donner::svg::components::StyleSystem. The cascade uses this pipeline:
The unindexed and unfiltered modes are test-only differential paths. Production callers use the indexed, filtered cascade.
Names beginning with -- cascade as untyped token sequences. Computed custom-property maps and their token sequences are immutable and shared; retained allocations are charged to the document style budget until the last holder releases them.
Donner supports the static-name CSS syntax var(--name[, fallback]) in stylesheet declarations, inline style and presentation attributes, including shorthands. Resolution is iterative. It follows the dependency branch selected by substitution, marks an actual selected cycle guaranteed-invalid, and permits a dependent declaration to choose its fallback. An invalid or over-limit expansion publishes no partial token sequence.
Ordinary typed properties that contain var() remain pending until the custom-property map is complete. The substituted component values are then parsed with the property's normal grammar; failure makes that winning declaration invalid at computed-value time.
Stylesheets retain named, dotted, nested and anonymous @layer statements and blocks. Layered @import rules participate at their declared position in the document layer stream. Normal declarations use first-layer-to-last-layer precedence, while important declarations reverse that order. Unlayered author declarations outrank normal layered declarations, and presentation attributes remain below author rules. Inline style keeps its existing author-inline precedence.
Layer order is recomputed when stylesheet contents, imports or document order change. The same frozen order is shared by serial and prepared parallel cascade work.
SVGDocument::applySourceEdit projects XML edits into stylesheet-local offsets. StylesheetComponent::applyTextEdit reparses and splices a verified rule window; edits that cannot be mapped or resynchronized use a full stylesheet parse. SelectorDependencySummary and the old and new rule windows identify affected elements. StyleSystem::computeStylesFor recomputes that set, while structural or otherwise unbounded changes request a full style pass. See Structured Source Editing for the XML edit transaction and source-provenance model shared by stylesheet edits.
The source editor uses XML regions to locate CSS and applies the CSS tokenizer to every byte of style elements, style attributes and presentation values. Completion preserves XML completion and uses the same property metadata and source-derived custom names. See Source Focus for source-to-style mapping and selection behavior.
Large eligible passes use StyleSystem::PreparedBatch and StylePassScheduler. Workers read a const registry, own budget shares, warning buffers, ancestor filters and matched-style caches, and write only preallocated result slots. The document owner publishes parents and commits results in document order. Unsupported inputs, budget pressure or insufficient work fall back to the serial path. Native Linux and Apple admission requires 64 KiB of live client-stack headroom above the scheduler's separate 32 KiB reporting reserve; it does not allocate a fixed 64 KiB stack. Other platforms remain serial for this path. See Multithreading for document access and registry ownership outside the style pass.
The public donner/css representation exposes layer and variable metadata needed by embedders:
The core SVG document and element mutation semantics are unchanged. Public source tooling in SVGStyleQuery.h exposes source-backed matched rules, exact stylesheet-range mapping and bounded custom-property provenance. FindCustomPropertyDeclarations charges aggregate work supplied by the caller, and VisitAuthoredStylesheetDeclarations borrows its views only for the callback. ElementFactory::TryWrapStyle wraps an existing embedded stylesheet without mutation.
Rule indexes, invalidation summaries, custom-property maps, cascade budgets, counters and caches remain internal components. StyleCascadeMetrics provides cumulative logical-work counters and style time for diagnostics and benchmarks.
CSS and SVG text are untrusted input. Parsing, substitution, matching and retention fail closed at document or value boundaries; a refusal never exposes partial substituted output.
| Resource | Current limit |
|---|---|
| One substituted value | 65,536 component values and 1 MiB source text |
| One stylesheet | 4,096 layers, nested at most 32 deep |
| One document layer order | 16,384 layer nodes |
| One import pass | depth 16, 64 loads and 2 MiB imported CSS text |
| Rule-element candidates admitted per style pass | 1,048,576 |
| Matched declaration applications admitted per style pass | 1,048,576 |
| Aggregate declaration work | 16,777,216 component units and 16 MiB source text |
| Retained complex property data | 64 MiB per style budget, also charged to its document family |
The resource loader remains the trust boundary for external imports. It controls reachable URLs; secure processing modes and the editor do not provide an external loader. Import cycles, invalid UTF-8, disallowed media types and exhausted fetch budgets are diagnosed and skipped.
The rule index, ancestor filter, immutable records and matched-properties cache reduce ordinary work without changing computed results. StyleCascadeCounters::rulesTested counts admitted rule-element candidates; rulesMatched counts successful selector matches; and declarationsApplied counts separately admitted declaration applications. Aggregate generated component and byte work use independent counters. Retained maps, token sequences and complex properties instead use lifetime byte accounts shared by their holders and document family.
Selective source edits expose the same logical counters as full passes, making differential tests and benchmarks compare work as well as values. Avoiding constant overhead on ordinary documents that do not use custom properties or layers remains an accepted optimization area; changes must preserve imports, inheritance, cache identity, mutation invalidation and every resource limit.
Each shipped editor Wasm runtime is limited to 11,274,000 raw bytes and 3,550,000 gzip bytes by //donner/editor/wasm:wasm_geode_package_size_tests.
The main verification targets are:
Add conformance cases at the lowest layer that owns the guarantee, and add an SVG computed-style companion when token substitution alone does not prove inheritance or neighboring declarations.
The cascade does not implement env(), typed attr(), dynamic variable names such as var(var(...)), SMIL animation of custom properties, conditional @import evaluation, or the revert-layer keyword. Unsupported conditional imports are diagnosed and ignored.
CSS @import is supported in SVG <style> elements. Loading is split between the CSS library and the SVG document:
Import keywords are recognized after CSS escape decoding and without regard to ASCII case. The target is a <string>, an unquoted url(), or url(<string>); an import may end at EOF without a semicolon. Following CSS Cascade 5, an import is valid only before every other rule except @layer statements (@charset is consumed first). Unknown at-rules and style rules that fail to parse are dropped and do not end that leading section.
Conditions are decided as follows:
| Import | Behavior |
|---|---|
| No condition, or the media query all | Loaded |
| Other media query lists | Ignored with a warning, matching how @media blocks are ignored |
| supports(...) | Ignored with a warning, matching how @supports blocks are ignored |
| layer or layer(name) | Loaded into the anonymous or named cascade layer at the import's position |
| After another rule, malformed, or with an empty target | Ignored with a warning |
Diagnostics use static text and never repeat the target URL.
Imports never add a network capability. They are fetched only through the resource loader the application configured for the document, sharing the external-fetch cache, the per-document fetch attempt limit and the aggregate resource byte budget used by <image>, @font-face and external <use> references. The loader decides what a URL may reach; for example, donner::svg::SandboxedFileResourceLoader rejects paths outside its root, including after .. resolution.
Imports load when the document is prepared for rendering, so a computed-style query made before the first render does not yet include imported rules; the first render discards such styles and recomputes them with the imports in place.
Warnings are reported at the top-level import that led to them, mapped to document source when the <style> text has source provenance. Diagnostics from parsing imported text are not reported, since their offsets refer to another resource. Rules loaded through @import have no document source, so editor queries that map rules back to source consider only each <style> element's own rules.
A CSS Stylesheet is composed of a list of rules, each of which contains a selector and a list of declarations.
There are two main rule types:
| Rule type | Example |
|---|---|
| Qualified rule | div { color: red; } |
| At-rule | @media (max-width: 600px) { ... } |
Qualified rules are the most common and are used to apply styles to elements that match a selector. At-rules are used for media queries, keyframes, and other special constructs.
Within qualified rules, there is a selector and a list of declarations. The selector is used to match elements in the DOM, and the declarations are a list of key-value pairs that apply styles to the matched elements.
flowchart TD Stylesheet --> SelectorRule ComponentValue --> Token Token --> String Declaration --> QualifiedRule Declaration --> ComponentValue Rule --> QualifiedRule Rule --> AtRule AtRule --> SimpleBlock AtRule --> ComponentValue SimpleBlock --> ComponentValue QualifiedRule --> ComponentValue QualifiedRule --> SimpleBlock SelectorRule --> Selector SelectorRule --> Declaration Selector --> ComplexSelector ComplexSelector --> CompoundSelector ComplexSelector --> Combinator CompoundSelector --> TypeSelector CompoundSelector --> IdSelector CompoundSelector --> ClassSelector CompoundSelector --> PseudoClassSelector CompoundSelector --> PseudoElementSelector CompoundSelector --> AttributeSelector TypeSelector --> ComponentValue IdSelector --> ComponentValue ClassSelector --> ComponentValue TypeSelector --> ComponentValue PseudoClassSelector --> ComponentValue PseudoElementSelector --> ComponentValue AttributeSelector --> ComponentValue
Selectors provide many ways to match elements in the DOM.
| Selector | Example | Description |
|---|---|---|
| TypeSelector | div | Matches all div elements |
| ClassSelector | .class | Matches all elements with the class class |
| IdSelector | #id | Matches the element with the ID id |
| AttributeSelector | [attr=value] | Matches elements with the attribute attr set to value |
| PseudoClassSelector | :first-child | Matches elements in a special state, such as the first child of their parent |
| PseudoElementSelector | ::first-line | Not yet supported, matches special elements in the tree, mostly for text |
Attribute selectors, pseudo-classes, and combinators match elements based on their state or their relationships with other elements.
Selectors can be chained with combinators to create complex matching rules. For example, div > p matches p elements that are children of div elements.
Attribute selectors are used within square brackets on the selector list, such as a[href^="https://"] or h1[title]. Different match modes are available, which are specified by AttrMatcher.
| Matcher | Enum | Description |
|---|---|---|
| ~= | Includes | Interprets the attribute value as a space-separated list of values. Matches if any value in the list is equal |
| \|= | DashMatch | Matches if the attribute value either exactly matches, or begins with the value immediately followed by a dash, - |
| ^= | PrefixMatch | Matches if the attribute value begins with the matcher value |
| $= | SuffixMatch | Matches if the attribute value ends with the matcher value |
| *= | SubstringMatch | Matches if the attribute value contains the matcher value |
| = | Eq | Matches if the attribute value exactly matches the matcher value |
If no matcher is specified, the attribute is matched if it is set to any value, for example, a[href].
| Selector | Matches |
|---|---|
| a[href] | All a elements with an href attribute |
| a[href^="https://"] | All a elements with an href attribute that begins with "https://" |
| a[href$=".pdf"] | All a elements with an href attribute that ends with .pdf |
| a[href*=".pdf"] | All a elements with an href attribute that contains .pdf |
| a[class~="link"] | An alternate way of writing a.link, included here to illustrate the ~= matcher. |
Pseudo-classes match elements in special states, such as being the first child of their parent, or having a child that matches a selector.
| Pseudo-class | Description |
|---|---|
| :root | Matches the root element of the document. |
| :empty | Matches elements that have no children. |
| :first-child | Matches elements that are the first child of their parent. |
| :last-child | Matches elements that are the last child of their parent. |
| :only-child | Matches elements that are the only child of their parent. |
| :nth-child(An+B [of S]) | Matches elements that are the An+B-th child of their parent. S is a Selector that should match child elements to count. |
| :nth-last-child(An+B [of S]) | Matches elements that are the An+B-th child of their parent, counting from the last child. S is a Selector that should match child elements to count. |
| :is(S) | Matches elements that match any of the selectors inside the parentheses, where S is a Selector parsed as a forgiving selector list (invalid selectors removed). |
| :not(S) | Matches elements that do not match the Selector inside the parentheses. |
| :where(S) | Matches elements that match any of the selectors inside the parentheses, where S is a Selector parsed as a forgiving selector list (invalid selectors removed). Same as :is(S), except has a specificity of 0. |
| :has(S) | Matches elements that have a descendant that matches the selector inside the parentheses, where S is a relative forgiving selector list. These may start with a combinator, such as :has(> .highlighted) |
| :nth-of-type(An+B) | Matches elements that are the An+B-th child of their parent, counting only elements of the same type. |
| :nth-last-of-type(An+B) | Matches elements that are the An+B-th child of their parent, counting from the last child and only elements of the same type. |
| :first-of-type | Matches elements that are the first child of their parent and are of the same type. |
| :last-of-type | Matches elements that are the last child of their parent and are of the same type. |
| :only-of-type | Matches elements that are the only child of their parent and are of the same type. |
| :defined | Matches if the element is supported by the runtime. For SVG this would match <path> and not <div>. |
CSS::ParseStylesheet can parse a .css file into a Stylesheet, which contains a list of SelectorRule.
CSS::ParseStyleAttribute parses style="..." attribute values, which are a list of key: value; pairs.
ParseSelector parses a CSS selector, and may be used for implementing querySelector.
flowchart TD SelectorParser --> Tokenizer SelectorParser --> AnbMicrosyntaxParser SelectorParser --> SelectorParser CSS --> StylesheetParser CSS --> DeclarationListParser CSS --> SelectorParser StylesheetParser --> SelectorParser StylesheetParser --> RuleParser StylesheetParser --> DeclarationListParser DeclarationListParser --> Subparsers DeclarationListParser --> Tokenizer DeclarationListParser --> Subparsers DeclarationListParser --> Tokenizer Subparsers --> ComponentValueParser ComponentValueParser --> Tokenizer Tokenizer --> LengthParser Tokenizer --> NumberParser RuleParser --> ComponentValueParser RuleParser --> Tokenizer
donner::css::CSS wraps the low-level parsing APIs to parse a stylesheet and selectors: