Donner
C++20 SVG rendering library
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  AnbValue
 An+B microsyntax value, which is parsed by parser::AnbMicrosyntaxParser. 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  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  Color
 Represents a CSS color value, like a RGBA color from a #rrggbb or #rgb hex value, or the currentcolor keyword. More...
 
struct  ComplexSelector
 A complex selector is a sequence of one or more compound selectors, separated by combinators. More...
 
class  ComplexSelectorIsImpl
 
struct  ComponentValue
 A CSS component value, which is either a token, or a parsed function or block. 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...
 
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  Function
 A CSS function, such as rgb(255, 0, 0), parsed into a function name and a list of parameter values. More...
 
struct  HSLA
 Represents an HSLA color. More...
 
struct  IdSelector
 Selector which match the element's id attribute, for example #foo matches an element with an id="foo" attribute. More...
 
struct  InvalidRule
 InvalidRule is used to represent a rule which could not be parsed, such as an invalid at-rule. More...
 
class  MultiSelectorMatcher
 
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  QualifiedRule
 A QualifiedRule has a list of component values and a block, this is the intermediate representation of a stylesheet rule. More...
 
struct  RGBA
 Represents as 32-bit RGBA color, with each component in the range [0, 255]. 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  SelectorMatchOptions
 Options for matching a selector against an element. More...
 
struct  SelectorMatchResult
 Returned by Selector::matches to indicate whether the selector matched, and if so, the specificity of the match. More...
 
struct  SelectorRule
 A rule in a stylesheet, which consists of a selector and a list of declarations. More...
 
struct  SimpleBlock
 A CSS simple block, such as a rule block or a parenthesized expression. 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...
 
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  TypeSelector
 Selector which matches the element type, e.g. 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  MatcherOptions {
  CaseSensitive ,
  CaseInsensitive ,
  Default = CaseSensitive
}
 
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

std::ostream & operator<< (std::ostream &os, const RGBA &color)
 
std::ostream & operator<< (std::ostream &os, const HSLA &color)
 
std::ostream & operator<< (std::ostream &os, const Color &color)
 
std::ostream & operator<< (std::ostream &os, const Function &func)
 
std::ostream & operator<< (std::ostream &os, const SimpleBlock &block)
 
std::ostream & operator<< (std::ostream &os, const Declaration &declaration)
 
 MATCHER_P (TokenIsImpl, token, "")
 
template<typename T , typename... Args>
auto TokenIs (Args... args)
 Given a Token or a variant containing a token, matches if the token is equal, ignoring the offset.
 
template<typename... Args>
auto TokenIsIdent (Args... args)
 
template<typename... Args>
auto TokenIsFunction (Args... args)
 
template<typename... Args>
auto TokenIsAtKeyword (Args... args)
 
template<typename... Args>
auto TokenIsHash (Args... args)
 
template<typename... Args>
auto TokenIsString (Args... args)
 
template<typename... Args>
auto TokenIsBadString (Args... args)
 
template<typename... Args>
auto TokenIsUrl (Args... args)
 
template<typename... Args>
auto TokenIsBadUrl (Args... args)
 
template<typename... Args>
auto TokenIsDelim (Args... args)
 
template<typename... Args>
auto TokenIsNumber (Args... args)
 
template<typename... Args>
auto TokenIsPercentage (Args... args)
 
template<typename... Args>
auto TokenIsDimension (Args... args)
 
template<typename... Args>
auto TokenIsWhitespace (Args... args)
 
template<typename... Args>
auto TokenIsCDO (Args... args)
 
template<typename... Args>
auto TokenIsCDC (Args... args)
 
template<typename... Args>
auto TokenIsColon (Args... args)
 
template<typename... Args>
auto TokenIsSemicolon (Args... args)
 
template<typename... Args>
auto TokenIsComma (Args... args)
 
template<typename... Args>
auto TokenIsSquareBracket (Args... args)
 
template<typename... Args>
auto TokenIsParenthesis (Args... args)
 
template<typename... Args>
auto TokenIsCurlyBracket (Args... args)
 
template<typename... Args>
auto TokenIsCloseSquareBracket (Args... args)
 
template<typename... Args>
auto TokenIsCloseParenthesis (Args... args)
 
template<typename... Args>
auto TokenIsCloseCurlyBracket (Args... args)
 
template<typename... Args>
auto TokenIsErrorToken (Args... args)
 
template<typename... Args>
auto TokenIsEofToken (Args... args)
 
 MATCHER_P3 (DeclarationIsImpl, nameMatcher, valuesMatcher, importantMatcher, "")
 
template<typename NameMatcher , typename ValuesMatcher , typename ImportantMatcher >
auto DeclarationIs (NameMatcher nameMatcher, ValuesMatcher valuesMatcher, ImportantMatcher importantMatcher)
 
