Expand description
CSS Syntax Level 3: the tokenizer, the parser and its entry points, the
An+B and <urange> microsyntaxes, and serialization back to CSS, as
the W3C’s current editor’s draft defines them, nesting included.
parse_stylesheet_deep parses a whole stylesheet at once, every
block’s contents included; parse_stylesheet keeps each rule’s
{} block as component values, for SimpleBlock::contents to parse
when you want it, since what a block holds depends on its rule.
Every rule, declaration, block, function and token carries its
Span in the input, and LineIndex turns one into a line and
column. Names and values borrow the input unless an escape changes
them. tests/ runs the css-parsing-tests suite, and the CSS tokenizer
tests, against it.
use parsec_css::{Item, Rule};
let css = "a { color: red; &:hover { color: blue } }";
let sheet = parsec_css::parse_stylesheet(css);
let Rule::Qualified(rule) = &sheet.rules[0] else { unreachable!() };
assert_eq!(parsec_css::to_css(&rule.prelude), "a ");
let contents = rule.block.clone().contents(css);
let Item::Declaration(color) = &contents.items[0] else { unreachable!() };
assert_eq!((&*color.name, color.span.start), ("color", 4));
assert!(matches!(contents.items[1], Item::Rule(_)));Modules§
- cascade
- The cascade, CSS Cascading and Inheritance Level 5, over a Parsec
document: stylesheets from the user agent, the user and the author,
@mediaand@supportsevaluated against anEnvironment,@layer, nesting, specificity,!importantandstyle="", then every element’s computed values with inheritance, the CSS-wide keywords (revertandrevert-layerincluded), custom properties andvar(). - media
- Media queries, Media Queries Level 5: a
<media-query-list>parsed, written back as CSSOM’smediaTextwrites it, and evaluated against anEnvironmentyou give, with the three-valued logic §3.2 has, so a feature nobody knows makes its query false rather than an error. - rules
- The at-rules besides
@media,@supportsand@layer:@keyframes,@counter-styleand@propertyparsed into what they say, and the preludes of@container,@scopeand@import, which the cascade evaluates. Each is written back as CSSOM writes it. - supports
@supportsandCSS.supports(), CSS Conditional Rules Level 5: a condition parsed and evaluated against what this crate parses. A declaration is supported when its value parses for its property, aselector()when Parsec’s selector engine parses it, andfont-format()andfont-tech()when they name a format or technology the specs define.- values
- Typed values: CSS Values and Units Level 4’s numbers, dimensions and math functions, CSS Color Level 4’s colors, and the value definition syntax every property’s grammar is written in.
Structs§
- AtRule
@name prelude;or@name prelude { block }.- Block
- A rule’s
{}block with its contents parsed, as declarations and rules, and its rules’ blocks too: what [parse_stylesheet_deep] makes. - Block
Contents - A block’s declarations and rules, and what was dropped while parsing it.
- Declaration
name: value !important.- Encoding
- An encoding as defined in the Encoding Standard.
- Function
name( … ).- Line
Index - The start of every line in a text, for turning offsets into lines and columns. CR LF, CR, LF and form feed each end a line, as CSS counts.
- Number
- A
<number-token>’s value, and the text it was written as. - Parse
Error - What a parse found wrong, and where.
- Qualified
Rule prelude { block }: a style rule when the prelude is a selector.Bis as forAtRule.- Simple
Block ( … ),[ … ]or{ … }.- Span
- Where something is in the input, in bytes:
start..end. - Stylesheet
- A stylesheet’s rules, and what was dropped while parsing it.
- Tokenizer
- Turns CSS text into tokens, one at a time, skipping comments.
- Unicode
Range - A
<urange>from CSS Fonts: the code pointsstart..=end.
Enums§
- Bracket
- Which bracket a
SimpleBlockwas written with. - Component
Value - A token, a function or a block: what every value, prelude and block in CSS is a list of.
- Error
Kind - Item
- One thing in a block: style rules and the nesting-era at-rules mix declarations and rules, in order.
- Rule
- Token
- A CSS token. The ones that open a block or a function never reach a
parser’s output; they turn into
ComponentValues.
Functions§
- anb
- Reads An+B from a list of component values:
(A, B), orNoneif they aren’t An+B. Values beyondi32saturate. - decode_
stylesheet - Decodes a stylesheet’s bytes, returning the text and the encoding it was in. Malformed bytes become U+FFFD.
- fallback_
encoding - The fallback encoding: what the bytes are decoded with unless they
start with a byte order mark.
protocolis a charset label from HTTP or the like;environmentis the linking document’s encoding. - parse_
anb anbover CSS text.- parse_
block_ contents - Parse a block’s contents: declarations, at-rules and nested style rules,
in order, the way a style rule’s
{}block holds them since CSS Nesting.SimpleBlock::contentsdoes the same for a parsed block. - parse_
comma_ separated - Parse a comma-separated list of component values, split at the commas outside any block or function.
- parse_
component_ value - Parse a component value: exactly one, with only whitespace around it.
- parse_
component_ values - Parse a list of component values: the whole input, as a selector or a media query list is read.
- parse_
declaration - Parse a declaration, as
@supports (…)does: exactly one. - parse_
declaration_ list - Parse a list of declarations, as for a
styleattribute. The spec has at-rules here parsed and thrown away; they’re kept, asItem::Rules, for you to throw away. Nothing else becomes a rule. - parse_
rule - Parse a rule: exactly one, with only whitespace around it.
- parse_
rule_ list - Parse a list of rules as the 2021 Candidate Recommendation did, where
<!--and-->are ordinary tokens. The current draft’s “parse a stylesheet’s contents” isparse_stylesheet. - parse_
stylesheet - Parse a stylesheet: its rules, with
<!--and-->skipped between them as the HTML<style>element’s history needs. - parse_
stylesheet_ deep - Parse a stylesheet with every rule’s
{}block parsed too, as declarations and nested rules, to any depth: whatparse_stylesheetandSimpleBlock::contentson each block make, in one pass and about 70% of the time. The errors are all in the stylesheet’s list, in the order they were found. - parse_
unicode_ range_ value - The value of a
unicode-rangedescriptor, tokenized with unicode-range tokens: the one place CSS makes them. - parse_
unicode_ ranges - Reads a
unicode-rangedescriptor’s comma-separated<urange>s. Each isNoneif it isn’t one range, or its range is backwards or past U+10FFFF. - serialize_
anb - Writes An+B the way CSS Syntax serializes it:
2n+1,-n,5. - to_css
write_valuesinto a new string.- write_
identifier - CSSOM’s “serialize an identifier”, except that a non-ASCII code point outside the ident set is escaped, as it must be since CSS Syntax narrowed that set in 2024.
- write_
string - CSSOM’s “serialize a string”, in double quotes.
- write_
values - Writes component values as CSS text.