Skip to content

feat(css): nothing cascaded stylesheets onto a document - #94

Merged
jamesconwaydev merged 6 commits into
mainfrom
css-cascade
Nov 19, 2026
Merged

jamesconwaydev merged 6 commits into
mainfrom
css-cascade

Conversation

@jamesconwaydev

Copy link
Copy Markdown
Collaborator

Problem

parsec-css could type a value but not say which value an element gets: no cascade, no media query evaluation, no computed values (#30). Lexbor has none of the last two either (discussion #265).

Change

Three new modules in parsec-html-css, which now depends on parsec-html for the document and its selector engine:

  • media: <media-query-list> per Media Queries 5 (types with not/only, and/or/not conditions, plain, boolean and one- and two-sided range features, ratios, math), written back as mediaText, and evaluated against an Environment you give with three-valued logic, so an unknown feature makes its query false.
  • supports: @supports and CSS.supports(): declarations through the property table, selector() through Parsec's selector engine, font-format(), font-tech().
  • cascade: Styler styles a Parsec document.
    • Sheets: user agent (the HTML Standard's rendering-section stylesheet ships as HTML_UA_STYLESHEET), user and author, <style media>, @media and @supports.
    • Ordering, per CSS Cascade 5 §6: origin and importance, then style="" as element-attached, then @layer (statements, nesting, anonymous layers, reversed for !important), then specificity per complex selector, then order of appearance.
    • CSS Nesting desugared to :is(), shorthands split as they cascade, and all.
    • Computed values in tree order: inheritance, initial/inherit/unset/revert/revert-layer, custom properties resolved with the parent's and var() substituted (invalid at computed-value time is unset), absolute lengths, canonical angles and times, colours worked out, and the common per-property rules (font-size and font-weight keywords, line-height, border/outline widths when no style draws them, blockification, opacity, filters, positions, gradients).
    • Styled::get(element, property) gives the value as getComputedStyle() writes it; style_element styles one element and its ancestors.
    • Rules are bucketed by the id, class, type or attribute their rightmost compound needs.
  • Colour maths in values: conversion between every CSS Color 4 space (its sample code's matrices and transfer functions), missing channels carried per §12.2. CSS Color 5's relative colours and color-mix() (any number of colours, premultiplied, every hue method) are worked out from them. Also contrast-color() and alpha(from …).
  • Values fixes: flex's left-out parts expand to 1 1 0%, and white-space's legacy keywords expand to their longhands.
  • bench/: a cascade table: every element of the 13 real pages styled.

Not done, and on the wiki page: anything needing layout (width: 50% as pixels, grid tracks, transform-origin as pixels), @container, @scope, @property, @keyframes, @counter-style, and fetching @import.

Tests

Three more WPT suites in cargo test, at the same pinned commit (1a2a0c5) as #91, each extracted by a Node vm-sandbox script in crates/parsec-css/scripts (probes stand in for what a test asks the browser), each with a known-failures.txt the run checks both ways. scripts/fetch-wpt-cascade.sh COMMIT refreshes all three.

  • Media queries: css/mediaqueries, mostly the Mozilla suite, with its viewport changes recorded per case. 1,370 of 1,370 (applies 1,052, parses 283, serializes 35); 14 skipped (they probe more than once in a test).
  • Computed values: test_computed_value, fuzzy_test_computed_color and inheritance-testcommon.js from css/*/parsing and css/*/inheritance*. The runner parses each vendored test file with Parsec, applies its <style>s and the UA sheet, sets #target's style as the test does in an 800×600 viewport, and checks the value and its round trip. 10,974 of 12,041 (computed 10,525/11,563, inherited 229/239, initial 220/239); 357 skipped. The failures are mostly values that need layout (156 are grid tracks) and per-property computed forms not done yet.
  • Cascade: css/css-cascade tests that add <style> from script and read a computed style (the layer tests, mostly). 104 of 123; the 19 are @scope (13) and the tentative revert-rule (6).
  • WPT CSS parsing: now 13,907 of 14,893 (8 more, from the flex and white-space expansion).
  • Other tests: 9 new unit tests (origins and importance, style attributes, layers including important reversal and sublayers, revert, revert-layer and all, inheritance and units, var() with fallbacks and invalid-at-computed-time, media/supports/nesting, buckets, supports conditions) and new doctests.
  • Fuzz: the target also parses and evaluates media queries and supports conditions, and cascades its input onto a small page. 236,000 inputs in 5 minutes, clean.

bench -- css, twice on an M2 Max, each started with the load under 2.8, cascade over the 13 real pages, median of 5, run 2:

Page Elements <style> CSS ms
reddit-programming 1,280 284 KB 196.1
nytimes 6,454 43 KB 76.8
wikipedia-population 10,276 32 KB 118.8
whatwg-full 337,200 none 1,972.2
all 13 2,766.3

Run 1 totalled 2,770.5. Most pages <link> their CSS, which isn't fetched, so they're timed with the UA sheet alone. There's no Lexbor row because Lexbor computes no values. The syntax and typed-value rows are unchanged from #91.

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

Closes #30

parsec_css::media parses a <media-query-list> as Media Queries 5 has
it (types, not and only, and/or/not conditions, plain, boolean and
range features including the two-sided form, ratios and math), writes
it back as CSSOM's mediaText does, and evaluates it against an
Environment the caller gives, with the spec's three-valued logic so an
unknown feature makes its query false. parsec_css::supports evaluates
@supports and CSS.supports(): declarations through the property
table, selector() through Parsec's selector engine, font-format() and
font-tech().

WPT's media query tests run in cargo test: scripts/wpt-media-cases.js
runs each file in a Node sandbox where what a test asks the browser
goes through a probe, and the Mozilla suite's viewport changes are
recorded with each case. All 1,370 cases pass.
values::convert has CSS Color 4's conversions between every space
(the sample code's matrices and transfer functions, through XYZ) with
missing channels carried as §12.2 says, and CSS Color 5's relative
colours and color-mix() worked out from them, any number of colours,
premultiplied, with each hue method. Color::absolute gives a colour's
channels; color-mix() keeps what it mixes, and contrast-color() and
alpha(from …) parse.

Also: flex's left-out parts expand to 1, 1 and 0% and white-space's
legacy keywords to their longhands, which eight WPT shorthand cases
now pass; Math can substitute keywords and map its leaves, which the
computed values build on.
parsec_css::cascade styles a Parsec document: user agent, user and
author sheets, `<style media>`, @media and @supports evaluated against
the environment, @layer with nesting and anonymous layers, CSS Nesting
desugared to :is(), specificity per complex selector, !important with
origins and layers reversed, style="" as element-attached, then every
element's computed values in tree order: inheritance, initial, inherit,
unset, revert and revert-layer, `all`, custom properties resolved with
the parent's and var() substituted, invalid-at-computed-value-time as
unset, lengths absolute, colours worked out, and the per-property rules
(font-size and font-weight keywords, line-height, border widths with
no style, display blockification, opacity, filters, positions and
gradients). The HTML Standard's user agent stylesheet comes with it.
Rules are bucketed by their rightmost id, class, type or attribute.

WPT in cargo test, each runner parsing the vendored test file itself:
computed values (test_computed_value, the colour tests and the
inheritance tests), 10,974 of 12,041; and css-cascade's static
cases, 104 of 123. Failures are listed in each known-failures.txt.
The fuzz target also parses media queries and supports conditions
and cascades its input onto a small page.
@jamesconwaydev
jamesconwaydev merged commit 2fd7d6f into main Nov 19, 2026
32 checks passed
@jamesconwaydev
jamesconwaydev deleted the css-cascade branch November 19, 2026 06:19
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.

Media queries and computed values

1 participant