Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / css / invalidation_set.lucb

#==============================================================================================
#
#   invalidation_set - CSS::InvalidationSet: the features (classes, ids, tag names, attributes,
#                      pseudo-classes) whose change can invalidate an element's style
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/InvalidationSet.cpp (lines 1-122) and the inline members of
#       InvalidationSet.h (lines 18-85): the set's flags and property table, equality, merging,
#       AK::Traits<InvalidationSet::Property> and the two AK::Formatter specializations
#       (`$` self, `*` whole subtree, `.class`, `#id`, `tag`, `[attribute]`, `:pseudo-class`).
#
#       A set is a value holding a HashTable: where C++ copies one and the copy is changed,
#       the port clones it (css_invalidation_set_clone, DESIGN ยง3.3 rule 2).
#
#       Copyright (c) 2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: InvalidationSet::Property ===============================================================

## Ported from Libraries/LibWeb/CSS/InvalidationSet.h:34 Web::CSS::InvalidationSet::Property::name.
pub func css_invalidation_set_property_name(this: const CssInvalidationSetProperty*) -> ak.FlyString:
    match this.value:
        .fly_string(name): return name
        _: trap("VERIFY: Variant::get<FlyString>")

## Ported from Libraries/LibWeb/CSS/InvalidationSet.h:36 Web::CSS::InvalidationSet::Property::operator== (defaulted:
## the type, then the variant's alternative and value).
pub func css_invalidation_set_property_eq(this: const CssInvalidationSetProperty*, other: const CssInvalidationSetProperty*) -> bool:
    if this.type_ != other.type_:
        return false
    match this.value:
        .fly_string(name):
            match other.value:
                .fly_string(other_name): return ak.fly_string_eq_fly_string(&name, &other_name)
                _: return false
        .pseudo_class(pseudo_class):
            match other.value:
                .pseudo_class(other_pseudo_class): return pseudo_class == other_pseudo_class
                _: return false
        .empty:
            match other.value:
                .empty: return true
                _: return false

## Ported from Libraries/LibWeb/CSS/InvalidationSet.cpp:60 AK::Traits<Web::CSS::InvalidationSet::Property>::hash.
pub func css_invalidation_set_property_traits_hash(invalidation_set_property: const CssInvalidationSetProperty*) -> u32:
    var value_hash: u32 = 0
    match invalidation_set_property.value:
        .fly_string(value): value_hash = ak.fly_string_hash(&value)
        .pseudo_class(value): value_hash = (u32)(i32)value
        .empty: value_hash = 0
    return ak.pair_int_hash((u32)(u8)invalidation_set_property.type_, value_hash)

## Ported from Libraries/LibWeb/CSS/InvalidationSet.cpp:69 AK::Formatter<Web::CSS::InvalidationSet::Property>::format.
pub func css_invalidation_set_property_format(builder: ak.StringBuilder*, invalidation_set_property: const CssInvalidationSetProperty*):
    match invalidation_set_property.type_:
        .invalidate_self:
            ak.string_builder_append(builder, "$")
        .class_:
            let name = css_invalidation_set_property_name(invalidation_set_property)
            ak.string_builder_append(builder, ".")
            ak.string_builder_append_string_view(builder, ak.fly_string_bytes_as_string_view(&name))
        .id:
            let name = css_invalidation_set_property_name(invalidation_set_property)
            ak.string_builder_append(builder, "#")
            ak.string_builder_append_string_view(builder, ak.fly_string_bytes_as_string_view(&name))
        .tag_name:
            let name = css_invalidation_set_property_name(invalidation_set_property)
            ak.string_builder_append_string_view(builder, ak.fly_string_bytes_as_string_view(&name))
        .attribute:
            let name = css_invalidation_set_property_name(invalidation_set_property)
            ak.string_builder_append(builder, "[")
            ak.string_builder_append_string_view(builder, ak.fly_string_bytes_as_string_view(&name))
            ak.string_builder_append(builder, "]")
        .pseudo_class:
            match invalidation_set_property.value:
                .pseudo_class(pseudo_class):
                    ak.string_builder_append(builder, ":")
                    ak.string_builder_append_string_view(builder, css_data.pseudo_class_name(pseudo_class))
                _:
                    trap("VERIFY: Variant::get<PseudoClass>")
        .invalidate_whole_subtree:
            ak.string_builder_append(builder, "*")
        _:
            trap("VERIFY_NOT_REACHED")

