Skip to main content

cssparser/
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#![crate_name = "cssparser"]
6#![crate_type = "rlib"]
7#![cfg_attr(feature = "bench", feature(test))]
8#![deny(missing_docs)]
9
10/*!
11
12Implementation of [CSS Syntax Module Level 3](https://drafts.csswg.org/css-syntax/) for Rust.
13
14# Input
15
16Everything is based on `Parser` objects, which borrow a `&str` input.
17If you have bytes (from a file, the network, or something)
18and want to support character encodings other than UTF-8,
19see the `stylesheet_encoding` function,
20which can be used together with rust-encoding or encoding-rs.
21
22# Conventions for parsing functions
23
24* Take (at least) a `input: &mut cssparser::Parser` parameter
25* Return `Result<_, ()>`
26* When returning `Ok(_)`,
27  the function must have consumed exactly the amount of input that represents the parsed value.
28* When returning `Err(())`, any amount of input may have been consumed.
29
30As a consequence, when calling another parsing function, either:
31
32* Any `Err(())` return value must be propagated.
33  This happens by definition for tail calls,
34  and can otherwise be done with the `?` operator.
35* Or the call must be wrapped in a `Parser::try` call.
36  `try` takes a closure that takes a `Parser` and returns a `Result`,
37  calls it once,
38  and returns itself that same result.
39  If the result is `Err`,
40  it restores the position inside the input to the one saved before calling the closure.
41
42Examples:
43
44```rust,ignore
45// 'none' | <image>
46fn parse_background_image(context: &ParserContext, input: &mut Parser)
47                                    -> Result<Option<Image>, ()> {
48    if input.try_parse(|input| input.expect_ident_matching("none")).is_ok() {
49        Ok(None)
50    } else {
51        Image::parse(context, input).map(Some)  // tail call
52    }
53}
54```
55
56```rust,ignore
57// [ <length> | <percentage> ] [ <length> | <percentage> ]?
58fn parse_border_spacing(_context: &ParserContext, input: &mut Parser)
59                          -> Result<(LengthOrPercentage, LengthOrPercentage), ()> {
60    let first = LengthOrPercentage::parse?;
61    let second = input.try_parse(LengthOrPercentage::parse).unwrap_or(first);
62    (first, second)
63}
64```
65
66*/
67
68#![recursion_limit = "200"] // For color::parse_color_keyword
69
70pub use crate::cow_rc_str::CowRcStr;
71pub use crate::from_bytes::{EncodingSupport, stylesheet_encoding};
72#[doc(hidden)]
73pub use crate::macros::_cssparser_internal_to_lowercase;
74pub use crate::nth::parse_nth;
75pub use crate::parser::{BasicParseError, BasicParseErrorKind, ParseError, ParseErrorKind};
76pub use crate::parser::{Delimiter, Delimiters, Parser, ParserState};
77pub use crate::rules_and_declarations::{AtRuleParser, QualifiedRuleParser};
78pub use crate::rules_and_declarations::{DeclarationParser, RuleBodyItemParser, RuleBodyParser};
79pub use crate::rules_and_declarations::{StyleSheetParser, parse_one_rule};
80pub use crate::rules_and_declarations::{parse_important, parse_one_declaration};
81pub use crate::serializer::{CssStringWriter, ToCss, TokenSerializationType};
82pub use crate::serializer::{serialize_identifier, serialize_name, serialize_string};
83pub use crate::tokenizer::{SourceLocation, SourcePosition, Token};
84pub use crate::unicode_range::UnicodeRange;
85
86#[cfg(feature = "fast_match_byte")]
87pub use cssparser_macros::match_byte;
88
89#[cfg(not(feature = "fast_match_byte"))]
90#[macro_use]
91mod mac {
92    /// Expand a TokenStream corresponding to the `match_byte` macro.
93    ///
94    /// ## Example
95    ///
96    /// ```rust,ignore
97    /// match_byte! { tokenizer.next_byte_unchecked(),
98    ///     b'a'..b'z' => { ... }
99    ///     b'0'..b'9' => { ... }
100    ///     b'\n' | b'\\' => { ... }
101    ///     foo => { ... }
102    ///  }
103    ///  ```
104    ///
105    #[macro_export]
106    macro_rules! match_byte {
107      ($value:expr, $($rest:tt)* ) => {
108          match $value {
109              $(
110                  $rest
111              )+
112          }
113      };
114  }
115}
116
117// Re-exporting phf here means that the crate using the ascii_case_insensitive_phf_map macro do
118// do not have to depend on phf directly.
119#[cfg(feature = "fast_match_color")]
120#[doc(hidden)]
121pub use phf as _cssparser_internal_phf;
122
123#[macro_use]
124mod macros;
125
126mod rules_and_declarations;
127mod tokenizer;
128
129pub mod color;
130mod cow_rc_str;
131mod from_bytes;
132mod nth;
133mod parser;
134mod serializer;
135mod unicode_range;
136
137#[cfg(all(test, target_pointer_width = "64"))]
138mod size_of_tests;
139#[cfg(test)]
140mod tests;