Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / css / selector_1.lucb

#==============================================================================================
#
#   selector_1 - CSS::Selector: Selector.h's inline members, construction, the ancestor hashes
#                and specificity
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/Selector.h's inline members (PseudoElementSelector's constructors
#       and accessors, SimpleSelector's value accessors, Selector::create and the accessors)
#       and LibWeb/CSS/Selector.cpp (lines 1-332): component_value_contains_nesting_selector,
#       can_selector_use_fast_matches, the constructor, collect_ancestor_hashes and
#       specificity.
#
#       A Selector is ref-counted in C++ (RefCounted<Selector>): here it is a managed blob made
#       with `new` (DESIGN.md §2.3, §3.3), passed as `CssSelector*`. The two caches that C++
#       declares `mutable` (m_specificity, m_sibling_invalidation_distance) are written through
#       a cast of the const receiver.
#
#       Copyright (c) 2018-2025, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: Selector::PseudoElementSelector (Selector.h) ============================================

## Ported from Libraries/LibWeb/CSS/Selector.h:37 Web::CSS::Selector::PseudoElementSelector::PseudoElementSelector.
pub func css_selector_pseudo_element_selector_construct_css_selector_pseudo_element_se(this: CssSelectorPseudoElementSelector*, type_: css_data.PseudoElement, value: CssSelectorPseudoElementSelectorValue = CssSelectorPseudoElementSelectorValue.empty):
    this.m_type = type_
    this.m_name = ak.string_new()
    this.m_value = value
    assert(css_selector_pseudo_element_selector_is_known_pseudo_element_type(type_))

## Ported from Libraries/LibWeb/CSS/Selector.h:44 Web::CSS::Selector::PseudoElementSelector::PseudoElementSelector.
pub func css_selector_pseudo_element_selector_construct_string(this: CssSelectorPseudoElementSelector*, type_: css_data.PseudoElement, name: ak.String, value: CssSelectorPseudoElementSelectorValue = CssSelectorPseudoElementSelectorValue.empty):
    this.m_type = type_
    this.m_name = name
    this.m_value = value

## Ported from Libraries/LibWeb/CSS/Selector.h:53 Web::CSS::Selector::PseudoElementSelector::is_known_pseudo_element_type.
pub func css_selector_pseudo_element_selector_is_known_pseudo_element_type(type_: css_data.PseudoElement) -> bool:
    return (u8)type_ < (u8)css_data.PseudoElement.known_pseudo_element_count

## Ported from Libraries/LibWeb/CSS/Selector.h:60 Web::CSS::Selector::PseudoElementSelector::type.
pub func css_selector_pseudo_element_selector_type(this: const CssSelectorPseudoElementSelector*) -> css_data.PseudoElement:
    return this.m_type

## Ported from Libraries/LibWeb/CSS/Selector.h:62 Web::CSS::Selector::PseudoElementSelector::pt_name_selector.
pub func css_selector_pseudo_element_selector_pt_name_selector(this: const CssSelectorPseudoElementSelector*) -> CssSelectorPseudoElementSelectorPtNameSelector:
    match this.m_value:
        .css_selector_pseudo_element_selector_pt_name_selector(pt_name_selector): return pt_name_selector
        _: trap("VERIFY: PseudoElementSelector::pt_name_selector() on another alternative")

## Ported from Libraries/LibWeb/CSS/Selector.h:65 Web::CSS::Selector::PseudoElementSelector::compound_selector.
# NOTE: This can't (currently) be a CompoundSelector due to cyclic dependencies.
pub func css_selector_pseudo_element_selector_compound_selector(this: const CssSelectorPseudoElementSelector*) -> CssSelector*:
    match this.m_value:
        .css_selector(compound_selector): return compound_selector
        _: trap("VERIFY: PseudoElementSelector::compound_selector() on another alternative")

## Ported from Libraries/LibWeb/CSS/Selector.h:67 Web::CSS::Selector::PseudoElementSelector::ident_list.
pub func css_selector_pseudo_element_selector_ident_list(this: const CssSelectorPseudoElementSelector*) -> ak.Vector[ak.FlyString]:
    match this.m_value:
        .vector_fly_string(ident_list): return ident_list
        _: trap("VERIFY: PseudoElementSelector::ident_list() on another alternative")

# mark: Selector::SimpleSelector's value accessors (Selector.h) =================================

