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