dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / css / custom_property_data.lucb
#==============================================================================================
#
# custom_property_data - CSS::CustomPropertyData: chains of custom property maps, and
# CustomPropertyRegistration's equality
#
# DESCRIPTION:
# Port of LibWeb/CSS/CustomPropertyData.cpp (lines 1-124), CustomPropertyData.h's inline
# members and CustomPropertyRegistration.h's operator==. A CustomPropertyData is
# ref-counted in C++: here it is a managed blob made with `new` (DESIGN.md ยง3.3). The
# members C++ declares `mutable` (the inheritable() cache) are written through a cast of
# the const receiver.
#
# Copyright (c) 2026, Andreas Kling <andreas@ladybird.org>
# Copyright (c) 2026, Sam Atkins <sam@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: CustomPropertyData ======================================================================
## Ported from Libraries/LibWeb/CSS/CustomPropertyData.cpp:16 Web::CSS::CustomPropertyData::CustomPropertyData.
pub func css_custom_property_data_construct(this: CssCustomPropertyData*, own_values: ak.OrderedHashMap[ak.FlyString, CssStyleProperty, ak.FlyStringTraits], parent: const CssCustomPropertyData*?, ancestor_count: u8):
css_custom_property_data_init_fields(this)
this.m_own_values = own_values
this.m_parent = parent
this.m_ancestor_count = ancestor_count
## `adopt_ref(*new CustomPropertyData(...))`: a new blob made by the constructor.
func css_custom_property_data_new(own_values: ak.OrderedHashMap[ak.FlyString, CssStyleProperty, ak.FlyStringTraits], parent: const CssCustomPropertyData*?, ancestor_count: u8) -> CssCustomPropertyData*:
let this = new CssCustomPropertyData else trap("out of memory")
css_custom_property_data_construct(this, own_values, parent, ancestor_count)
return this
## for_each_property's callback in create(): `own_values.ensure(name, [&] { return property; })`.
func css_custom_property_data_create_flatten(context: void*?, name: const ak.FlyString*, property: const CssStyleProperty*):
let own_values = (ak.OrderedHashMap[ak.FlyString, CssStyleProperty, ak.FlyStringTraits]*)(context else trap("no captures"))
if not ak.ordered_hash_map_contains_k(own_values, *name):
_ = ak.ordered_hash_map_set(own_values, *name, *property)
## Ported from Libraries/LibWeb/CSS/CustomPropertyData.cpp:23 Web::CSS::CustomPropertyData::create.
pub func css_custom_property_data_create(own_values: ak.OrderedHashMap[ak.FlyString, CssStyleProperty, ak.FlyStringTraits], parent: const CssCustomPropertyData*?) -> CssCustomPropertyData*:
var values = own_values
let parent_data = parent else return css_custom_property_data_new(values, none, 0)
# If parent chain is too deep, flatten by copying all ancestor values into own.
if parent_data.m_ancestor_count >= css_max_ancestor_count - 1:
css_custom_property_data_for_each_property(parent_data, ak.Function2[const ak.FlyString*, const CssStyleProperty*, unit](call = css_custom_property_data_create_flatten, context = &values))
return css_custom_property_data_new(values, none, 0)
# If parent has few own values, absorb them to shorten the chain.
if ak.ordered_hash_map_size(&parent_data.m_own_values) <= css_absorb_threshold:
for entry in ak.ordered_hash_map_begin_const(&parent_data.m_own_values):
if not ak.ordered_hash_map_contains_k(&values, entry.key):
_ = ak.ordered_hash_map_set(&values, entry.key, entry.value)
let grandparent = parent_data.m_parent
var ancestor_count: u8 = 0
if let grandparent_data = grandparent:
ancestor_count = grandparent_data.m_ancestor_count + 1
return css_custom_property_data_new(values, grandparent, ancestor_count)
let ancestor_count = parent_data.m_ancestor_count + 1
return css_custom_property_data_new(values, parent, ancestor_count)
## Ported from Libraries/LibWeb/CSS/CustomPropertyData.cpp:51 Web::CSS::CustomPropertyData::get.
pub func css_custom_property_data_get(this: const CssCustomPropertyData*, name: ak.FlyString) -> const CssStyleProperty*?:
if let property = ak.ordered_hash_map_get_k_const(&this.m_own_values, name):
return property
if let parent = this.m_parent:
return css_custom_property_data_get(parent, name)
return none
## Ported from Libraries/LibWeb/CSS/CustomPropertyData.cpp:60 Web::CSS::CustomPropertyData::inheritable.
pub func css_custom_property_data_inheritable(this: const CssCustomPropertyData*, document: DomDocument*) -> const CssCustomPropertyData*?:
# The cache members are `mutable` in C++.
let mutable_this = (CssCustomPropertyData*)this
let document_identity = (usize)document
let generation = dom_document_custom_property_registration_generation(document)
if this.m_cached_inheritable_document_identity == document_identity and this.m_cached_inheritable_generation == generation:
if this.m_cached_inheritable_is_self:
return this
return this.m_cached_inheritable_data
var inheritable_parent: const CssCustomPropertyData*? = none
if let parent = this.m_parent:
inheritable_parent = css_custom_property_data_inheritable(parent, document)
var inheritable_own_values: ak.OrderedHashMap[ak.FlyString, CssStyleProperty, ak.FlyStringTraits]
var filtered_any_own_values = false
for entry in ak.ordered_hash_map_begin_const(&this.m_own_values):
if let registration = dom_document_get_registered_custom_property(document, entry.key):
if not registration.inherit:
filtered_any_own_values = true
continue
_ = ak.ordered_hash_map_set(&inheritable_own_values, entry.key, entry.value)
# Registration generations are local to each document, so the cache has to include the destination document
# identity as well. Otherwise a subtree adopted into another document can incorrectly reuse a filtered result
# that was computed under a different registration set.
mutable_this.m_cached_inheritable_document_identity = document_identity
mutable_this.m_cached_inheritable_generation = generation
if not filtered_any_own_values and inheritable_parent == this.m_parent:
mutable_this.m_cached_inheritable_data = none
mutable_this.m_cached_inheritable_is_self = true
return this
mutable_this.m_cached_inheritable_is_self = false
if ak.ordered_hash_map_is_empty(&inheritable_own_values) and inheritable_parent == none:
mutable_this.m_cached_inheritable_data = none
return none
mutable_this.m_cached_inheritable_data = css_custom_property_data_create(inheritable_own_values, inheritable_parent)
return this.m_cached_inheritable_data
## Ported from Libraries/LibWeb/CSS/CustomPropertyData.h:32 Web::CSS::CustomPropertyData::own_values.
pub func css_custom_property_data_own_values(this: const CssCustomPropertyData*) -> ak.OrderedHashMap[ak.FlyString, CssStyleProperty, ak.FlyStringTraits]:
return this.m_own_values
## Ported from Libraries/LibWeb/CSS/CustomPropertyData.cpp:107 Web::CSS::CustomPropertyData::for_each_property.
pub func css_custom_property_data_for_each_property(this: const CssCustomPropertyData*, callback: ak.Function2[const ak.FlyString*, const CssStyleProperty*, unit]):
var seen: ak.HashTable[ak.FlyString, ak.FlyStringTraits]
var node: const CssCustomPropertyData*? = this
while let current = node:
for entry in ak.ordered_hash_map_begin_const(¤t.m_own_values):
# Donor quirk: HashTable::set replaces an existing entry by default and answers
# ReplacedExistingEntry, never KeptExistingEntry, so a name shadowed by a nearer node is
# passed to the callback again (create()'s callback keeps the nearer value).
if ak.hash_table_set(&seen, entry.key) == ak.HashSetResult.kept_existing_entry:
continue
ak.function2_call(&callback, &entry.key, &entry.value)
node = current.m_parent
## Ported from Libraries/LibWeb/CSS/CustomPropertyData.h:36 Web::CSS::CustomPropertyData::parent.
pub func css_custom_property_data_parent(this: const CssCustomPropertyData*) -> const CssCustomPropertyData*?:
return this.m_parent
## Ported from Libraries/LibWeb/CSS/CustomPropertyData.cpp:119 Web::CSS::CustomPropertyData::is_empty.
pub func css_custom_property_data_is_empty(this: const CssCustomPropertyData*) -> bool:
if not ak.ordered_hash_map_is_empty(&this.m_own_values):
return false
let parent = this.m_parent else return true
return css_custom_property_data_is_empty(parent)
# mark: CustomPropertyRegistration ==============================================================
## Ported from Libraries/LibWeb/CSS/CustomPropertyRegistration.h:35 Web::CSS::operator==.
pub func css_eq_css_custom_property_registrati(a: CssCustomPropertyRegistration, b: CssCustomPropertyRegistration) -> bool:
if not ak.fly_string_eq_fly_string(&a.property_name, &b.property_name):
return false
if not ak.string_equals(&a.syntax, &b.syntax):
return false
if a.inherit != b.inherit:
return false
if a.initial_value == b.initial_value:
return true
let a_initial_value = a.initial_value else return false
let b_initial_value = b.initial_value else return false
return css_style_value_equals(a_initial_value, b_initial_value)