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

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

#include "donner/css/Specificity.h"

Classes

struct  ABC
 A 3-tuple of integers representing the specificity before modifiers such as the "!important" flag have been applied. More...

Public Types

enum class  SpecialType {
  UserAgent ,
  None ,
  StyleAttribute ,
  Animation ,
  Important ,
  ImportantStyleAttribute ,
  ImportantUserAgent ,
  Override
}
 Special values for specificity, which take precedence over the 3-tuple. More...

Public Member Functions

constexpr Specificity ()=default
 Default constructor, creates a specificity of (0, 0, 0).
constexpr Specificity (const ABC &abc)
 Constructs a specificity from a ABC 3-tuple.
 ~Specificity ()=default
 Destructor.
 Specificity (const Specificity &other)=default
 Copy constructor.
 Specificity (Specificity &&other) noexcept=default
 Move constructor.
Specificity & operator= (const Specificity &other)=default
 Assignment operator.
Specificity & operator= (Specificity &&other) noexcept=default
 Move assignment operator.
auto operator<=> (const Specificity &other) const
 Spaceship operator.
bool operator== (const Specificity &other) const
 Equality operator, for gtest.
const ABC & abc () const
 Gets the 3-tuple of integers.
SpecialType specialValue () const
 Gets the special value of the specificity, defaults to SpecialType::None.
bool isStyleAttribute () const
 Whether this rank came from an inline style declaration, regardless of importance.
Specificity toUserAgentSpecificity () const
 Converts a normal selector rank to user-agent origin; special ranks are unchanged.

Static Public Member Functions

static constexpr Specificity FromABC (uint32_t a, uint32_t b, uint32_t c)
 Creates a specificity from the 3-tuple of integers.
static constexpr Specificity Animation ()
 Creates the declarative animation cascade level.
static constexpr Specificity Important ()
 Creates a specificity for an !important declaration.
static constexpr Specificity Important (Specificity specificity)
 Marks a declaration important while preserving its origin and selector specificity.
static constexpr Specificity StyleAttribute ()
 Creates a specificity for a style attribute.
static constexpr Specificity Override ()
 Creates a specificity that overrides any other value, for overriding styles from the C++ API.

Friends

std::ostream & operator<< (std::ostream &os, const SpecialType &type)
 Output stream operator.
std::ostream & operator<< (std::ostream &os, const Specificity &obj)
 Ostream output operator.

Detailed Description

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.

The specificity is a 3-tuple of integers, where the first integer is the most significant, plus a few special values such as "!important" which override.

The 3-tuple is defined as:

  • a: The number of ID selectors in the selector.
  • b: The number of class selectors, attributes selectors, and pseudo-classes in the selector.
  • c: The number of type selectors and pseudo-elements in the selector.

Examples

Selector Specificity
#id (1, 0, 0)
.class (0, 1, 0)
div (0, 0, 1)
#id.class (1, 1, 0)
#id.class div (1, 1, 1)
#id.class > div (1, 1, 1)
[class~="class"] (0, 1, 0)

For example, the selector #id.class has a specificity of (1, 1, 0), while div > p has a specificity of (0, 0, 2).

To construct from a 3-tuple:

const Specificity spec = Specificity::FromABC(1, 2, 3);
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:164
constexpr Specificity()=default
Default constructor, creates a specificity of (0, 0, 0).

To construct from "!important":

static constexpr Specificity Important()
Creates a specificity for an !important declaration.
Definition Specificity.h:174

Member Enumeration Documentation

◆ SpecialType

Special values for specificity, which take precedence over the 3-tuple.

The order of these values is important, since operator<=> considers later enum values to be greater.

Enumerator
UserAgent 

Normal user agent stylesheet declaration.

None 

Normal author declaration.

StyleAttribute 

Normal inline style, above normal author selectors.

Animation 

Declarative animation, below important declarations.

Important 

Important author selector declaration.

ImportantStyleAttribute 

Important inline style, above important author selectors.

ImportantUserAgent 

Important user agent declaration, above author CSS.

Override 

Explicit C++ values, above every CSS declaration.

Member Function Documentation

◆ FromABC()

constexpr Specificity donner::css::Specificity::FromABC ( uint32_t a,
uint32_t b,
uint32_t c )
inlinestaticconstexpr

Creates a specificity from the 3-tuple of integers.

Parameters
aThe number of ID selectors in the selector.
bThe number of class selectors, attributes selectors, and pseudo-classes in the selector.
cThe number of type selectors and pseudo-elements in the selector.

◆ Important()

constexpr Specificity donner::css::Specificity::Important ( Specificity specificity)
inlinestaticconstexpr

Marks a declaration important while preserving its origin and selector specificity.

Already-important ranks and explicit non-CSS overrides are unchanged.

Parameters
specificityOriginal declaration rank.
Returns
The important rank for specificity's origin, with its selector specificity.

◆ operator<<

std::ostream & operator<< ( std::ostream & os,
const Specificity & obj )
friend

Ostream output operator.

Example output:

Specificity(1, 2, 3)

or

Specificity(!important, 0, 0, 0)
Parameters
osOutput stream.
objSpecificity to output.

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