Crate cssparser [−] [src]
Implementation of CSS Syntax Module Level 3 for Rust.
Input
Everything is based on Parser objects, which borrow a &str input.
If you have bytes (from a file, the network, or something),
see the decode_stylesheet_bytes function.
Conventions for parsing functions
- Take (at least) a
input: &mut cssparser::Parserparameter - Return
Result<_, ()> - When returning
Ok(_), the function must have consume exactly the amount of input that represents the parsed value. - When returning
Err(()), any amount of input may have been consumed.
As a consequence, when calling another parsing function, either:
- Any
Err(())return value must be propagated. This happens by definition for tail calls, and can otherwise be done with thetry!macro. - Or the call must be wrapped in a
Parser::trycall.trytakes a closure that takes aParserand returns aResult, calls it once, and returns itself that same result. If the result isErr, it restores the position inside the input to the one saved before calling the closure.
Examples:
// 'none' | <image> fn parse_background_image(context: &ParserContext, input: &mut Parser) -> Result<Option<Image>, ()> { if input.try(|input| input.expect_ident_matching("none")).is_ok() { Ok(None) } else { Image::parse(context, input).map(Some) // tail call } }
// [ <length> | <percentage> ] [ <length> | <percentage> ]? fn parse_border_spacing(_context: &ParserContext, input: &mut Parser) -> Result<(LengthOrPercentage, LengthOrPercentage), ()> { let first = try!(LengthOrPercentage::parse); let second = input.try(LengthOrPercentage::parse).unwrap_or(first); (first, second) }
Modules
| Delimiter |
|
Macros
| match_ignore_ascii_case! |
This macro is equivalent to a |
Structs
| CssStringWriter |
A |
| DeclarationListParser |
Provides an iterator for declaration list parsing. |
| Delimiters |
A set of characters, to be used with the |
| NumericValue |
The numeric value of |
| Parser |
A CSS parser that borrows its |
| PercentageValue |
The numeric value of |
| RGBA |
A color with red, green, blue, and alpha components. |
| RuleListParser |
Provides an iterator for rule list parsing. |
| SourceLocation |
The line and column number for a given position within the input. |
| SourcePosition |
A capture of the internal state of a |
| TokenSerializationType |
A category of token. See the |
Enums
| AtRuleType |
The return value for |
| Color |
A |
| Token |
One of the pieces the CSS input is broken into. |
Traits
| AtRuleParser |
A trait to provide various parsing of at-rules. |
| DeclarationParser |
A trait to provide various parsing of declaration values. |
| QualifiedRuleParser |
A trait to provide various parsing of qualified rules. |
| ToCss |
Trait for things the can serialize themselves in CSS syntax. |
Functions
| decode_stylesheet_bytes |
Determine the character encoding of a CSS stylesheet and decode it. |
| parse_color_keyword |
Return the named color with the given name. |
| parse_important |
Parse |
| parse_nth |
Parse the An+B notation, as found in the |
| parse_one_declaration |
Parse a single declaration, such as an |
| parse_one_rule |
Parse a single rule, such as for CSSOM’s |
| serialize_identifier |
Write a CSS identifier, escaping characters as necessary. |
| serialize_string |
Write a double-quoted CSS string token, escaping content as necessary. |