8 releases

Uses old Rust 2015

0.1.7 Jun 20, 2020
0.1.6 Jun 14, 2018
0.1.5 Nov 11, 2017
0.1.3 Oct 17, 2017
0.1.1 Feb 28, 2017

#464 in Procedural macros

Download history 1784/week @ 2022-06-13 1478/week @ 2022-06-20 1180/week @ 2022-06-27 1154/week @ 2022-07-04 1089/week @ 2022-07-11 1138/week @ 2022-07-18 1074/week @ 2022-07-25 1628/week @ 2022-08-01 2019/week @ 2022-08-08 1588/week @ 2022-08-15 1867/week @ 2022-08-22 1314/week @ 2022-08-29 1429/week @ 2022-09-05 1722/week @ 2022-09-12 1474/week @ 2022-09-19 1467/week @ 2022-09-26

6,160 downloads per month
Used in 90 crates (5 directly)


88 lines


Build Status


Rust implementation of CSS Syntax Module Level 3


Parsing CSS involves a series of steps:

  • When parsing from bytes, (e.g. reading a file or fetching an URL from the network,) detect the character encoding (based on a Content-Type HTTP header, an @charset rule, a BOM, etc.) and decode to Unicode text.

    rust-cssparser does not do this yet and just assumes UTF-8.

    This step is skipped when parsing from Unicode, e.g. in an HTML <style> element.

  • Tokenization, a.k.a. lexing. The input, a stream of Unicode text, is transformed into a stream of tokens. Tokenization never fails, although the output may contain error tokens.

  • This flat stream of tokens is then transformed into a tree of component values, which are either preserved tokens, or blocks/functions ({ … }, [ … ], ( … ), foo( … )) that contain more component values.

    rust-cssparser does this at the same time as tokenization: raw tokens are never materialized, you only get component values.

  • Component values can then be parsed into generic rules or declarations. The header and body of rules as well as the value of declarations are still just lists of component values at this point. See the Token enum for the data structure.

  • The last step of a full CSS parser is parsing the remaining component values into Selectors, specific CSS properties, etc.

    By design, rust-cssparser does not do this last step which depends a lot on what you want to do: which properties you want to support, what you want to do with selectors, etc.

    It does however provide some helper functions to parse CSS colors and An+B (the argument to :nth-child() and related selectors.

    See Servo’s style crate for an example of a parser based on rust-cssparser.

No runtime deps