Donner SVG main
0.8.0 ยท 382bd0190.8.0 main
SVG editor and embeddable C⁠+⁠+⁠20 engine.
Loading...
Searching...
No Matches
donner::css::Selector Struct Reference

A top-level Selector, which is a list of ComplexSelector. More...

#include "donner/css/Selector.h"

Public Member Functions

 Selector ()
 Constructor.
 ~Selector () noexcept
 Destructor.
 Selector (Selector &&) noexcept
 Move constructor.
Selector & operator= (Selector &&) noexcept
 Move assignment operator.
 Selector (const Selector &)
 Copy constructor.
Selector & operator= (const Selector &)
 Copy assignment operator.
void collectAttributeSelectorNames (std::vector< RcString > &outNames, bool &outMatchesAnyName) const
 Collect the attribute names referenced by every attribute selector in this selector.
bool dependsOnTreePosition () const
 Return true when matching this selector depends on where an element sits in the tree, rather than on the element alone.
bool dependsOnTextContent () const
 Return true when matching this selector can change with an element's text content.
Specificity::ABC maxSpecificity () const
 Get the max specificity of all ComplexSelectors in the Selector.
template<ElementLike T>
SelectorMatchResult matches (const T &targetElement, const SelectorMatchOptions< T > &options=SelectorMatchOptions< T >()) const
 Match an element against a Selector.

Public Attributes

std::vector< ComplexSelector > entries
 The list of ComplexSelector entries that compose this selector.

Friends

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).

Detailed Description

A top-level Selector, which is a list of ComplexSelector.

This represents the prelude in front of any CSS rule, e.g. div.foo > span#bar, which would be a single ComplexSelector. For a comma-separated list, such as div.foo > span#bar, span#bar, this would be a Selector with two ComplexSelector entries.

Member Function Documentation

◆ collectAttributeSelectorNames()

void donner::css::Selector::collectAttributeSelectorNames ( std::vector< RcString > & outNames,
bool & outMatchesAnyName ) const

Collect the attribute names referenced by every attribute selector in this selector.

Recurses into functional pseudo-classes that carry their own selector list, such as :is(), :not(), :has() and :nth-child(An+B of S), so a nested [attr] is not missed.

Namespaces are ignored: only the local name is collected, which over-approximates [ns|attr] to [attr]. Callers use this to decide whether writing an attribute can change selector matching, where over-approximating is the safe direction.

Parameters
outNamesReceives each distinct local name found, appended to any existing contents.
outMatchesAnyNameSet to true if a selector uses a wildcard local name, meaning any attribute write can change matching. No CSS syntax produces this today; it is a fail-safe.

◆ dependsOnTextContent()

bool donner::css::Selector::dependsOnTextContent ( ) const

Return true when matching this selector can change with an element's text content.

:empty is the only supported pseudo-class that inspects text, so this finds it anywhere in the selector, including inside functional pseudo-classes such as :not(), :is() and :has(). Callers use this to decide whether a text edit can change which elements a rule matches.

Returns
True when a text edit can change whether this selector matches.

◆ dependsOnTreePosition()

bool donner::css::Selector::dependsOnTreePosition ( ) const

Return true when matching this selector depends on where an element sits in the tree, rather than on the element alone.

A combinator makes matching depend on ancestors or siblings (#layer rect, svg > rect, rect + rect), as does a leading combinator on a relative selector (> div). So do the structural pseudo-classes (:nth-child(), :first-child, :empty, :root, :scope, :host, :has(), :focus-within). Callers use this to decide whether moving an element can change which rules match it, or which elements a rule matches.

State pseudo-classes that inspect the element alone (:hover, :focus, :visited, :lang(), :checked, ...) and pseudo-elements do not count, and :is(), :not() and :where() are as positional as the selectors they contain. Any pseudo-class name this implementation does not recognize counts as positional, so the answer fails closed.

Returns
True when a move can change whether this selector matches.

◆ matches()

template<ElementLike T>
SelectorMatchResult donner::css::Selector::matches ( const T & targetElement,
const SelectorMatchOptions< T > & options = SelectorMatchOptions<T>() ) const
inline

Match an element against a Selector.

Template Parameters
TA type that fulfills the ElementLike concept, to enable traversing the tree to match the selector.
Parameters
targetElementElement to match against.
optionsOptions to control matching.
Returns
true if any ComplexSelector in the Selector matches the given element.

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