Skip to main content

azul_simplecss/
lib.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5/*! A very simple streaming parser/tokenizer for [CSS 2.1](https://www.w3.org/TR/CSS21/)
6data format without heap allocations. Supports CSS nesting for nested selectors and @-rules.
7
8Since it's very simple we will start with limitations:
9
10## Limitations
11- The ident token must be ASCII only.
12
13  CSS like `#аттр { имя:значение }` will lead to a parsing error.
14- Property values are not parsed.
15
16  In CSS like `* { width: 5px }` you will get `width` property with `5px` values as a string.
17- Attribute selector rule is not parsed.
18
19  `[foo~="warning"]` will be parsed as `Token::AttributeSelector("foo~=\"warning\"")`.
20- There are no data validation.
21
22  - Pseudo-class tokens can contain any text, language pseudo-class can contain any text or even none.
23  - Declarations can contain any kind of names and values.
24- All comments will be ignored.
25
26  They didn't have it's own `Token` item.
27- CDO/CDC comments are not supported.
28- Parser is case sensitive. All keywords should be lowercase.
29- Unicode escape, like `\26`, is not supported.
30- No spec-defined error handling.
31
32  If something will go wrong you will get an error. Parser will not recover an invalid input.
33  [Details](https://www.w3.org/TR/CSS21/syndata.html#rule-sets).
34
35## Where to use
36`simplecss` can be useful for parsing a very simple or predefined CSS.
37
38It's tiny, dependency free and pretty fast.
39
40## Examples
41
42Simple
43
44```text
45* { color : red }
46| | |           ||
47| | |           |+- Token::EndOfStream
48| | |           +- Token::BlockEnd
49| | +- Token::Declaration("color", "red")
50| +- Token::BlockStart
51+- Token::UniversalSelector
52```
53
54Complex
55
56```text
57div#x:first-letter em[id] + .hh1 { color : red }
58|  | |            || |    | |    | |           ||
59|  | |            || |    | |    | |           |+- Token::EndOfStream
60|  | |            || |    | |    | |           +- Token::BlockEnd
61|  | |            || |    | |    | +- Token::Declaration("color", "red")
62|  | |            || |    | |    +- Token::BlockStart
63|  | |            || |    | +- Token::ClassSelector("hh1")
64|  | |            || |    +- Token::Combinator(Combinator::Plus)
65|  | |            || +- Token::AttributeSelector("id")
66|  | |            |+- Token::TypeSelector("em")
67|  | |            +- Token::Combinator(Combinator::Space)
68|  | +- Token::PseudoClass("first-letter")
69|  +- Token::IdSelector("x")
70+- Token::TypeSelector("div")
71```
72*/
73
74#![forbid(unsafe_code)]
75#![warn(missing_docs)]
76
77pub use error::{Error, ErrorPos};
78pub use tokenizer::{Tokenizer, Token, Combinator};
79
80mod error;
81mod stream;
82mod tokenizer;