## Ported from Libraries/LibWeb/CSS/Selector.h:173 Web::CSS::Selector::SimpleSelector::attribute.
pub func css_selector_simple_selector_attribute(this: const CssSelectorSimpleSelector*) -> CssSelectorSimpleSelectorAttribute:
    match this.value:
        .css_selector_simple_selector_attribute(attribute): return attribute
        _: trap("VERIFY: SimpleSelector::attribute() on another alternative")

## Ported from Libraries/LibWeb/CSS/Selector.h:175 Web::CSS::Selector::SimpleSelector::pseudo_class.
pub func css_selector_simple_selector_pseudo_class(this: const CssSelectorSimpleSelector*) -> CssSelectorSimpleSelectorPseudoClassSelector:
    match this.value:
        .css_selector_simple_selector_pseudo_class_selector(pseudo_class): return pseudo_class
        _: trap("VERIFY: SimpleSelector::pseudo_class() on another alternative")

## Ported from Libraries/LibWeb/CSS/Selector.h:177 Web::CSS::Selector::SimpleSelector::pseudo_element.
pub func css_selector_simple_selector_pseudo_element(this: const CssSelectorSimpleSelector*) -> CssSelectorPseudoElementSelector:
    match this.value:
        .css_selector_pseudo_element_selector(pseudo_element): return pseudo_element
        _: trap("VERIFY: SimpleSelector::pseudo_element() on another alternative")

## Ported from Libraries/LibWeb/CSS/Selector.h:180 Web::CSS::Selector::SimpleSelector::name.
pub func css_selector_simple_selector_name(this: const CssSelectorSimpleSelector*) -> ak.FlyString:
    match this.value:
        .css_selector_simple_selector_name(name): return name.name
        _: trap("VERIFY: SimpleSelector::name() on another alternative")

## Ported from Libraries/LibWeb/CSS/Selector.h:182 Web::CSS::Selector::SimpleSelector::lowercase_name.
pub func css_selector_simple_selector_lowercase_name(this: const CssSelectorSimpleSelector*) -> ak.FlyString:
    match this.value:
        .css_selector_simple_selector_name(name): return name.lowercase_name
        _: trap("VERIFY: SimpleSelector::lowercase_name() on another alternative")

## Ported from Libraries/LibWeb/CSS/Selector.h:184 Web::CSS::Selector::SimpleSelector::qualified_name.
pub func css_selector_simple_selector_qualified_name(this: const CssSelectorSimpleSelector*) -> CssSelectorSimpleSelectorQualifiedName:
    match this.value:
        .css_selector_simple_selector_qualified_name(qualified_name): return qualified_name
        _: trap("VERIFY: SimpleSelector::qualified_name() on another alternative")

# mark: Selector (Selector.h) ===================================================================

## Ported from Libraries/LibWeb/CSS/Selector.h:210 Web::CSS::Selector::create.
pub func css_selector_create(compound_selectors: ak.Vector[CssSelectorCompoundSelector]) -> CssSelector*:
    let selector = new CssSelector else trap("out of memory")
    css_selector_construct(selector, compound_selectors)
    return selector

## Ported from Libraries/LibWeb/CSS/Selector.h:217 Web::CSS::Selector::compound_selectors.
pub func css_selector_compound_selectors(this: const CssSelector*) -> ak.Vector[CssSelectorCompoundSelector]:
    return this.m_compound_selectors

## Ported from Libraries/LibWeb/CSS/Selector.h:218 Web::CSS::Selector::target_pseudo_element.
pub func css_selector_target_pseudo_element(this: const CssSelector*) -> CssSelectorPseudoElementSelector?:
    return this.m_target_pseudo_element

## Ported from Libraries/LibWeb/CSS/Selector.h:220 Web::CSS::Selector::contains_the_nesting_selector.
pub func css_selector_contains_the_nesting_selector(this: const CssSelector*) -> bool:
    return this.m_contains_the_nesting_selector

## Ported from Libraries/LibWeb/CSS/Selector.h:221 Web::CSS::Selector::contains_pseudo_class.
pub func css_selector_contains_pseudo_class(this: const CssSelector*, pseudo_class: css_data.PseudoClass) -> bool:
    return css_pseudo_class_bitmap_get(&this.m_contained_pseudo_classes, pseudo_class)

## Ported from Libraries/LibWeb/CSS/Selector.h:227 Web::CSS::Selector::ancestor_hashes.
pub func css_selector_ancestor_hashes(this: const CssSelector*) -> u32[8]:
    return this.m_ancestor_hashes

