Donner
C++20 SVG rendering library
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 ,
  Important ,
  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.
 
Specificityoperator= (const Specificity &other)=default
 Assignment operator.
 
Specificityoperator= (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 ABCabc () const
 Gets the 3-tuple of integers.
 
SpecialType specialValue () const
 Gets the special value of the specificity, defaults to SpecialType::None.
 
Specificity toUserAgentSpecificity () const
 Converts this Specificity into a UserAgent-specific Specificity (lowering the priority).
 

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 Important ()
 Creates a specificity for an !important declaration.
 
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"] (1, 1, 1)

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);
A CSS specificity value (id, class, type), as defined in https://www.w3.org/TR/selectors-4/#specifici...
Definition Specificity.h:49
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

To construct from "!important":

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

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 

User agent stylesheet, lowest precedence in CSS.

None 

No special value.

StyleAttribute 

Style attribute, second highest precedence in CSS.

Important 

!important declaration, highest precedence in CSS.

Override 

Values set from C++ API, which overrides all other values.

Member Function Documentation

◆ FromABC()

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

Friends And Related Symbol Documentation

◆ operator<<

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

Ostream output operator.

Example output:

Specificity(1, 2, 3)
constexpr Specificity()=default
Default constructor, creates a specificity of (0, 0, 0).

or

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

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