# mark: InvalidationSet =========================================================================

## A copy of `other` with its own property table (the C++ copy constructor).
pub func css_invalidation_set_clone(other: const CssInvalidationSet*) -> CssInvalidationSet:
    return CssInvalidationSet(m_needs_invalidate_self = other.m_needs_invalidate_self, m_needs_invalidate_whole_subtree = other.m_needs_invalidate_whole_subtree, m_properties = ak.hash_table_clone(&other.m_properties))

## Ported from Libraries/LibWeb/CSS/InvalidationSet.cpp:11 Web::CSS::InvalidationSet::operator==.
pub func css_invalidation_set_eq(this: const CssInvalidationSet*, other: CssInvalidationSet) -> bool:
    if this.m_needs_invalidate_self != other.m_needs_invalidate_self:
        return false
    if this.m_needs_invalidate_whole_subtree != other.m_needs_invalidate_whole_subtree:
        return false
    if ak.hash_table_size(&this.m_properties) != ak.hash_table_size(&other.m_properties):
        return false

    for property in ak.hash_table_begin_const(&this.m_properties):
        if not ak.hash_table_contains_t(&other.m_properties, *property):
            return false
    return true

## Ported from Libraries/LibWeb/CSS/InvalidationSet.cpp:27 Web::CSS::InvalidationSet::include_all_from.
pub func css_invalidation_set_include_all_from(this: CssInvalidationSet*, other: CssInvalidationSet):
    this.m_needs_invalidate_self = this.m_needs_invalidate_self or other.m_needs_invalidate_self
    this.m_needs_invalidate_whole_subtree = this.m_needs_invalidate_whole_subtree or other.m_needs_invalidate_whole_subtree
    for property in ak.hash_table_begin_const(&other.m_properties):
        _ = ak.hash_table_set(&this.m_properties, *property)

## Ported from Libraries/LibWeb/CSS/InvalidationSet.h:41 Web::CSS::InvalidationSet::needs_invalidate_self.
pub func css_invalidation_set_needs_invalidate_self(this: const CssInvalidationSet*) -> bool:
    return this.m_needs_invalidate_self

## Ported from Libraries/LibWeb/CSS/InvalidationSet.h:42 Web::CSS::InvalidationSet::set_needs_invalidate_self.
pub func css_invalidation_set_set_needs_invalidate_self(this: CssInvalidationSet*):
    this.m_needs_invalidate_self = true

## Ported from Libraries/LibWeb/CSS/InvalidationSet.h:44 Web::CSS::InvalidationSet::needs_invalidate_whole_subtree.
pub func css_invalidation_set_needs_invalidate_whole_subtree(this: const CssInvalidationSet*) -> bool:
    return this.m_needs_invalidate_whole_subtree

## Ported from Libraries/LibWeb/CSS/InvalidationSet.h:45 Web::CSS::InvalidationSet::set_needs_invalidate_whole_subtree.
pub func css_invalidation_set_set_needs_invalidate_whole_subtree(this: CssInvalidationSet*):
    this.m_needs_invalidate_whole_subtree = true

## Ported from Libraries/LibWeb/CSS/InvalidationSet.h:49 Web::CSS::InvalidationSet::set_needs_invalidate_class.
pub func css_invalidation_set_set_needs_invalidate_class(this: CssInvalidationSet*, name: ak.FlyString):
    _ = ak.hash_table_set(&this.m_properties, CssInvalidationSetProperty(type_ = CssInvalidationSetPropertyType.class_, value = FlyStringOrPseudoClassOrEmpty.fly_string(name)))

## Ported from Libraries/LibWeb/CSS/InvalidationSet.h:50 Web::CSS::InvalidationSet::set_needs_invalidate_id.
pub func css_invalidation_set_set_needs_invalidate_id(this: CssInvalidationSet*, name: ak.FlyString):
    _ = ak.hash_table_set(&this.m_properties, CssInvalidationSetProperty(type_ = CssInvalidationSetPropertyType.id, value = FlyStringOrPseudoClassOrEmpty.fly_string(name)))

