Donner SVG main
0.8.0 ยท 766e862a0.8.0 main
SVG editor and embeddable C⁠+⁠+⁠20 engine.
Loading...
Searching...
No Matches
donner::svg::components::RuleIndex Class Reference

Candidate rules of one stylesheet, bucketed by a key of each selector-list entry's subject compound. More...

#include "donner/svg/components/style/RuleIndex.h"

Public Types

using AncestorHashGroup = std::array<std::uint32_t, 4>
 Hashes of keys every ancestor-filter match needs among an element's ancestors: four, with repeats when a selector-list entry has fewer than four distinct ones.

Public Member Functions

 RuleIndex ()=default
 An empty index, for a stylesheet with no rules.
 RuleIndex (std::span< const css::SelectorRule > rules)
 Build the index of rules.
std::span< const AncestorHashGroup > ancestorHashes (std::uint32_t rule) const
 The ancestor hash groups of the rule at rule, one per selector-list entry, or none when some entry has no ancestor key.
css::Specificity::ABC specificity (std::uint32_t rule, std::size_t entry) const
 Specificity captured for one selector-list entry of the stylesheet used to build this index.
template<RuleIndexElement Element>
std::size_t collectCandidates (const Element &element, std::vector< std::uint32_t > &candidates) const
 Replace candidates with the positions of the rules that can match element, ascending and without duplicates.
bool shallowForParallelCascade () const
 Whether every rule stays within the recursive CSS depth supported by scheduled style tasks.
std::size_t ruleCount () const
 Number of rules the index was built from.
std::size_t entryCount () const
 Number of filed entries: one per selector-list entry, less entries of one rule with the same key.
std::size_t retainedBytes () const
 Bytes the index retains: vector capacities, plus the text of type and attribute keys, which may be lowercased copies.

Static Public Member Functions

static RuleIndexKey KeyFor (const css::ComplexSelector &selector)
 The key one selector-list entry is filed under: the subject compound's id, else a class, else an attribute local name without a namespace, else a non-universal type name.
static BloomHash AncestorKeyHash (RuleIndexKey::Kind kind, std::string_view name)
 The ancestor-filter hash of a key, as StyleAncestorFilter inserts it for an ancestor and ancestorHashes records it for a selector.
static SmallVector< std::uint32_t, 4 > AncestorHashesFor (const css::ComplexSelector &selector)
 Up to four keys that every element selector matches must have among its ancestors, as AncestorKeyHash values: from the compounds reached from the subject through descendant and child combinators, stopping at the first sibling combinator (compounds beyond it are siblings of an ancestor), taking ids, then classes, then attribute local names without a namespace, then non-universal type names.
static bool IsDeclarationShallowForParallelCascade (const css::Declaration &declaration)
 Whether a declaration's component tree stays within the scheduled style depth limit.

Static Public Attributes

static constexpr std::size_t kMaxBytesPerEntry
 Most bytes one selector-list entry adds to retainedBytes besides key text and vector slack: its rule position, at most one key record, its ancestor hash group, its specificity, and at most two per-rule offsets. A nonempty index also retains one leading 4-byte offset.

Detailed Description

Candidate rules of one stylesheet, bucketed by a key of each selector-list entry's subject compound.

Every key is a necessary condition for its entry to match, so the candidates of an element include every rule that can match it; the matcher still tests each candidate in full.

Built once per parsed stylesheet. It stores rule positions, not pointers, so it stays valid while the stylesheet it was built from is unchanged and must be rebuilt when it changes.

Constructor & Destructor Documentation

◆ RuleIndex()

donner::svg::components::RuleIndex::RuleIndex ( std::span< const css::SelectorRule > rules)
explicit

Build the index of rules.

Parameters
rulesRules of one stylesheet, in cascade order.

Member Function Documentation

◆ ancestorHashes()

std::span< const AncestorHashGroup > donner::svg::components::RuleIndex::ancestorHashes ( std::uint32_t rule) const
inline

The ancestor hash groups of the rule at rule, one per selector-list entry, or none when some entry has no ancestor key.

A candidate rule can be skipped when, for every group, some hash of the group is absent from the element's ancestors; with no groups it is never skipped.

Parameters
ruleRule position, less than ruleCount.

◆ AncestorHashesFor()

SmallVector< std::uint32_t, 4 > donner::svg::components::RuleIndex::AncestorHashesFor ( const css::ComplexSelector & selector)
static

Up to four keys that every element selector matches must have among its ancestors, as AncestorKeyHash values: from the compounds reached from the subject through descendant and child combinators, stopping at the first sibling combinator (compounds beyond it are siblings of an ancestor), taking ids, then classes, then attribute local names without a namespace, then non-universal type names.

Simple selectors inside pseudo-class arguments are never used, because :not() inverts them and :is() lists alternatives.

Parameters
selectorOne entry of a rule's selector list.
Returns
The distinct hashes, at most four; empty when no ancestor compound has such a key.

◆ AncestorKeyHash()

BloomHash donner::svg::components::RuleIndex::AncestorKeyHash ( RuleIndexKey::Kind kind,
std::string_view name )
inlinestatic

The ancestor-filter hash of a key, as StyleAncestorFilter inserts it for an ancestor and ancestorHashes records it for a selector.

Parameters
kindThe key's kind; RuleIndexKey::Kind::Universal is not a key.
nameThe key text, ASCII-lowercased for RuleIndexKey::Kind::Type and RuleIndexKey::Kind::Attribute.

◆ collectCandidates()

template<RuleIndexElement Element>
std::size_t donner::svg::components::RuleIndex::collectCandidates ( const Element & element,
std::vector< std::uint32_t > & candidates ) const
inline

Replace candidates with the positions of the rules that can match element, ascending and without duplicates.

Each bucket is gathered at most once per element, however often the element names its key: a repeated class token, or attribute names that differ only in case. Every filed entry sits in exactly one bucket or the universal span, so at most entryCount positions are gathered before duplicates are removed, and candidates never grows past that. The remaining work is one lookup per key the element carries, which is linear in the element's own source.

Parameters
elementThe element, read only for the key kinds this index has.
candidatesReceives the rule positions; its capacity is reused.
Returns
Rule positions gathered before duplicates were removed, at most entryCount.

◆ KeyFor()

RuleIndexKey donner::svg::components::RuleIndex::KeyFor ( const css::ComplexSelector & selector)
static

The key one selector-list entry is filed under: the subject compound's id, else a class, else an attribute local name without a namespace, else a non-universal type name.

Pseudo-classes and pseudo-elements give no key, and neither do their arguments.

Parameters
selectorOne entry of a rule's selector list.

Member Data Documentation

◆ kMaxBytesPerEntry

std::size_t donner::svg::components::RuleIndex::kMaxBytesPerEntry
staticconstexpr
Initial value:
=
sizeof(std::uint32_t) + sizeof(Bucket) + sizeof(AncestorHashGroup) + sizeof(std::uint32_t) +
sizeof(css::Specificity::ABC) + sizeof(std::size_t)
std::array< std::uint32_t, 4 > AncestorHashGroup
Hashes of keys every ancestor-filter match needs among an element's ancestors: four,...
Definition RuleIndex.h:123
A 3-tuple of integers representing the specificity before modifiers such as the "!...
Definition Specificity.h:86

Most bytes one selector-list entry adds to retainedBytes besides key text and vector slack: its rule position, at most one key record, its ancestor hash group, its specificity, and at most two per-rule offsets. A nonempty index also retains one leading 4-byte offset.


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