Repository navigation
feat(css): nothing typed a declaration's value for its property - #91
Merged
Merged
Conversation
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
This was referenced Nov 19, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Builds on #89, now merged.
Problem
parsec-css parsed CSS syntax but knew no properties:
color: redwas a declaration namedcolorwhose value was the identred. Lexbor types 105 properties and has novar()(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:scripts/properties.jsfrom w3c/webref's@webref/css8.7.9, vendored incrates/parsec-css/data/webrefwith its MIT licence and a COMMIT note, intosrc/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.<'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 aSpecified(typedComponents, a CSS-wide keyword, a value waiting forvar(), or a custom property's tokens), written back as CSSOM writes a specified value:||/&&groups in grammar order,normaldefaults left out, box shorthands at their shortest, gradient andcolor-mix()defaults dropped, anddisplay,white-spaceandflexin their shortest forms.Math, i.e.calc()and all the other math functions with their constants, simplified (§10.10), typed (§10.9) and serialized (§10.12).noneand 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().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.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.substituteandsubstitute_valueputvar()into any value. Inheritance and the cascade are left to Media queries and computed values #30: you pass the parent's resolved properties in.bench/: a typed-values table, Parsec's one-pass parse plus every declaration's value typed against Lexbor'slxb_css_stylesheet_parse.Tests
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 withnonenow keeping their own function, 6 custom color spaces (@color-profile), 4light-dark()written as a pair. The 293/12 syntax counts are unchanged.cargo test:scripts/wpt-values-cases.jsruns everycss/*/parsing/*.htmlin a Node vm sandbox with no DOM (Netweir's selector-test approach) and records eachtest_valid_value,test_invalid_value,test_shorthand_valueandfuzzy_test_valid_color: 14,893 cases from 777 files at WPT1a2a0c5, intests/wpt-values/cases.jsonwith 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 inknown-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 testalso 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).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: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 checkand the bench's clippy pass.Closes #29