template<typename NameMatcher , typename ValuesMatcher >
auto DeclarationIs (NameMatcher nameMatcher, ValuesMatcher valuesMatcher)
 
 MATCHER_P3 (AtRuleIsImpl, nameMatcher, preludeMatcher, blockMatcher, "")
 
template<typename NameMatcher , typename PreludeMatcher , typename BlockMatcher >
auto AtRuleIs (NameMatcher nameMatcher, PreludeMatcher preludeMatcher, BlockMatcher blockMatcher)
 
template<typename NameMatcher , typename PreludeMatcher >
auto AtRuleIs (NameMatcher nameMatcher, PreludeMatcher preludeMatcher)
 
 MATCHER (InvalidRuleTypeImpl, "")
 
auto InvalidRuleType ()
 
 MATCHER_P2 (FunctionIs, nameMatcher, valuesMatcher, "")
 
 MATCHER_P2 (SimpleBlockIs, associatedTokenMatcher, valuesMatcher, "")
 
template<typename ValuesMatcher >
auto SimpleBlockIsCurly (ValuesMatcher valuesMatcher)
 
template<typename ValuesMatcher >
auto SimpleBlockIsSquare (ValuesMatcher valuesMatcher)
 
template<typename ValuesMatcher >
auto SimpleBlockIsParenthesis (ValuesMatcher valuesMatcher)
 
 MATCHER_P2 (QualifiedRuleIsImpl, preludeMatcher, blockMatcher, "")
 
template<typename PreludeMatcher , typename BlockMatcher >
auto QualifiedRuleIs (PreludeMatcher preludeMatcher, BlockMatcher blockMatcher)
 
std::ostream & operator<< (std::ostream &os, const AtRule &rule)
 
std::ostream & operator<< (std::ostream &os, const Selector &obj)
 Ostream output operator for Selector, prints a debug representation of the selector, e.g. Selector(div, .class, #id).
 
std::ostream & operator<< (std::ostream &os, AttrMatcher matcher)
 Ostream output operator.
 
std::ostream & operator<< (std::ostream &os, const ComplexSelector &obj)
 Ostream output operator for ComplexSelector, outputs debug strings e.g. "ComplexSelector(CompoundSelector(TypeSelector(name)))".
 
std::ostream & operator<< (std::ostream &os, Combinator combinator)
 Ostream output operator.
 
std::ostream & operator<< (std::ostream &os, const PseudoClassSelector &obj)
 Ostream output operator for PseudoClassSelector, outputs a debug string e.g. PseudoClassSelector(after).
 
template<typename... Args>
auto SelectorsAre (const Args &... matchers)
 Match a Selector against an array of ComplexSelectors, used like:
 
Specificity computeSpecificity (std::string_view str)
 Get the specificity of the Selector string (id, class, type).
 
template<typename... Args>
auto ComplexSelectorIs (const Args &... matchers)
 Match either a single ComplexSelector, or a Selector containing a single ComplexSelector.
 
 MATCHER_P (SpecificityIs, specificity, "")
 Checks if either a Selector, ComplexSelector, or Specificity has the given specificity.
 
template<typename... Args>
auto EntryIs (Combinator combinator, const Args &... matchers)
 
template<typename... Args>
auto EntryIs (const Args &... matchers)
 
 MATCHER_P2 (PseudoElementSelectorIsImpl, ident, argsMatcher, "")
 
auto PseudoElementSelectorIs (const char *ident)
 
template<typename ArgsMatcher >
auto PseudoElementSelectorIs (const char *ident, ArgsMatcher argsMatcher)
 
 MATCHER_P2 (TypeSelectorIsImpl, ns, name, "")
 
auto TypeSelectorIs (const char *name)
 
auto TypeSelectorIs (const char *ns, const char *name)
 
 MATCHER_P (IdSelectorIs, name, "")
 
 MATCHER_P (ClassSelectorIs, name, "")
 
 MATCHER_P2 (PseudoClassSelectorIsImpl, ident, argsMatcher, "")
 
auto PseudoClassSelectorIs (const char *ident)
 
template<typename ArgsMatcher >
auto PseudoClassSelectorIs (const char *ident, ArgsMatcher argsMatcher)
 
 MATCHER_P3 (AttributeSelectorIsImpl, ns, name, matcherMatcher, "")
 
auto AttributeSelectorIs (const char *name)
 
auto AttributeSelectorIs (const char *ns, const char *name)
 
template<typename MatcherMatcher >
auto AttributeSelectorIs (const char *name, MatcherMatcher matcherMatcher)
 
template<typename MatcherMatcher >
auto AttributeSelectorIs (const char *ns, const char *name, MatcherMatcher matcherMatcher)
 
auto MatcherIs (AttrMatcher op, const char *value, MatcherOptions options=MatcherOptions::Default)
 

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; }");
for (const css::SelectorRule& rule : stylesheet.rules()) {
if (css::SelectorMatchResult match = rule.selector.matches(domNode)) {
applyDeclaration(rule.declarations, match.specificity);
}
}
static Stylesheet ParseStylesheet(std::string_view str)
Parse a CSS stylesheet into a list of selectors and their associated declarations,...
Definition CSS.cc:9
Returned by Selector::matches to indicate whether the selector matched, and if so,...
Definition ComplexSelector.h:28
A rule in a stylesheet, which consists of a selector and a list of declarations.
Definition Stylesheet.h:26

