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. | |
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:
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:
To construct from "!important":
|
strong |
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. |
|
inlinestaticconstexpr |
Creates a specificity from the 3-tuple of integers.
| 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. |
|
inlinestaticconstexpr |
Marks a declaration important while preserving its origin and selector specificity.
Already-important ranks and explicit non-CSS overrides are unchanged.
| specificity | Original declaration rank. |
specificity's origin, with its selector specificity.
|
friend |
Ostream output operator.
Example output:
or
| os | Output stream. |
| obj | Specificity to output. |