## Ported from Libraries/LibWeb/CSS/InvalidationSet.h:51 Web::CSS::InvalidationSet::set_needs_invalidate_tag_name.
pub func css_invalidation_set_set_needs_invalidate_tag_name(this: CssInvalidationSet*, name: ak.FlyString):
    _ = ak.hash_table_set(&this.m_properties, CssInvalidationSetProperty(type_ = CssInvalidationSetPropertyType.tag_name, value = FlyStringOrPseudoClassOrEmpty.fly_string(name)))

## Ported from Libraries/LibWeb/CSS/InvalidationSet.h:52 Web::CSS::InvalidationSet::set_needs_invalidate_attribute.
pub func css_invalidation_set_set_needs_invalidate_attribute(this: CssInvalidationSet*, name: ak.FlyString):
    _ = ak.hash_table_set(&this.m_properties, CssInvalidationSetProperty(type_ = CssInvalidationSetPropertyType.attribute, value = FlyStringOrPseudoClassOrEmpty.fly_string(name)))

## Ported from Libraries/LibWeb/CSS/InvalidationSet.h:53 Web::CSS::InvalidationSet::set_needs_invalidate_pseudo_class.
pub func css_invalidation_set_set_needs_invalidate_pseudo_class(this: CssInvalidationSet*, pseudo_class: css_data.PseudoClass):
    _ = ak.hash_table_set(&this.m_properties, CssInvalidationSetProperty(type_ = CssInvalidationSetPropertyType.pseudo_class, value = FlyStringOrPseudoClassOrEmpty.pseudo_class(pseudo_class)))

## Ported from Libraries/LibWeb/CSS/InvalidationSet.cpp:35 Web::CSS::InvalidationSet::is_empty.
pub func css_invalidation_set_is_empty(this: const CssInvalidationSet*) -> bool:
    return not this.m_needs_invalidate_self and not this.m_needs_invalidate_whole_subtree and ak.hash_table_is_empty(&this.m_properties)

## Ported from Libraries/LibWeb/CSS/InvalidationSet.h:56 Web::CSS::InvalidationSet::has_properties.
pub func css_invalidation_set_has_properties(this: const CssInvalidationSet*) -> bool:
    return not ak.hash_table_is_empty(&this.m_properties)

## Ported from Libraries/LibWeb/CSS/InvalidationSet.cpp:40 Web::CSS::InvalidationSet::for_each_property.
## (Loops in the port use css_invalidation_set_properties, the same walk as an iterator.)
pub func css_invalidation_set_for_each_property(this: const CssInvalidationSet*, callback: ak.Function1[const CssInvalidationSetProperty*, ak.IterationDecision]):
    if this.m_needs_invalidate_self:
        let property = CssInvalidationSetProperty(type_ = CssInvalidationSetPropertyType.invalidate_self, value = FlyStringOrPseudoClassOrEmpty.empty)
        if ak.function1_call(&callback, &property) == ak.IterationDecision.break_:
            return
    if this.m_needs_invalidate_whole_subtree:
        let property = CssInvalidationSetProperty(type_ = CssInvalidationSetPropertyType.invalidate_whole_subtree, value = FlyStringOrPseudoClassOrEmpty.empty)
        if ak.function1_call(&callback, &property) == ak.IterationDecision.break_:
            return
    for property in ak.hash_table_begin_const(&this.m_properties):
        if ak.function1_call(&callback, (const CssInvalidationSetProperty*)property) == ak.IterationDecision.break_:
            return

## Ported from Libraries/LibWeb/CSS/InvalidationSet.cpp:110 AK::Formatter<Web::CSS::InvalidationSet>::format.
pub func css_invalidation_set_format(builder: ak.StringBuilder*, invalidation_set: const CssInvalidationSet*):
    let first = true
    for property in css_invalidation_set_properties(invalidation_set):
        if not first:
            ak.string_builder_append(builder, ", ")
        css_invalidation_set_property_format(builder, &property)
        # Donor quirk kept: `first` is never cleared, so no ", " is ever written between properties.