Repository navigation
feat(css): Parsec had no CSS parser to run css-parsing-tests against - #87
Merged
Merged
Conversation
crates/parsec-css (package parsec-html-css, crate parsec_css) is CSS Syntax Level 3 as the current editor's draft has it: the tokenizer, every parser entry point including the nesting-era block contents, the An+B and <urange> microsyntaxes, the @charset byte-stream decoding, and serialization back to text that parses to the same values. Every token, function, block, rule and declaration has a byte span, and LineIndex turns one into a line and column. Names and values borrow the input unless an escape changes them. Nothing recurses on the input's nesting, building, writing or dropping. cargo test runs css-parsing-tests (CourtBouillon, 203ce36), the urange cases from rust-cssparser's copy, and Romain Menke's tokenizer tests (0db1d12), and round-trips every input through the serializer. 293 of 305 syntax cases pass; the 12 others are where the suite predates the draft, each listed with the reason. All 12 urange and all 287 tokenizer cases pass. A cargo-fuzz target joins the nightly fuzz run, and bench/ gains a css mode timing tokenizing and parsing against Lexbor's CSS module and Servo's cssparser on real stylesheets.
The CSS Syntax parser moves from planned to done in the README's list and table, with its css-parsing-tests count and its speed against Lexbor and cssparser, and M7 in the roadmap records it.
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.
Problem
Parsec had no CSS parser, so nothing could run css-parsing-tests, and nothing for #29 and #30 to build on. Lexbor has one, tested only against its own files, with no positions on rules or declarations (lexbor#374).
Change
A new crate,
crates/parsec-css(packageparsec-html-css, crateparsec_css), written from the CSS Syntax Level 3 editor's draft as of October 2026:parse_stylesheet,parse_block_contents(nesting's declarations-then-rules),parse_declaration_list,parse_rule,parse_declaration,parse_component_value,parse_component_values,parse_comma_separated,parse_unicode_range_value, andparse_rule_listfor the 2021 draft's CDO/CDC behaviour. A rule's{}block is kept as component values andSimpleBlock::contentsparses it when the rule calls for that, with the same result as parsing it at once.LineIndexgives lines and columns on request.parse_anb,anb,serialize_anb),<urange>(parse_unicode_ranges), anddecode_stylesheetfor bytes with@charset, through encoding_rs, which the workspace already has.to_cssandDisplayon every type) that parses back to the same values, with/**/where the spec's table says, plus the two pairs it misses (--before>,<before!).Also: a fuzz target in
crates/parsec-css/fuzz, added to the nightly matrix; acssmode inbench/timing tokenizing and parsing against Lexbor's CSS module (through a small C file, since reading a token's type needs Lexbor's struct layout) and Servo'scssparser0.38 (a bench-only dependency);bench/stylesheets.txtwith five MIT stylesheets fetched by hash; NOTICE entries for the vendored test data; README and ROADMAP.Tests
cargo test -p parsec-html-cssruns three suites, pinned with their commit and licence, and writes their counts for CI's summary:color_*files, CSS Color rather than Syntax. The 12 failures are where the current draft parses differently from the suite's: 9 expect unicode-range tokens outside aunicode-rangedescriptor, 1 has U+0080 as an ident code point, and 2 are "parse a declaration" before nesting made it stop at;. Each is listed with its reason, and the run fails on any other failure or if a listed one passes.<urange>cases from rust-cssparser's copy, at 4765111: 12 of 12.Every suite input is also serialized and parsed back, as values, as a stylesheet and as block contents. There are 12 unit tests and 9 doctests, and the fuzz target ran clean for about seven minutes locally after the two serializer fixes it found.
cargo run --release --manifest-path bench/Cargo.toml -- css, twice on an M2 Max with the load under 2.5, ten real stylesheets (1.87 MB), MB/s for all ten, second run:Ahead of Lexbor on every sheet at both (1.2 to 2.7 times tokenizing, 1.1 to 1.65 parsing, and Lexbor's parse does more: selectors and typed values). cssparser parses a third faster, since it builds no tree; Parsec keeps every block with spans and parses contents from it. The first run was within 2% except cssparser tokenizing (321).
cargo fmt --all,cargo clippy --workspace --all-targets -- -D warnings,cargo test --workspace,cargo deny check, and the bench's own clippy and deny all pass.Closes #28