## Ported from Libraries/LibWeb/CSS/Selector.h:229 Web::CSS::Selector::can_use_fast_matches.
pub func css_selector_can_use_fast_matches(this: const CssSelector*) -> bool:
    return this.m_can_use_fast_matches

## Ported from Libraries/LibWeb/CSS/Selector.h:230 Web::CSS::Selector::can_use_ancestor_filter.
pub func css_selector_can_use_ancestor_filter(this: const CssSelector*) -> bool:
    return this.m_can_use_ancestor_filter

## Ported from Libraries/LibWeb/CSS/Selector.h:235 Web::CSS::Selector::is_slotted.
# FIXME: Assess these once we fully support multiple pseudo-elements in one selector.
pub func css_selector_is_slotted(this: const CssSelector*) -> bool:
    let target_pseudo_element = this.m_target_pseudo_element else return false
    return css_selector_pseudo_element_selector_type(&target_pseudo_element) == css_data.PseudoElement.slotted

## Ported from Libraries/LibWeb/CSS/Selector.h:236 Web::CSS::Selector::has_part_pseudo_element.
pub func css_selector_has_part_pseudo_element(this: const CssSelector*) -> bool:
    return this.m_contains_part_pseudo_element

# mark: Construction (Selector.cpp) =============================================================

## Ported from Libraries/LibWeb/CSS/Selector.cpp:15 Web::CSS::component_value_contains_nesting_selector.
func css_component_value_contains_nesting_selector(component_value: const css_syntax.ComponentValue*) -> bool:
    if css_syntax.component_value_is_delim(component_value, (u32)'&'):
        return true

    if css_syntax.component_value_is_block(component_value):
        let block = css_syntax.component_value_block(component_value)
        let values = ak.vector_span_const(&block.value)
        for value in &values:
            if css_component_value_contains_nesting_selector(value):
                return true
        return false

    if css_syntax.component_value_is_function(component_value):
        let function = css_syntax.component_value_function(component_value)
        let values = ak.vector_span_const(&function.value)
        for value in &values:
            if css_component_value_contains_nesting_selector(value):
                return true
        return false

    return false

## can_selector_use_fast_matches's first_is_one_of over the pseudo-classes fast_matches handles.
func css_can_selector_use_fast_matches_pseudo_class(pseudo_class: css_data.PseudoClass) -> bool:
    match pseudo_class:
        .active, .any_link, .autofill, .checked, .disabled, .empty, .enabled, .first_child, .focus, .focus_visible: return true
        .focus_within, .hover, .last_child, .link, .local_link, .only_child, .root, .state, .unchecked, .visited: return true
        _: return false

## can_selector_use_fast_matches's first_is_one_of over the simple selector types fast_matches handles.
func css_can_selector_use_fast_matches_simple_selector_type(type_: CssSelectorSimpleSelectorType) -> bool:
    match type_:
        .tag_name, .universal, .class_, .id, .attribute: return true
        _: return false

## Ported from Libraries/LibWeb/CSS/Selector.cpp:29 Web::CSS::can_selector_use_fast_matches.
func css_can_selector_use_fast_matches(selector: const CssSelector*) -> bool:
    let compound_selectors = ak.vector_span_const(&selector.m_compound_selectors)
    for compound_selector in &compound_selectors:
        if not (compound_selector.combinator == CssSelectorCombinator.none_ or compound_selector.combinator == CssSelectorCombinator.descendant or compound_selector.combinator == CssSelectorCombinator.immediate_child):
            return false

        let simple_selectors = ak.vector_span_const(&compound_selector.simple_selectors)
        for simple_selector in &simple_selectors:
            if simple_selector.type_ == CssSelectorSimpleSelectorType.pseudo_class:
                let pseudo_class = css_selector_simple_selector_pseudo_class(simple_selector).type_
                if not css_can_selector_use_fast_matches_pseudo_class(pseudo_class):
                    return false
            elif not css_can_selector_use_fast_matches_simple_selector_type(simple_selector.type_):
                return false

    return true

## Ported from Libraries/LibWeb/CSS/Selector.cpp:76 Web::CSS::Selector::Selector.
pub func css_selector_construct(this: CssSelector*, compound_selectors: ak.Vector[CssSelectorCompoundSelector]):
    this.m_compound_selectors = compound_selectors

    # FIXME: This assumes that only one pseudo-element is allowed in a selector, and that it appears at the end.
    #        This is not true in Selectors-4!
    if not ak.vector_is_empty(&this.m_compound_selectors):
        let last_simple_selectors = ak.vector_span_const(&ak.vector_last_const(&this.m_compound_selectors).simple_selectors)
        for simple_selector in &last_simple_selectors:
            if simple_selector.type_ == CssSelectorSimpleSelectorType.pseudo_element:
                let pseudo_element = css_selector_simple_selector_pseudo_element(simple_selector)
                if css_selector_pseudo_element_selector_type(&pseudo_element) == css_data.PseudoElement.part:
                    this.m_contains_part_pseudo_element = true
                this.m_target_pseudo_element = pseudo_element

    # https://drafts.csswg.org/css-nesting-1/#contain-the-nesting-selector
    # "A selector is said to contain the nesting selector if, when it was parsed as any type of selector,
    # a <delim-token> with the value "&" (U+0026 AMPERSAND) was encountered."
    let all_compound_selectors = ak.vector_span_const(&this.m_compound_selectors)
    for compound_selector in &all_compound_selectors:
        let simple_selectors = ak.vector_span_const(&compound_selector.simple_selectors)
        for simple_selector in &simple_selectors:
            if simple_selector.type_ == CssSelectorSimpleSelectorType.nesting:
                this.m_contains_the_nesting_selector = true
                break
            if simple_selector.type_ == CssSelectorSimpleSelectorType.pseudo_class:
                let pseudo_class = css_selector_simple_selector_pseudo_class(simple_selector)
                css_pseudo_class_bitmap_set(&this.m_contained_pseudo_classes, pseudo_class.type_, true)
                for child_selector in ak.vector_span_const(&pseudo_class.argument_selector_list):
                    if css_selector_contains_the_nesting_selector(child_selector):
                        this.m_contains_the_nesting_selector = true
                    css_pseudo_class_bitmap_or_assign(&this.m_contained_pseudo_classes, child_selector.m_contained_pseudo_classes)
                if this.m_contains_the_nesting_selector:
                    break
            if simple_selector.type_ == CssSelectorSimpleSelectorType.invalid:
                match simple_selector.value:
                    .css_selector_simple_selector_invalid(invalid):
                        let component_values = ak.vector_span_const(&invalid.component_values)
                        for item in &component_values:
                            if css_component_value_contains_nesting_selector(item):
                                this.m_contains_the_nesting_selector = true
                                break
                    _:
                        trap("VERIFY: SimpleSelector::Invalid without its value")
                if this.m_contains_the_nesting_selector:
                    break
        if this.m_contains_the_nesting_selector:
            break

    css_selector_collect_ancestor_hashes(this)

    this.m_can_use_fast_matches = css_can_selector_use_fast_matches(this)

# mark: Ancestor hashes =========================================================================

## collect_ancestor_hashes's `append_unique_hash` lambda: appends `hash` to the ancestor hashes
## unless it is there already; answers true when the hashes are full.
func css_selector_collect_ancestor_hashes_append_unique_hash(this: CssSelector*, next_hash_index: usize*, hash_: u32) -> bool:
    if *next_hash_index >= this.m_ancestor_hashes.length:
        return true
    for i in 0..<*next_hash_index:
        if this.m_ancestor_hashes[i] == hash_:
            return false
    this.m_ancestor_hashes[*next_hash_index] = hash_
    *next_hash_index += 1
    return false

## collect_ancestor_hashes's `append_hashes_from_compound` lambda: answers true when the hashes
## are full.
func css_selector_collect_ancestor_hashes_append_hashes_from_compound(this: CssSelector*, next_hash_index: usize*, compound_selector: const CssSelectorCompoundSelector*) -> bool:
    let simple_selectors = ak.vector_span_const(&compound_selector.simple_selectors)
    for simple_selector in &simple_selectors:
        match simple_selector.type_:
            .id, .class_:
                let name = css_selector_simple_selector_name(simple_selector)
                if css_selector_collect_ancestor_hashes_append_unique_hash(this, next_hash_index, ak.fly_string_hash(&name)):
                    return true
            .tag_name:
                let qualified_name = css_selector_simple_selector_qualified_name(simple_selector)
                if css_selector_collect_ancestor_hashes_append_unique_hash(this, next_hash_index, ak.fly_string_hash(&qualified_name.name.lowercase_name)):
                    return true
            .attribute:
                let attribute = css_selector_simple_selector_attribute(simple_selector)
                if css_selector_collect_ancestor_hashes_append_unique_hash(this, next_hash_index, ak.fly_string_hash(&attribute.qualified_name.name.lowercase_name)):
                    return true
            _:
                _ = simple_selector
    return false

## Ported from Libraries/LibWeb/CSS/Selector.cpp:132 Web::CSS::Selector::collect_ancestor_hashes.
func css_selector_collect_ancestor_hashes(this: CssSelector*):
    if css_selector_is_slotted(this):
        # Ancestor filtering is not supported for slotted selectors, because those
        # are supposed to be collected for element inside a slot, while being
        # matched against slot element.
        return

    var next_hash_index: usize = 0

    # Walk from the compound immediately to the left of the subject toward the left.
    # The combinator that connects `i` to `i+1` is stored on `i+1`.
    var combinator_to_right = ak.vector_last_const(&this.m_compound_selectors).combinator

    # If we cross a sibling combinator, compounds further to the left are not ancestors of the subject,
    # but they *are* ancestors of that sibling and therefore shared ancestors of the subject (since siblings
    # share all ancestors above their parent). We can safely require tokens from those shared ancestors.
    var i = (isize)ak.vector_size(&this.m_compound_selectors) - 2
    while i >= 0:
        let compound_selector = ak.vector_at_const(&this.m_compound_selectors, (usize)i)

        match combinator_to_right:
            .descendant, .immediate_child:
                # This compound is on the ancestor axis (directly, or as a shared ancestor past a sibling boundary).
                if css_selector_collect_ancestor_hashes_append_hashes_from_compound(this, &next_hash_index, compound_selector):
                    this.m_can_use_ancestor_filter = next_hash_index > 0
                    return
            .next_sibling, .subsequent_sibling:
                # The compound immediately to the left is a sibling constraint, not an ancestor.
                # Do not collect hashes from it.
                _ = compound_selector
            _:
                # Not representable by the ancestor-hash filter.
                next_hash_index = 0
                this.m_can_use_ancestor_filter = false
                for slot in 0..<this.m_ancestor_hashes.length:
                    this.m_ancestor_hashes[slot] = 0
                return

        combinator_to_right = compound_selector.combinator
        i -= 1

    this.m_can_use_ancestor_filter = next_hash_index > 0

    for slot in next_hash_index..<this.m_ancestor_hashes.length:
        this.m_ancestor_hashes[slot] = 0

# mark: Specificity =============================================================================

## The counts specificity's `count_specificity_of_most_complex_selector` lambda adds to (its
## captures: ids, classes and tag_names by reference).
struct CssSelectorSpecificityCounts:
    var ids: u32
    var classes: u32
    var tag_names: u32

let css_selector_specificity_ids_shift: u32 = 16
let css_selector_specificity_classes_shift: u32 = 8
let css_selector_specificity_tag_names_shift: u32 = 0
let css_selector_specificity_ids_mask: u32 = 0xff << css_selector_specificity_ids_shift
let css_selector_specificity_classes_mask: u32 = 0xff << css_selector_specificity_classes_shift
let css_selector_specificity_tag_names_mask: u32 = 0xff << css_selector_specificity_tag_names_shift

## specificity's `count_specificity_of_most_complex_selector` lambda.
func css_selector_specificity_count_most_complex_selector(counts: CssSelectorSpecificityCounts*, selector_list: const ak.Vector[CssSelector*]*):
    var max_selector_list_argument_specificity: u32 = 0
    for complex_selector in ak.vector_span_const(selector_list):
        let complex_specificity = css_selector_specificity(complex_selector)
        if complex_specificity > max_selector_list_argument_specificity:
            max_selector_list_argument_specificity = complex_specificity

    let child_ids = (max_selector_list_argument_specificity & css_selector_specificity_ids_mask) >> css_selector_specificity_ids_shift
    let child_classes = (max_selector_list_argument_specificity & css_selector_specificity_classes_mask) >> css_selector_specificity_classes_shift
    let child_tag_names = (max_selector_list_argument_specificity & css_selector_specificity_tag_names_mask) >> css_selector_specificity_tag_names_shift

    counts.ids += child_ids
    counts.classes += child_classes
    counts.tag_names += child_tag_names

## `min(count, 0xff)`.
func css_selector_specificity_clamp(count: u32) -> u32:
    if count < 0xff:
        return count
    return 0xff

## Ported from Libraries/LibWeb/CSS/Selector.cpp:222 Web::CSS::Selector::specificity.
# https://www.w3.org/TR/selectors-4/#specificity-rules
pub func css_selector_specificity(this: const CssSelector*) -> u32:
    if let specificity = this.m_specificity:
        return specificity

    var counts = CssSelectorSpecificityCounts(ids = 0, classes = 0, tag_names = 0)

    let compound_selectors = ak.vector_span_const(&this.m_compound_selectors)
    for list in &compound_selectors:
        let simple_selectors = ak.vector_span_const(&list.simple_selectors)
        for simple_selector in &simple_selectors:
            match simple_selector.type_:
                .id:
                    # count the number of ID selectors in the selector (= A)
                    counts.ids += 1
                .class_, .attribute:
                    # count the number of class selectors, attributes selectors, and pseudo-classes in the selector (= B)
                    counts.classes += 1
                .pseudo_class:
                    let pseudo_class = css_selector_simple_selector_pseudo_class(simple_selector)
                    match pseudo_class.type_:
                        .has, .is, .not_:
                            # The specificity of an :is(), :not(), or :has() pseudo-class is replaced by the
                            # specificity of the most specific complex selector in its selector list argument.
                            css_selector_specificity_count_most_complex_selector(&counts, &pseudo_class.argument_selector_list)
                        .nth_child, .nth_last_child:
                            # Analogously, the specificity of an :nth-child() or :nth-last-child() selector
                            # is the specificity of the pseudo class itself (counting as one pseudo-class selector)
                            # plus the specificity of the most specific complex selector in its selector list argument (if any).
                            counts.classes += 1
                            css_selector_specificity_count_most_complex_selector(&counts, &pseudo_class.argument_selector_list)
                        .where:
                            # The specificity of a :where() pseudo-class is replaced by zero.
                            _ = pseudo_class
                        _:
                            counts.classes += 1
                .tag_name:
                    # count the number of type selectors and pseudo-elements in the selector (= C)
                    counts.tag_names += 1
                .pseudo_element:
                    # https://drafts.csswg.org/css-view-transitions-1/#named-view-transition-pseudo
                    # The specificity of a named view transition pseudo-element selector with a <custom-ident> argument is equivalent
                    # to a type selector. The specificity of a named view transition pseudo-element selector with a '*' argument is zero.
                    # NB: We just break before adding to the type (tag name) specificity in case this is a named view transition pseudo that uses '*'
                    let pseudo_element = css_selector_simple_selector_pseudo_element(simple_selector)
                    let type_ = css_selector_pseudo_element_selector_type(&pseudo_element)
                    if ((type_ == css_data.PseudoElement.view_transition_group or type_ == css_data.PseudoElement.view_transition_image_pair or type_ == css_data.PseudoElement.view_transition_old or type_ == css_data.PseudoElement.view_transition_new) and css_selector_pseudo_element_selector_pt_name_selector(&pseudo_element).is_universal):
                        continue

                    # count the number of type selectors and pseudo-elements in the selector (= C)
                    counts.tag_names += 1
                .universal:
                    # ignore the universal selector
                    _ = simple_selector
                .nesting:
                    # "The specificity of the nesting selector is equal to the largest specificity among the complex selectors in the parent style rule’s selector list (identical to the behavior of :is()), or zero if no such selector list exists."
                    # - https://drafts.csswg.org/css-nesting/#ref-for-specificity
                    # The parented case is handled by replacing & with :is().
                    # So if we got here, the specificity is 0.
                    _ = simple_selector
                .invalid:
                    # Ignore invalid selectors
                    _ = simple_selector
                _:
                    trap("VERIFY_NOT_REACHED")

    # Due to storage limitations, implementations may have limitations on the size of A, B, or C.
    # If so, values higher than the limit must be clamped to that limit, and not overflow.
    let specificity = ((css_selector_specificity_clamp(counts.ids) << css_selector_specificity_ids_shift) + (css_selector_specificity_clamp(counts.classes) << css_selector_specificity_classes_shift) + (css_selector_specificity_clamp(counts.tag_names) << css_selector_specificity_tag_names_shift))
    ((CssSelector*)this).m_specificity = specificity  # mutable in C++

    return specificity