Skip to main content

Crate parsec_css

Crate parsec_css 

Source
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, @media and @supports evaluated against an Environment, @layer, nesting, specificity, !important and style="", then every element’s computed values with inheritance, the CSS-wide keywords (revert and revert-layer included), custom properties and var().
media
Media queries, Media Queries Level 5: a <media-query-list> parsed, written back as CSSOM’s mediaText writes it, and evaluated against an Environment you 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, @supports and @layer: @keyframes, @counter-style and @property parsed into what they say, and the preludes of @container, @scope and @import, which the cascade evaluates. Each is written back as CSSOM writes it.
supports
@supports and CSS.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, a selector() when Parsec’s selector engine parses it, and font-format() and font-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.
BlockContents
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( … ).
LineIndex
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.
ParseError
What a parse found wrong, and where.
QualifiedRule
prelude { block }: a style rule when the prelude is a selector. B is as for AtRule.
SimpleBlock
( … ), [ … ] 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.
UnicodeRange
A <urange> from CSS Fonts: the code points start..=end.

Enums§

Bracket
Which bracket a SimpleBlock was written with.
ComponentValue
A token, a function or a block: what every value, prelude and block in CSS is a list of.
ErrorKind
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), or None if they aren’t An+B. Values beyond i32 saturate.
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. protocol is a charset label from HTTP or the like; environment is the linking document’s encoding.
parse_anb
anb over 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::contents does 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 style attribute. The spec has at-rules here parsed and thrown away; they’re kept, as Item::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” is parse_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: what parse_stylesheet and SimpleBlock::contents on 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-range descriptor, tokenized with unicode-range tokens: the one place CSS makes them.
parse_unicode_ranges
Reads a unicode-range descriptor’s comma-separated <urange>s. Each is None if 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_values into 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.