Skip to content

feat(css): nothing typed a declaration's value for its property - #91

Merged
jamesconwaydev merged 11 commits into
mainfrom
css-values
Nov 19, 2026
Merged

jamesconwaydev merged 11 commits into
mainfrom
css-values

Conversation

@jamesconwaydev

@jamesconwaydev jamesconwaydev commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Builds on #89, now merged.

Problem

parsec-css parsed CSS syntax but knew no properties: color: red was a declaration named color whose value was the ident red. Lexbor types 105 properties and has no var() (lexbor#232), and css-parsing-tests' 8,061 color cases were skipped for want of typed values (#29).

Change

A new module, parsec_css::values, no new dependencies:

  • Every property in the CSS specs: 822 (597 longhands, 161 shorthands, 64 legacy aliases), generated by scripts/properties.js from w3c/webref's @webref/css 8.7.9, vendored in crates/parsec-css/data/webref with its MIT licence and a COMMIT note, into src/values/generated.rs: grammar, initial value, inheritance, longhands, and the grammars of the 530 types and 172 functions they use. property(name), properties(). Filter Effects' prose rule against negative values is written into its functions' grammars, since webref has only the grammar.
  • A value definition syntax parser and a backtracking matcher: every combinator and multiplier, ranges, <'property'> references, comma elision (§2.6), a 200,000-step budget per value, and Values 4 §4.2's rule that a keyword the grammar can take isn't a <custom-ident>. parse_value(property, values) returns a Specified (typed Components, a CSS-wide keyword, a value waiting for var(), or a custom property's tokens), written back as CSSOM writes a specified value: ||/&& groups in grammar order, normal defaults left out, box shorthands at their shortest, gradient and color-mix() defaults dropped, and display, white-space and flex in their shortest forms.
  • Values 4: every unit; Math, i.e. calc() and all the other math functions with their constants, simplified (§10.10), typed (§10.9) and serialized (§10.12).
  • Colors: CSS Color 4 in full (none and math in any channel), and from Color 5 relative colors (channel names in math too), color-mix(), contrast-color(), color-layers(), light-dark(), device-cmyk().
  • Shorthands: expand(shorthand, values) splits into longhands by the longhands the grammar names, or by position (sides, a / b); the rest get their initial values. Where neither works it says invalid rather than guess.
  • Custom properties: CustomProperties::resolve(parent) resolves them against each other with Tarjan's algorithm written without recursion (a 50,000-long chain resolves), makes cycles (fallbacks included) the guaranteed-invalid value, and caps substitution at 100,000 component values against exponential growth. substitute and substitute_value put var() into any value. Inheritance and the cascade are left to Media queries and computed values #30: you pass the parent's resolved properties in.
  • Nothing recurses on input without a limit: math nests to 32, colors in colors to 16, grammars to 48.
  • bench/: a typed-values table, Parsec's one-pass parse plus every declaration's value typed against Lexbor's lxb_css_stylesheet_parse.

Tests

  • css-parsing-tests' color files now run: 4,803 of 8,061 pass. Numbers are compared to within the precision CSS Color 4 leaves to the implementation (legacy rgb() channels rounded to integers, alpha to three decimals). Each of the other 3,258 is checked by the runner against one of four ways CSS Color changed since the suite was written, and counted under it; anything else fails the run: 2,760 lightness/chroma now clamped when parsed (the runner clamps the suite's answer and checks it then matches), 488 legacy colors with none now keeping their own function, 6 custom color spaces (@color-profile), 4 light-dark() written as a pair. The 293/12 syntax counts are unchanged.
  • web-platform-tests' CSS parsing tests, new in cargo test: scripts/wpt-values-cases.js runs every css/*/parsing/*.html in a Node vm sandbox with no DOM (Netweir's selector-test approach) and records each test_valid_value, test_invalid_value, test_shorthand_value and fuzzy_test_valid_color: 14,893 cases from 777 files at WPT 1a2a0c5, in tests/wpt-values/cases.json with LICENSE.md and COMMIT; 442 files have nothing static (mostly computed values, Media queries and computed values #30). 13,899 pass (valid 8,760 of 9,429, invalid 4,920 of 5,070, shorthand 219 of 394). The 994 others are in known-failures.txt; the run fails on any other failure or if a listed case passes. They're mostly per-property canonical forms not done yet (508), grammars in webref behind the specs (161), prose-only restrictions (150), and shorthands the grammar doesn't split (93 not split, 82 a longhand different).
  • cargo test also checks every grammar in the table parses and every longhand's initial value is a value of its own grammar (6 listed exceptions, prose), plus 21 new unit tests and 8 doctests (math, colors, relative colors, deep nesting, custom property cycles, exponential growth, a 50,000 chain, shortest forms, shorthand splits).
  • The fuzz target now types, writes and re-reads every declaration's value, splits shorthands and resolves custom properties: clean for 5 minutes twice locally (1.1M and 600k inputs).

cargo run --release --manifest-path bench/Cargo.toml -- css, twice on an M2 Max, each started with the one-minute load under 2.4, ten real stylesheets (1.87 MB), 33,155 declaration values:

MB/s, all ten Parsec Lexbor 3.0.1
Parse + every value typed, run 2 120 116
run 1 124 113

By sheet it's 0.70× to 1.17× (1.34× in run 1): short-declaration <style> sheets are slower than Lexbor, the big frameworks faster. Not the same work: Lexbor types 105 properties and keeps the rest as text, Parsec types all 822; Lexbor also parses selectors. The syntax-only rows from #89 are unchanged (221 MB/s parse to cssparser's 195–199).