Class Documentation

◆ 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 `TokenNumber` and Token::Dimension.

Enumerator
Integer 

Integer number (no decimal point).

Number 

Floating point number.

Function Documentation

◆ ComplexSelectorIs()

template<typename... Args>
auto donner::css::ComplexSelectorIs ( const Args &... matchers)

Match either a single ComplexSelector, or a Selector containing a single ComplexSelector.

Example

EXPECT_THAT(selector, ComplexSelectorIs(
EntryIs(ClassSelectorIs("b")),
auto ComplexSelectorIs(const Args &... matchers)
Match either a single ComplexSelector, or a Selector containing a single ComplexSelector.
Definition SelectorTestUtils.h:195
@ Descendant
Space-separated, finds descendants in the tree.
Selector which matches the element type, e.g.
Definition TypeSelector.h:24
Parameters
matchers...Matchers against a ComplexSelector::Entry.

◆ computeSpecificity()

Specificity donner::css::computeSpecificity ( std::string_view str)
inline

Get the specificity of the Selector string (id, class, type).

For example "div.class" would return (0, 1, 1). If there is a parse error, returns (0, 0, 0).

Parameters
strSelector string, for example "div.class"

◆ MATCHER_P()

donner::css::MATCHER_P ( SpecificityIs ,
specificity ,
""  )

Checks if either a Selector, ComplexSelector, or Specificity has the given specificity.

Examples:

EXPECT_THAT(selector, SpecificityIs(Specificity::FromABC(0, 0, 0)));
EXPECT_THAT(specificity, SpecificityIs(Specificity::FromABC(0, 0, 0)));
static constexpr Specificity FromABC(uint32_t a, uint32_t b, uint32_t c)
Creates a specificity from the 3-tuple of integers.
Definition Specificity.h:155
Parameters
argSelector, ComplexSelector, or Specificity to match against.
specificitySpecificity to match against.

◆ operator<<() [1/8]

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.

◆ operator<<() [2/8]

std::ostream & donner::css::operator<< ( std::ostream & os,
const AtRule & rule )
Parameters
osOutput stream.
ruleAtRule to output.

◆ operator<<() [3/8]

std::ostream & donner::css::operator<< ( std::ostream & os,
const Color & color )

Example output:

Color(currentColor)
Represents a CSS color value, like a RGBA color from a #rrggbb or #rgb hex value, or the currentcolor...
Definition Color.h:112

or

Color(rgba(0, 255, 128, 255))
Parameters
osThe output stream.
colorThe color to output.

◆ operator<<() [4/8]

std::ostream & donner::css::operator<< ( std::ostream & os,
const Declaration & declaration )
Parameters
osOutput stream.
declarationDeclaration to output.

◆ operator<<() [5/8]

std::ostream & donner::css::operator<< ( std::ostream & os,
const Function & func )
Parameters
osOutput stream.
funcFunction to output.

◆ operator<<() [6/8]

std::ostream & donner::css::operator<< ( std::ostream & os,
const HSLA & color )

Outputs: hsla(h, s, l, a).

Parameters
osThe output stream.
colorThe color to output.

◆ operator<<() [7/8]

std::ostream & donner::css::operator<< ( std::ostream & os,
const RGBA & color )

Outputs: rgba(r, g, b, a).

Parameters
osThe output stream.
colorThe color to output.

◆ operator<<() [8/8]

std::ostream & donner::css::operator<< ( std::ostream & os,
const SimpleBlock & block )
Parameters
osOutput stream.
blockSimple block to output.

◆ SelectorsAre()

template<typename... Args>
auto donner::css::SelectorsAre ( const Args &... matchers)

Match a Selector against an array of ComplexSelectors, used like:

EXPECT_THAT(selector, SelectorsAre(ComplexSelectorIs(...)));
auto SelectorsAre(const Args &... matchers)
Match a Selector against an array of ComplexSelectors, used like:
Definition SelectorTestUtils.h:22
Parameters
matchersMatcher against a ComplexSelector array.

◆ TokenIs()

template<typename T , typename... Args>
auto donner::css::TokenIs ( Args... args)

Given a Token or a variant containing a token, matches if the token is equal, ignoring the offset.

Parameters
tokenToken subclass to match against.