Skip to content

feat(css): typed values weren't written in CSSOM's canonical forms - #98

Merged
jamesconwaydev merged 2 commits into
mainfrom
css-canonical-forms
Nov 19, 2026
Merged

jamesconwaydev merged 2 commits into
mainfrom
css-canonical-forms

Conversation

@jamesconwaydev

Copy link
Copy Markdown
Collaborator

Problem

After #91, parsec_css::values matched every value against its property's grammar and wrote it back in the grammar's order, but not in the shortest form CSSOM gives each property. scale: 100% 100% stayed as it was, where browsers write 1. circle(closest-side) kept its default, and a lone left as a position wasn't left center. Of WPT's 14,893 static CSS parsing cases, 986 failed: 508 on canonical forms, 161 on grammars webref is behind on, 150 on rules the specs only give in prose, and 167 on shorthands. Typing every value was also slower than Lexbor's parse on the short-declaration <style> sheets in the bench, 0.70× at worst (#92).

Change

All in crates/parsec-css. There are no new dependencies.

  • Canonical forms, in values/canonical.rs. There are rules for 65 properties (opacity: 50% as 0.5, rotate: 0 1 0 90deg as y 90deg, counter-reset: x as x 0, grid-area: a / b / a / b as a / b, plus list-style, mask, line-clamp, animation-range, grid-template, transform-origin and the rest). There are also rules for the types and functions they share, wherever those appear: positions, repeat-style, bg-size, basic shapes, transform and filter functions, easing, counters and symbols(). A shorthand's || group leaves out longhands at their initial values. If that leaves nothing, it writes the first longhand's initial value (text-decoration: solid is none). Box shorthands collapse repeats at any depth.
  • Prose restrictions, in values/restrict.rs:
    • names a <custom-ident> can't be: none as a counter, span and auto as grid lines, will-change in will-change, generic families and revert-rule in a family name;
    • steps() and symbols() counts;
    • OpenType tags;
    • grid-template-areas rectangles, checked in linear time;
    • flex's unitless zero.
  • SVG path data, in values/path.rs. path() strings are parsed by SVG 2's grammar and written as browsers write them. Bad or empty data makes path() invalid.
  • The matcher:
    • Commas follow Values 4 §2.6 exactly, so linear-gradient(, red, blue) and cursor: url(x) pointer are invalid.
    • A unitless zero is a <number> where both a number and a length would do.
    • sibling-index() and sibling-count() are math functions.
  • Grammar patches, in scripts/properties.js, for places where webref lags its spec. Each patch cites its source: <timeline-range-name>, line-clamp, column-width (whose initial value webref lost), circle()/ellipse(), polygon's radius, <url-set>, light-dark() images, and <single-transition>'s duration.
  • expand:
    • splits background, mask, transition, animation and animation-range layer by layer;
    • follows a longhand that's a shorthand with its own longhands;
    • handles border-radius, grid-area/grid-row/grid-column and place-content;
    • fixes one-value margin, padding and inset, which set only the first side before;
    • resets mask-border for mask and border-image for border.
  • Speed, found with sample:
    • A lone keyword is looked up in a per-property table built the first time the property is seen.
    • A one-token value skips the backtracking matcher.
    • Each type and longhand reference knows what token it can start with, so alternatives that can't match aren't tried.
    • The canonical pass works on the match in place for the common shapes and hands it back untouched when nothing changes.
    • font-family has a direct parser, tested against the matcher.
    • Property names use an open-addressed table hashed on their first and last eight bytes.
  • Hostile input: the new passes are linear or bounded by grammar depth, layer splitting caps a run at eight values, and the fuzz target now also parses and splits every value as font, background, mask, grid, animation, transition, clip-path, offset, grid-template-areas, animation-range and others.

