Crate cssparser

source ·
Expand description

Implementation of CSS Syntax Module Level 3 for Rust.


Everything is based on Parser objects, which borrow a &str input. If you have bytes (from a file, the network, or something) and want to support character encodings other than UTF-8, see the stylesheet_encoding function, which can be used together with rust-encoding or encoding-rs.

Conventions for parsing functions

  • Take (at least) a input: &mut cssparser::Parser parameter
  • Return Result<_, ()>
  • When returning Ok(_), the function must have consumed exactly the amount of input that represents the parsed value.
  • When returning Err(()), any amount of input may have been consumed.

As a consequence, when calling another parsing function, either:

  • Any Err(()) return value must be propagated. This happens by definition for tail calls, and can otherwise be done with the ? operator.
  • Or the call must be wrapped in a Parser::try call. try takes a closure that takes a Parser and returns a Result, calls it once, and returns itself that same result. If the result is Err, it restores the position inside the input to the one saved before calling the closure.


// 'none' | <image>
fn parse_background_image(context: &ParserContext, input: &mut Parser)
                                    -> Result<Option<Image>, ()> {
    if input.try_parse(|input| input.expect_ident_matching("none")).is_ok() {
    } else {
        Image::parse(context, input).map(Some)  // tail call
// [ <length> | <percentage> ] [ <length> | <percentage> ]?
fn parse_border_spacing(_context: &ParserContext, input: &mut Parser)
                          -> Result<(LengthOrPercentage, LengthOrPercentage), ()> {
    let first = LengthOrPercentage::parse?;
    let second = input.try_parse(LengthOrPercentage::parse).unwrap_or(first);
    (first, second)




  • The fundamental parsing errors that can be triggered by built-in parsing routines.
  • A color specified by the color() function.
  • A string that is either shared (heap-allocated and reference-counted) or borrowed.
  • A fmt::Write adapter that escapes text for writing as a double-quoted CSS string. Quotes are not included.
  • A set of characters, to be used with the Parser::parse_until* methods.
  • Color specified by hue, saturation and lightness components.
  • Color specified by hue, whiteness and blackness components.
  • Color specified by lightness, a- and b-axis components.
  • Color specified by lightness, chroma and hue components.
  • Color specified by lightness, a- and b-axis components.
  • Color specified by lightness, chroma and hue components.
  • Extensible parse errors that can be encountered by client parsing implementations.
  • A CSS parser that borrows its &str input, yields Tokens, and keeps track of nested blocks and functions.
  • The owned input for a parser.
  • A capture of the internal state of a Parser (including the position within the input), obtained from the Parser::position method.
  • A color with red, green, blue, and alpha components, in a byte each.
  • Provides an iterator for rule bodies and declaration lists.
  • The line and column number for a given position within the input.
  • A position from the start of the input, counted in UTF-8 bytes.
  • Provides an iterator for rule list parsing at the top-level of a stylesheet.
  • A category of token. See the needs_separator_when_before method.
  • One contiguous range of code points.



  • A trait to provide various parsing of at-rules.
  • A trait that can be used to hook into how cssparser parses color components, with the intention of implementing more complicated behavior.
  • A trait to provide various parsing of declaration values.
  • Abstraction for avoiding a dependency from cssparser to an encoding library
  • This trait is used by the ColorParser to construct colors of any type.
  • A trait to provide various parsing of qualified rules.
  • A parser for a rule body item.
  • Trait for things the can serialize themselves in CSS syntax.