css-tokenize
A CSS tokenizer after CSS Syntax Level 3, in safe Rust without dependencies.
Turns a &str into CSS tokens and nothing else. It knows no grammar built on top of the
tokens: media queries, selectors and stylesheets live in other packages. Norm: CSS Syntax
Module Level 3, §3 (preprocessing) and §4 (tokenization).
Status
On crates.io, no dependencies,
#![forbid(unsafe_code)]. Used by media-query-parse and stylesheet-parse. It was split
out of media-query-parse (barrierlab#116), which still re-exports it under a hidden path for
code that used the old one.
Tested against component_value_list.json from SimonSapin/css-parsing-tests (CC0, pinned
in tests/corpus/css-parsing-tests/UPSTREAM.md). The cases the tokenizer does not satisfy are
listed in tests/css_parsing_tests_known_failures.txt: they need <unicode-range> tokens
or the Selectors 3 match tokens (^=), neither of which is part of CSS Syntax Level 3. The
list is checked both ways, so a listed case that starts to pass fails the test.
Structure
Public surface
| Entry | Purpose |
|---|---|
tokenize | text → Vec<Token>, always ending with a single Token::Eof |
Tokenizer | the same as an iterator, for callers that do not want the whole vector |
Token | Ident, Function, AtKeyword, Hash, String, Url, Number, Percentage, Dimension, Delim, Whitespace, brackets, Colon, Semicolon, Comma, Cdo, Cdc, BadString, BadUrl, Eof |
NumericType, HashType | the integer/number and id/unrestricted flags of the specification |
Token and the flag enums are #[non_exhaustive].
Dependencies
Downward: none. Upward: media-query-parse (L0) and stylesheet-parse (L1).
Limits
- Tokens only. No component-value, rule or selector parsing, no judgement of what the tokens mean.
- No parse errors reported. Where the specification marks a parse error, the tokenizer
recovers as prescribed (
BadString,BadUrl); theeof-in-*errors are not exposed. - No
<unicode-range>and no Selectors 3 match tokens, because neither is part of CSS Syntax Level 3. Token::Percentagecarries no numeric type flag.