Expand description
Representation of CSS types, and the CSS parsing and matching engine.
§Terminology
Consider a CSS stylesheet like this:
@import url("another.css");
foo, .bar {
fill: red;
stroke: green;
}
#baz { stroke-width: 42; }The example contains three rules, the first one is an *at-rule, the other two are qualified rules.
Each rule is made of two parts, a prelude and an optional block
The prelude is the part until the first { or until ;, depending on
whether a block is present. The block is the part between curly braces.
Let’s look at each rule:
@import is an at-rule. This rule has a prelude, but no block.
There are other at-rules like @media and some of them may have a block,
but librsvg doesn’t support those yet.
The prelude of the following rule is foo, .bar.
It is a selector list with two selectors, one for
foo elements and one for elements that have the bar class.
The content of the block between {} for a qualified rule is a
declaration list. The block of the first qualified rule contains two
declarations, one for the fill property and one for the
stroke property.
After the first qualified rule, we have a second qualified rule with
a single selector for the #baz id, with a single declaration for the
stroke-width property.
§Helper crates we use
-
cssparsercrate as a CSS tokenizer, and some utilities to parse CSS rules and declarations. -
selectorscrate for the representation of selectors and selector lists, and for the matching engine.
Both crates provide very generic implementations of their concepts, and expect the caller to provide implementations of various traits, and to provide types that represent certain things.
For example, cssparser expects one to provide representations of
the following types:
-
A parsed CSS rule. For
fill: blue;we haveParsedProperty::Fill(...). -
A parsed selector list; we use
SelectorListfrom theselectorscrate.
In turn, the selectors crate needs a way to navigate and examine
one’s implementation of an element tree. We provide impl selectors::Element for RsvgElement for this. This implementation
has methods like “does this element have the id #foo”, or “give
me the next sibling element”.
Finally, the matching engine ties all of this together with
matches_selector(). This takes an opaque representation of an
element, plus a selector, and returns a bool. We iterate through
the rules in the stylesheets and gather the matches; then sort the
matches by specificity and apply the result to each element.
Structs§
- Attribute
Value - Wrapper for attribute values.
- Decl
Parser - Dummy struct required to use
cssparser::DeclarationListParser - Declaration
- A parsed CSS declaration
- Identifier
- Wrapper for identifier values.
- Local
Name - Wrapper for local names.
- Match 🔒
- A match during the selector matching process
- Namespace
Prefix - Wrapper for namespace prefixes.
- Pseudo
Element - Dummy type required by the SelectorImpl trait
- Qualified
Rule - A CSS qualified rule (or ruleset)
- Rsvg
Element - Newtype wrapper around
Nodeso we can implement [selectors::Element] for it. - Rule
Parser - Struct to implement cssparser::QualifiedRuleParser and cssparser::AtRuleParser
- Selector
- Holds all the types for the SelectorImpl trait
- Stylesheet
- A parsed CSS stylesheet.
Enums§
- AtRule
- A CSS at-rule (or ruleset)
- AtRule
Prelude - Prelude of at-rule used in the AtRuleParser.
- NonTS
Pseudo Class - Dummy type required by the SelectorImpl trait.
- Origin
- Origin for a stylesheet, per CSS 2.2.
- Parse
Error Kind - Errors from the CSS parsing process
- Rule
- A CSS rule (or ruleset)
- Rule
Body Item - This enum represents the fact that a rule body can be either a declaration or a nested rule.
Functions§
- cascade
- Runs the CSS cascade on the specified tree from all the stylesheets