Expand description
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)
and want to support character encodings other than UTF-8,
see the stylesheet_encoding function,
which can be used together with rust-encoding or encoding-rs.
§Conventions for parsing functions
- Take (at least) a
input: &mut cssparser::Parserparameter - Return
Result<_, ()> - When returning
Ok(_), the function must have consumed 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 the?operator. - 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_parse(|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 = LengthOrPercentage::parse?;
let second = input.try_parse(LengthOrPercentage::parse).unwrap_or(first);
(first, second)
}Modules§
- Delimiter
Delimitersconstants.- color
- General color-parsing utilities, independent on the specific color storage and parsing implementation.
Macros§
- _cssparser_
internal_ max_ len - ascii_
case_ insensitive_ phf_ map - Define a function
$name(&str) -> Option<&'static $ValueType> - match_
byte - Expand a TokenStream corresponding to the
match_bytemacro. - match_
ignore_ ascii_ case - Expands to a
matchexpression with string patterns, matching case-insensitively in the ASCII range.
Structs§
- Basic
Parse Error - The fundamental parsing errors that can be triggered by built-in parsing routines.
- CowRc
Str - A string that is either shared (heap-allocated and reference-counted) or borrowed.
- CssString
Writer - A
fmt::Writeadapter that escapes text for writing as a double-quoted CSS string. Quotes are not included. - Delimiters
- A set of characters, to be used with the
Parser::parse_until*methods. - Parse
Error - Extensible parse errors that can be encountered by client parsing implementations.
- Parser
- A CSS parser that borrows its
&strinput, yieldsTokens, and keeps track of nested blocks and functions. - Parser
Input - The owned input for a parser.
- Parser
State - A capture of the internal state of a
Parser(including the position within the input), obtained from theParser::positionmethod. - Rule
Body Parser - Provides an iterator for rule bodies and declaration lists.
- Source
Location - The line and column number for a given position within the input.
- Source
Position - A position from the start of the input, counted in UTF-8 bytes.
- Style
Sheet Parser - Provides an iterator for rule list parsing at the top-level of a stylesheet.
- Unicode
Range - One contiguous range of code points.
Enums§
- Basic
Parse Error Kind - Details about a
BasicParseError - Parse
Error Kind - Details of a
ParseError - Token
- One of the pieces the CSS input is broken into.
- Token
Serialization Type - A category of token. See the
needs_separator_when_beforemethod.
Traits§
- AtRule
Parser - A trait to provide various parsing of at-rules.
- Declaration
Parser - A trait to provide various parsing of declaration values.
- Encoding
Support - Abstraction for avoiding a dependency from cssparser to an encoding library
- Qualified
Rule Parser - A trait to provide various parsing of qualified rules.
- Rule
Body Item Parser - A parser for a rule body item.
- ToCss
- Trait for things the can serialize themselves in CSS syntax.
Functions§
- parse_
important - Parse
!important. - parse_
nth - Parse the An+B notation, as found in the
:nth-child()selector. The input is typically the arguments of a function, in which case the caller needs to check if the arguments’ parser is exhausted. ReturnOk((A, B)), or anErr(..)for a syntax error. - parse_
one_ declaration - Parse a single declaration, such as an
( /* ... */ )parenthesis in an@supportsprelude. - parse_
one_ rule - Parse a single rule, such as for CSSOM’s
CSSStyleSheet.insertRule. - serialize_
identifier - Write a CSS identifier, escaping characters as necessary.
- serialize_
name - Write a CSS name, like a custom property name.
- serialize_
string - Write a double-quoted CSS string token, escaping content as necessary.
- stylesheet_
encoding - Determine the character encoding of a CSS stylesheet.