Donner 0.5.1
Embeddable browser-grade SVG2 engine
Loading...
Searching...
No Matches
donner::css Namespace Reference

Donner CSS library, a standalone composable CSS parser. More...

Namespaces

namespace  parser
 Parsers for various CSS data types, such as the top-level StylesheetParser and SelectorParser, as well as internal details such as ColorParser.

Classes

struct  RGBA
 Represents as 32-bit RGBA color, with each component in the range [0, 255]. More...
struct  HSLA
 Represents an HSLA color. More...
struct  Color
 Represents a CSS color value, like a RGBA color from a #rrggbb or #rgb hex value, or the currentcolor keyword. More...
struct  Function
 A CSS function, such as rgb(255, 0, 0), parsed into a function name and a list of parameter values. More...
struct  SimpleBlock
 A CSS simple block, such as a rule block or a parenthesized expression. More...
struct  ComponentValue
 A CSS component value, which is either a token, or a parsed function or block. More...
class  CSS
 Public API for parsing CSS. More...
struct  Declaration
 A declaration is a CSS name/value pair, such as color: red;. More...
struct  DeclarationOrAtRule
 Return value of parsers that may return either a declaration or an AtRule, specifically donner::css::parser::DeclarationListParser::Parse. More...
struct  AnbValue
 An+B microsyntax value, which is parsed by parser::AnbMicrosyntaxParser. More...
struct  FontFaceSource
 A single entry listed in src:—either a local face, a URL, or inline data. More...
struct  FontFace
 In-memory representation of a single @font-face rule. More...
struct  AtRule
 Rules starting with an @ are called At-Rules, and are used to define CSS features such as @media, @font-face, @keyframes, etc. More...
struct  QualifiedRule
 A QualifiedRule has a list of component values and a block, this is the intermediate representation of a stylesheet rule. More...
struct  InvalidRule
 InvalidRule is used to represent a rule which could not be parsed, such as an invalid at-rule. More...
struct  Rule
 Holds a CSS rule which can either be a standard QualifiedRule, an AtRule, or an InvalidRule if there was a parse error. More...
struct  Selector
 A top-level Selector, which is a list of ComplexSelector. More...
struct  AttributeSelector
 Selectors which match against element attributes, such as a[href^="https://"] or h1[title]. More...
struct  ClassSelector
 Selector which match the element's class attribute, for example .foo matches an element with class foo. More...
struct  SelectorMatchResult
 Returned by Selector::matches to indicate whether the selector matched, and if so, the specificity of the match. More...
struct  ComplexSelector
 A complex selector is a sequence of one or more compound selectors, separated by combinators. More...
struct  CompoundSelector
 A compound selector is a sequence of simple selectors, which represents a set of conditions that are combined to match a single element. More...
struct  IdSelector
 Selector which match the element's id attribute, for example #foo matches an element with an id="foo" attribute. More...
struct  PseudoClassSelector
 Selectors which start with one colon, e.g. More...
struct  PseudoElementSelector
 Selectors which start with two colons are called pseudo-elements, e.g. More...
struct  SelectorMatchOptions
 Options for matching a selector against an element. More...
struct  TypeSelector
 Selector which matches the element type, e.g. More...
class  Specificity
 A CSS specificity value (id, class, type), as defined in https://www.w3.org/TR/selectors-4/#specificity-rules, which is used during cascading to determine which style takes precedence. More...
struct  SelectorRule
 A rule in a stylesheet, which consists of a selector and a list of declarations. More...
class  Stylesheet
 A CSS stylesheet, which is a list of rules. More...
struct  Token
 A CSS token, which are created as a first step when parsing a CSS string. More...
struct  WqName
 A CSS qualified name, which is a name optionally associated with a namespace. More...

Typedefs

using TokenIndex = size_t
 Type of the token unique identifier, which is returned by Token::tokenIndex() and Token::indexOf<T>().

Enumerations

enum class  AttrMatcher {
  Includes ,
  DashMatch ,
  PrefixMatch ,
  SuffixMatch ,
  SubstringMatch ,
  Eq
}
 For attribute selectors, different match modes are available, which are specified by this enum. More...
enum class  Combinator {
  Descendant ,
  Child ,
  NextSibling ,
  SubsequentSibling ,
  Column
}
 Between two compound selectors, there can be a combinator, which specifies how the two elements are associated in the tree. More...
enum class  NumberType {
  Integer ,
  Number
}
 Indicates if a number is an integer or a floating point number, used for number-containing tokens such as Token::Number and Token::Dimension. More...

Functions

constexpr Color RgbHex (uint32_t value)
 Construct a donner::css::Color from a 24-bit RGB hex literal.
constexpr Color RgbaHex (uint32_t value)
 Construct a donner::css::Color from a 32-bit RGBA hex literal.
std::string mergeStyleDeclarations (std::span< const Declaration > existing, std::span< const Declaration > updates)
 Merge two sets of CSS declarations, where updates override declarations in existing that have the same property name (case-insensitive).
std::ostream & operator<< (std::ostream &os, AttrMatcher matcher)
 Ostream output operator.
std::ostream & operator<< (std::ostream &os, Combinator combinator)
 Ostream output operator.

Detailed Description

Donner CSS library, a standalone composable CSS parser.

This library is designed to be used in other projects, and provides building blocks to add CSS parsing to any application. This is used by Donner itself to parse SVG stylesheets, but it can be used for HTML-based CSS as well.

This library includes support for parsing rules and declarations, as well as matching selectors against a DOM tree.

To get started, parse a Stylesheet using CSS::ParseStylesheet, and then use the returned Stylesheet object to match against a DOM tree:

auto stylesheet = CSS::ParseStylesheet("svg { fill: red; }", warnings);
for (const css::SelectorRule& rule : stylesheet.rules()) {
if (css::SelectorMatchResult match = rule.selector.matches(domNode)) {
applyDeclaration(rule.declarations, match.specificity);
}
}
Collects parse warnings during parsing.
Definition ParseWarningSink.h:28
static Stylesheet ParseStylesheet(std::string_view str, ParseWarningSink &warningSink)
Parse a CSS stylesheet into a list of selectors and their associated declarations,...
Returned by Selector::matches to indicate whether the selector matched, and if so,...
Definition ComplexSelector.h:29
A rule in a stylesheet, which consists of a selector and a list of declarations.
Definition Stylesheet.h:25

Class Documentation

◆ donner::css::FontFace

struct donner::css::FontFace

In-memory representation of a single @font-face rule.

Collaboration diagram for donner::css::FontFace:
[legend]
Class Members
RcString familyName font-family descriptor
int fontStretch = 5 font-stretch descriptor (1-9, 5=normal, matching FontStretch enum)
int fontStyle = 0 font-style descriptor (0=normal, 1=italic, 2=oblique)
int fontWeight = 400 font-weight descriptor (100-900, 400=normal, 700=bold)
vector< FontFaceSource > sources ordered src list

◆ donner::css::SelectorMatchOptions

struct donner::css::SelectorMatchOptions
template<ElementLike T>
struct donner::css::SelectorMatchOptions< T >

Options for matching a selector against an element.

This is used to pass additional information to the matching algorithm, such as the element to match against for relative queries.

Template Parameters
TA type that fulfills the ElementLike concept, matching the Selector::matches method.
Collaboration diagram for donner::css::SelectorMatchOptions< T >:
[legend]
Class Members
const T * relativeToElement Enables relative querying and uses this element as the reference point. For example, > div will match div that is a child of relativeToElement.
const T * scopeElement = nullptr Element to match against :scope queries. Cannot be matched directly, but can be used for relative matching.

Enumeration Type Documentation

◆ AttrMatcher

enum class donner::css::AttrMatcher
strong

For attribute selectors, different match modes are available, which are specified by this enum.

See https://www.w3.org/TR/selectors-4/#attribute-selectors for the full definition.

These are used within square brackets on the selector list, such as a[href^="https://"] or h1[title], and AttrMatcher represents the separator between the attribute name and string, such as ^= or =.

Enumerator
Includes 

"~=", matches if the attribute value is a whitespace-separated list of values, and one of them exactly matches the matcher value.

DashMatch 

"|=", matches if the attribute value either exactly matches, or begins with the value immediately followed by a dash ("-").

PrefixMatch 

"^=", matches if the attribute value begins with the matcher value.

SuffixMatch 

"$=", matches if the attribute value ends with the matcher value.

SubstringMatch 

"*=", matches if the attribute value contains the matcher value.

Eq 

"=", matches if the attribute value exactly matches the matcher value.

◆ Combinator

enum class donner::css::Combinator
strong

Between two compound selectors, there can be a combinator, which specifies how the two elements are associated in the tree.

By default, a space between compound selectors is a descendant combinator, e.g. div span is a Descendant combinator, while div > span is a Child combinator.

Enumerator
Descendant 

Space-separated, finds descendants in the tree.

Child 

'>', finds direct children in the tree.

NextSibling 

'+', finds the next sibling in the tree.

SubsequentSibling 

'~', finds all subsequent siblings in the tree.

Column 

'||', finds the next column in the tree. Note that this is a new feature in CSS Selectors Level 4, but isn't applicable to SVG.

◆ NumberType

enum class donner::css::NumberType
strong

Indicates if a number is an integer or a floating point number, used for number-containing tokens such as Token::Number and Token::Dimension.

Enumerator
Integer 

Integer number (no decimal point).

Number 

Floating point number.

Function Documentation

◆ mergeStyleDeclarations()

std::string donner::css::mergeStyleDeclarations ( std::span< const Declaration > existing,
std::span< const Declaration > updates )

Merge two sets of CSS declarations, where updates override declarations in existing that have the same property name (case-insensitive).

Unrelated existing declarations are preserved. Duplicate property names within updates are deduplicated, keeping the last occurrence.

Parameters
existingExisting declarations.
updatesUpdated declarations to apply.
Returns
Merged style string with declarations separated by "; ".

◆ operator<<()

std::ostream & donner::css::operator<< ( std::ostream & os,
Combinator combinator )
inline

Ostream output operator.

Outputs the combinator character, e.g. ' ', '>', '+', '~' or '||'.

Parameters
osThe output stream.
combinatorThe combinator.

◆ RgbaHex()

Color donner::css::RgbaHex ( uint32_t value)
constexpr

Construct a donner::css::Color from a 32-bit RGBA hex literal.

For example, for 50% opacity red:

const Color red = RgbaHex(0xFF000080);
constexpr Color RgbaHex(uint32_t value)
Construct a donner::css::Color from a 32-bit RGBA hex literal.
Definition Color.h:256
Represents a CSS color value, like a RGBA color from a #rrggbb or #rgb hex value, or the currentcolor...
Definition Color.h:113
Parameters
valueInteger representation of the color value (0xRRGGBBAA).

◆ RgbHex()

Color donner::css::RgbHex ( uint32_t value)
constexpr

Construct a donner::css::Color from a 24-bit RGB hex literal.

For example:

const Color red = RgbHex(0xFF0000);
constexpr Color RgbHex(uint32_t value)
Construct a donner::css::Color from a 24-bit RGB hex literal.
Definition Color.h:242
Parameters
valueInteger representation of the color value (0xRRGGBB).