dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / css / cascaded_properties.lucb
#==============================================================================================
#
# cascaded_properties - CSS::CascadedProperties: the cascade's winning declarations per property
#
# DESCRIPTION:
# Port of LibWeb/CSS/CascadedProperties.cpp (lines 1-153) and CascadedProperties.h. For
# each property the cascade keeps the entries that set it, one per origin and layer, in
# cascade order; the last entry is the cascaded value. `m_contained_properties_cache` (an
# AK::FixedBitmap in C++) is a bool array.
#
# Copyright (c) 2024, Andreas Kling <andreas@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## Ported from Libraries/LibWeb/CSS/CascadedProperties.cpp:17 Web::CSS::CascadedProperties::CascadedProperties.
pub func css_cascaded_properties_construct(this: CssCascadedProperties*):
# The cell is zeroed: m_properties is empty, m_next_cascade_index 0 and the cache all false.
return
## Ported from Libraries/LibWeb/CSS/CascadedProperties.cpp:21 Web::CSS::CascadedProperties::visit_edges (override).
pub func css_cascaded_properties_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (CssCascadedProperties*)base
gc.cell_visit_edges_impl(base, visitor)
for entry in ak.hash_map_begin(&this.m_properties):
for property_entry in ak.vector_span_const(&entry.value):
gc.cell_visitor_visit_t(visitor, property_entry.source)
gc.cell_visitor_visit_t(visitor, property_entry.source_shadow_root)
## The captures of revert_property's `remove_all_matching` predicate.
struct CssCascadedPropertiesRevertPropertyMatch:
var property_id: css_data.PropertyId
var important: css_syntax.Important
var cascade_origin: CssCascadeOrigin
## revert_property's `remove_all_matching` predicate.
func css_cascaded_properties_revert_property_match(context: void*?, entry: const CssCascadedPropertiesEntry*) -> bool:
let captures = (CssCascadedPropertiesRevertPropertyMatch*)(context else trap("no captures"))
# https://drafts.csswg.org/css-cascade-5/#author-presentational-hint-origin
# For the purpose of cascading this author presentational hint origin is treated as an independent origin, but
# for the purpose of the revert keyword it is considered part of the author origin.
let origin_matches = entry.origin == captures.cascade_origin or (captures.cascade_origin == CssCascadeOrigin.author and entry.origin == CssCascadeOrigin.author_presentational_hint)
return entry.property.property_id == captures.property_id and entry.property.important == captures.important and origin_matches
## Ported from Libraries/LibWeb/CSS/CascadedProperties.cpp:32 Web::CSS::CascadedProperties::revert_property.
pub func css_cascaded_properties_revert_property(this: CssCascadedProperties*, property_id: css_data.PropertyId, important: css_syntax.Important, cascade_origin: CssCascadeOrigin):
let it = ak.hash_map_find_k(&this.m_properties, property_id)
let bucket = it.m_bucket else return
let entries = &bucket.storage.value
var captures = CssCascadedPropertiesRevertPropertyMatch(property_id = property_id, important = important, cascade_origin = cascade_origin)
_ = ak.vector_remove_all_matching(entries, ak.Function1[const CssCascadedPropertiesEntry*, bool](call = css_cascaded_properties_revert_property_match, context = &captures))
if ak.vector_is_empty(entries):
this.m_contained_properties_cache[(usize)(u16)property_id] = false
ak.hash_map_remove_opaque(&this.m_properties, it)
## The captures of revert_layer_property's `remove_all_matching` predicate.
struct CssCascadedPropertiesRevertLayerPropertyMatch:
var property_id: css_data.PropertyId
var important: css_syntax.Important
var layer_name: ak.FlyString?
## revert_layer_property's `remove_all_matching` predicate.
func css_cascaded_properties_revert_layer_property_match(context: void*?, entry: const CssCascadedPropertiesEntry*) -> bool:
let captures = (CssCascadedPropertiesRevertLayerPropertyMatch*)(context else trap("no captures"))
return entry.property.property_id == captures.property_id and entry.property.important == captures.important and css_optional_fly_strings_equal(captures.layer_name, entry.layer_name)
## Ported from Libraries/LibWeb/CSS/CascadedProperties.cpp:54 Web::CSS::CascadedProperties::revert_layer_property.
pub func css_cascaded_properties_revert_layer_property(this: CssCascadedProperties*, property_id: css_data.PropertyId, important: css_syntax.Important, layer_name: ak.FlyString?):
let it = ak.hash_map_find_k(&this.m_properties, property_id)
let bucket = it.m_bucket else return
let entries = &bucket.storage.value
var captures = CssCascadedPropertiesRevertLayerPropertyMatch(property_id = property_id, important = important, layer_name = layer_name)
_ = ak.vector_remove_all_matching(entries, ak.Function1[const CssCascadedPropertiesEntry*, bool](call = css_cascaded_properties_revert_layer_property_match, context = &captures))
if ak.vector_is_empty(entries):
this.m_contained_properties_cache[(usize)(u16)property_id] = false
ak.hash_map_remove_opaque(&this.m_properties, it)
## Ported from Libraries/LibWeb/CSS/CascadedProperties.cpp:71 Web::CSS::CascadedProperties::set_property.
pub func css_cascaded_properties_set_property(this: CssCascadedProperties*, property_id: css_data.PropertyId, value: CssStyleValue*, important: css_syntax.Important, origin: CssCascadeOrigin, layer_name: ak.FlyString?, source: CssStyleDeclaration*?, source_shadow_root: DomShadowRoot*?):
this.m_contained_properties_cache[(usize)(u16)property_id] = true
let entries = ak.hash_map_ensure(&this.m_properties, property_id)
var index = ak.vector_size(entries)
while index > 0:
index -= 1
let entry = ak.vector_at(entries, index)
if entry.origin == origin and css_optional_fly_strings_equal(entry.layer_name, layer_name):
if entry.property.important == css_syntax.Important.yes and important == css_syntax.Important.no:
return
entry.property = CssStyleProperty(important = important, property_id = property_id, value = value)
entry.cascade_index = this.m_next_cascade_index
this.m_next_cascade_index += 1
entry.source = source
entry.source_shadow_root = source_shadow_root
return
ak.vector_append_t(entries, CssCascadedPropertiesEntry(
property = CssStyleProperty(important = important, property_id = property_id, value = value),
cascade_index = this.m_next_cascade_index,
origin = origin,
layer_name = layer_name,
source = source,
source_shadow_root = source_shadow_root,
))
this.m_next_cascade_index += 1
## The last (winning) entry of `property_id`, which the cache says exists.
func css_cascaded_properties_last_entry(this: CssCascadedProperties*, property_id: css_data.PropertyId) -> CssCascadedPropertiesEntry*:
let entries = ak.hash_map_get_k(&this.m_properties, property_id) else trap("VERIFY: the property has entries")
return ak.vector_last(entries)
## Ported from Libraries/LibWeb/CSS/CascadedProperties.cpp:107 Web::CSS::CascadedProperties::property.
pub func css_cascaded_properties_property(this: CssCascadedProperties*, property_id: css_data.PropertyId) -> CssStyleValue*?:
if not this.m_contained_properties_cache[(usize)(u16)property_id]:
return none
return css_cascaded_properties_last_entry(this, property_id).property.value
## Ported from Libraries/LibWeb/CSS/CascadedProperties.cpp:115 Web::CSS::CascadedProperties::property_with_higher_priority.
pub func css_cascaded_properties_property_with_higher_priority(this: CssCascadedProperties*, first_property_id: css_data.PropertyId, second_property_id: css_data.PropertyId) -> css_data.PropertyId:
if not this.m_contained_properties_cache[(usize)(u16)first_property_id]:
return second_property_id
if not this.m_contained_properties_cache[(usize)(u16)second_property_id]:
return first_property_id
if css_cascaded_properties_last_entry(this, first_property_id).cascade_index >= css_cascaded_properties_last_entry(this, second_property_id).cascade_index:
return first_property_id
return second_property_id
## Ported from Libraries/LibWeb/CSS/CascadedProperties.cpp:129 Web::CSS::CascadedProperties::property_source.
pub func css_cascaded_properties_property_source(this: CssCascadedProperties*, property_id: css_data.PropertyId) -> CssStyleDeclaration*?:
if not this.m_contained_properties_cache[(usize)(u16)property_id]:
return none
return css_cascaded_properties_last_entry(this, property_id).source
## Ported from Libraries/LibWeb/CSS/CascadedProperties.cpp:137 Web::CSS::CascadedProperties::property_source_shadow_root.
pub func css_cascaded_properties_property_source_shadow_root(this: CssCascadedProperties*, property_id: css_data.PropertyId) -> DomShadowRoot*?:
if not this.m_contained_properties_cache[(usize)(u16)property_id]:
return none
return css_cascaded_properties_last_entry(this, property_id).source_shadow_root
## Ported from Libraries/LibWeb/CSS/CascadedProperties.cpp:145 Web::CSS::CascadedProperties::style_property.
pub func css_cascaded_properties_style_property(this: CssCascadedProperties*, property_id: css_data.PropertyId) -> CssStyleProperty?:
if not this.m_contained_properties_cache[(usize)(u16)property_id]:
return none
return css_cascaded_properties_last_entry(this, property_id).property