Tests

  • WPT CSS parsing: 14,758 of 14,893 pass, up from 13,907. Valid values: 9,383 of 9,429. Invalid values: 5,054 of 5,070. Shorthands: 321 of 394. One canonical-form failure is left, and its test disagrees with its neighbours: image-resolution: snap -8dpcm from-image is expected in the order written, while every other case expects grammar order. The other 134 failures:

    • 73 are shorthands, 39 of them CSS Gaps' repeat() lists;
    • 55 are values the current editor's drafts don't have or rule out (safe normal in align-items, font-synthesis: oblique-only, quotes: match-parent, image-orientation without angles, and others);
    • 6 are legacy quirks: unitless -webkit-perspective and CSS 2's comma rect().

    The wiki's CSS values page lists them all.

  • WPT computed values: 11,287 of 12,041 pass, up from 10,974, because a computed value is written in the same shortest form. Media queries (1,370 of 1,370) and cascade (104 of 123) are unchanged.

  • Runner fix: the WPT runner's tolerant colour comparison read 1em as an exponent. It now reads it as a number and a unit, which had failed every colour case with sign(1em - 10px) in it. Shorthand cases also accept a longhand named by its legacy alias.

  • New unit tests: path data, the font-family fast path against the matcher, and the sort order of the canonical table.

  • Fuzzing: the extended fuzz target ran clean for five minutes twice, 276,444 inputs and then 233,969 on the final code.

Benchmarks. cargo run --release --manifest-path bench/Cargo.toml -- css, run twice on an M2 Max, each run started with the one-minute load under 2.1. Typed values, every declaration's value parsed for its property:

MB/s, all ten sheets Parsec Lexbor 3.0.1
run 2 145 114 1.26×
run 1 143 115 1.24×

By sheet, Parsec is ahead in both runs, from 1.04× (the New York Times' and BBC's <style>) to 1.9× (Font Awesome); before this change the range was 0.70× to 1.17×. The cascade over the 13 pages takes 2.3 s, down from 2.8 s.

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

Closes #92

A value matched its grammar and was written back in the grammar's
order, but not in the shortest form CSSOM gives each property:
`scale: 100% 100%` stayed as written, `circle(closest-side)` kept its
default, a lone `left` wasn't `left center`. 508 WPT parsing cases
failed on that alone, 150 more on rules the specs give in prose, and
short-declaration stylesheets typed slower than Lexbor parses them.

Canonical forms live in values/canonical.rs, for 65 properties and
the types and functions they share; prose restrictions in
values/restrict.rs; SVG path data in values/path.rs. Shorthands leave
out longhands at their initial values, `expand` splits lists of
layers and nested shorthands, and grammars webref lags on are patched
in the generator with where each comes from. Commas follow Values 4
§2.6, and sibling-index() and sibling-count() are math functions.

For speed: lone keywords are looked up, one-token values skip the
matcher, grammars know what token they can start with, the common
match shapes need no tree, and property names hash in a few
instructions.

WPT CSS parsing goes from 13,907 to 14,758 of 14,893, computed values
from 10,974 to 11,287 of 12,041.
@jamesconwaydev
jamesconwaydev enabled auto-merge (squash) November 19, 2026 08:45
@jamesconwaydev
jamesconwaydev merged commit 2f83834 into main Nov 19, 2026
33 checks passed
@jamesconwaydev
jamesconwaydev deleted the css-canonical-forms branch November 19, 2026 08:48
jamesconwaydev added a commit that referenced this pull request Nov 19, 2026
# Conflicts:
#	README.md
jamesconwaydev added a commit that referenced this pull request Nov 19, 2026
* feat(css): @scope, @container, @import and @Property didn't apply

The cascade read @media, @supports and @layer and dropped every other
at-rule: @scope rules never matched, @container ones never held,
@import fetched nothing, and @Property registered nothing, so a
registered custom property behaved like any other. @Keyframes and
@counter-style weren't parsed at all.

parsec_css::rules parses @Keyframes, @counter-style and @Property into
what they say, and the preludes of @container, @scope and @import. The
cascade applies @scope with scoping proximity in its order, evaluates
@container's size and style() queries against sizes the caller gives,
fetches @imports through the caller's fetch with layer(), scope(),
supports() and media, and computes registered properties by their
syntax, inheritance and initial value. Styler::at_rules gives the
@Keyframes, @counter-style and @Property that win for each name.

The WPT cascade runner now also takes css-conditional's container
queries, css-properties-values-api, the @counter-style syntax tests
and the <template>-based @scope tests: 1,139 of 1,345 pass, from 104
of 123.

* docs: the at-rule count after #98 is 1,141 of 1,345

* docs: ROADMAP's at-rule count too
@jamesconwaydev jamesconwaydev mentioned this pull request Nov 19, 2026
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.

Typed CSS values: canonical forms and small sheets

1 participant