cargo fmt --all, cargo clippy --workspace --all-targets -- -D warnings, cargo test --workspace, cargo deny check and the bench's clippy pass.

Closes #29

parse_stylesheet kept each rule's {} block as component values, and a
caller wanting every declaration then walked each block again with
contents(). parse_stylesheet_deep does both in one pass: each block's
declarations and rules are parsed straight from the tokenizer as the
parser reaches them, with a stack of open blocks so nesting doesn't
recurse, and a shared buffer so each list is allocated once at its size.

The rule types take the block as a type parameter, defaulting to the
raw SimpleBlock, so every existing entry point and the raw-block API
are unchanged; the deep parse returns Stylesheet<Block>.

Also fixes the raw path leaving a declaration's colon behind when its
value was empty (`a{foo:}`), which then read as a rule and reported a
spurious InvalidRule error.
The values layer's first part, CSS Values and Units 4 and CSS Color:

- Unit: every unit in Values 4 and the specs it points to, with its
  kind and, for the absolute ones, its ratio to the canonical unit;
  numbers written as CSSOM writes them, to six significant figures.
- Math: calc() and every other Values 4 math function, with its
  constants, parsed into a calculation tree, simplified as §10.10
  says, typed as §10.9 says, and serialized as §10.12 says. Nesting
  past 32 deep is invalid rather than a risk to the stack.
- Color: CSS Color 4 in full, none and math in any channel, written
  as §15 and WPT have it; and from Color 5, relative colors (with the
  origin's channel names in math), contrast-color(), light-dark() and
  device-cmyk(). Colors nested in colors stop at 16 deep.

The module isn't wired in yet; the next commit does that.
parsec_css::values knows every property in the CSS specs and parses a
value the way element.style[property] = value does:

- A table of all 822 properties (597 longhands, 161 shorthands, 64
  legacy aliases), generated by scripts/properties.js from w3c/webref's
  @webref/css 8.7.9, pinned in data/webref with its MIT licence: each
  one's grammar, initial value, inheritance and longhands, and the
  grammars of the 530 types and 172 functions they use. Filter Effects'
  "no negative values" is written into its functions' grammars, since
  webref carries only the grammar.
- A parser for the value definition syntax and a backtracking matcher
  for it, with comma elision, a step budget against hostile input, and
  the primitive types typed: numbers, every dimension and the math
  functions that fit them, colors, strings, URLs and identifiers.
  Keywords the grammar has elsewhere aren't taken as identifiers where
  the value can be read without that.
- Serialization as CSSOM writes a specified value: || and && groups in
  the grammar's order, default parts left out, box shorthands at their
  shortest, gradients' and color-mix()'s defaults dropped, and the
  shortest forms of display, white-space and flex.
- expand(): a shorthand split into its longhands, by the longhands its
  grammar names or by position, the rest at their initial values.
- CustomProperties: custom properties resolved against each other with
  Tarjan's algorithm, written without recursion, cycles made the
  guaranteed-invalid value, var() substituted with fallbacks anywhere,
  and substitution capped at 100,000 component values.

css-parsing-tests' color files now run: 4,803 of 8,061 pass, and every
other case is checked against one of four ways CSS Color has changed
since the suite was written. web-platform-tests' CSS parsing tests,
extracted by scripts/wpt-values-cases.js running each file in a Node
sandbox, join cargo test: 13,899 of 14,893 pass, and the rest are
listed. The fuzz target types, writes and re-reads every value, splits
every shorthand and resolves the custom properties it generates.
# Conflicts:
#	README.md
#	ROADMAP.md
#	bench/src/css.rs
#	crates/parsec-css/fuzz/fuzz_targets/parse.rs
@jamesconwaydev
jamesconwaydev merged commit 88e02fd into main Nov 19, 2026
32 checks passed
@jamesconwaydev
jamesconwaydev deleted the css-values branch November 19, 2026 05:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom properties, var(), calc() and every longhand

2 participants