Donner
C++20 SVG rendering library
Loading...
Searching...
No Matches
donner::css::PseudoClassSelector Struct Reference

Selectors which start with one colon, e.g. More...

#include "donner/css/selectors/PseudoClassSelector.h"

Collaboration diagram for donner::css::PseudoClassSelector:
[legend]

Classes

struct  PseudoMatchResult
 Result of matches, returns if the selector matched and if it can be treated as a "primary" matcher. More...
 

Public Member Functions

 PseudoClassSelector (const RcString &ident)
 Create a PseudoClassSelector with the given ident.
 
 ~PseudoClassSelector () noexcept
 Destructor.
 
 PseudoClassSelector (PseudoClassSelector &&) noexcept
 Move constructor.
 
PseudoClassSelectoroperator= (PseudoClassSelector &&) noexcept
 Move assignment operator.
 
 PseudoClassSelector (const PseudoClassSelector &other)
 Copy constructor.
 
PseudoClassSelectoroperator= (const PseudoClassSelector &other)
 Copy assignment operator.
 
bool isValid () const
 Returns true if this selector is valid and supported by this implementation.
 
template<ElementLike T>
PseudoMatchResult matches (const T &element, const SelectorMatchOptions< T > &options) const
 Returns true if the provided element matches this selector.
 
Specificity::ABC computeSpecificity () const
 Compute the pseudo-class's specificity, using the rules from https://www.w3.org/TR/2022/WD-selectors-4-20221111/#specificity-rules.
 

Public Attributes

RcString ident
 The name of the pseudo-class.
 
std::optional< std::vector< ComponentValue > > argsIfFunction
 The arguments of the pseudo-class, if it is a function.
 
std::optional< AnbValueanbValueIfAnb
 The An+B value of the pseudo-class, for An+B pseudo-classes such as :nth-child.
 
std::unique_ptr< Selectorselector
 The selector of the pseudo-class, for pseudo-classes such as :is() and :not(), or :nth-child(An+B of S).
 

Friends

std::ostream & operator<< (std::ostream &os, const PseudoClassSelector &obj)
 Ostream output operator for PseudoClassSelector, outputs a debug string e.g. PseudoClassSelector(after).
 

Detailed Description

Selectors which start with one colon, e.g.

:nth-child(), are called pseudo-classes, and they represent additional state information not directly present in the document tree.

Each pseudo-class selector has a unique behavior.

Selectors supported:

  • :nth-child(An+B [of S]) - Selects the element if its index within its parent is An+B (1-based) when counting from the first element that would be selected by S. If S is omitted, the selector matches only elements that are direct children of their parent.
  • :nth-last-child(An+B [of S]) - Selects the element if its index within its parent is An+B (1-based) when counting from the last element that would be selected by S. If S is omitted, the selector matches only elements that are direct children of their parent.
  • :nth-of-type(An+B) - Selects the element if its index within its parent's children of the same type is An+B (1-based).
  • :nth-last-of-type(An+B) - Selects the element if its index within its parent's children of the same type is An+B (1-based).
  • :first-child - Selects the element if it is the first child of its parent.
  • :last-child - Selects the element if it is the last child of its parent.
  • :first-of-type - Selects the element if it is the first child of its parent and its type is the same as its parent.
  • :last-of-type - Selects the element if it is the last child of its parent and its type is the same as its parent.
  • :only-child - Selects the element if it is the only child of its parent.
  • :only-of-type - Selects the element if it is the only child of its parent and its type is the same as its parent.
  • :empty - Selects the element if it has no children.
  • :root - Selects the element if it is the root of the document.
  • :is(S) - Selects the element if it matches any of the selectors in the argument list.
  • :not(S) - Selects the element if it does not match S.
  • :where(S) - Selects the element if it matches all of the selectors in the argument list.

Not yet implemented, see https://github.com/jwmcglynn/donner/issues/3:

  • :has(S) - Selects the element if any of its descendants match S.
  • :defined - Selects if the element is supported by the user agent (donner svg in this case).

Pseudo-classes are defined in the following specs:

Constructor & Destructor Documentation

◆ PseudoClassSelector()

donner::css::PseudoClassSelector::PseudoClassSelector ( const RcString & ident)
explicit

Create a PseudoClassSelector with the given ident.

Parameters
identThe name of the pseudo-class.

Member Function Documentation

◆ isValid()

bool donner::css::PseudoClassSelector::isValid ( ) const
inline

Returns true if this selector is valid and supported by this implementation.

See also
https://www.w3.org/TR/selectors-4/#invalid

◆ matches()

template<ElementLike T>
PseudoClassSelector::PseudoMatchResult donner::css::PseudoClassSelector::matches ( const T & element,
const SelectorMatchOptions< T > & options ) const

Returns true if the provided element matches this selector.

Template Parameters
TA type that fulfills the ElementLike concept, to enable traversing the tree to match the selector.
Parameters
elementThe element to check.
optionsThe options to use when matching

The documentation for this struct was generated from the following files: