dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / css / style_computer_8.lucb
#==============================================================================================
#
# style_computer_8 - CSS::StyleComputer: position-area, math-depth, the ancestor filter and
# the rule cache buckets
#
# DESCRIPTION:
# Port of LibWeb/CSS/StyleComputer.cpp (lines 2795-3080): compute_position_area and
# compute_math_depth, the hashes an element contributes to the ancestor Bloom filter,
# resetting the filter and the :has() result cache, pushing and popping ancestors, and
# RuleCache's bucketing of rules (by id, class, tag name, pseudo-element, :root,
# attribute) and its lookup of the buckets an element may match.
#
# Copyright (c) 2018-2025, Andreas Kling <andreas@ladybird.org>
# Copyright (c) 2021, the SerenityOS developers.
# Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
# Copyright (c) 2024, Matthew Olsson <mattco@serenityos.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: position-area and math-depth ============================================================
## compute_position_area's `to_short_keyword` lambda.
func css_position_area_to_short_keyword(keyword_value: CssKeywordStyleValue*) -> CssKeywordStyleValue*:
match css_keyword_style_value_keyword(keyword_value):
.block_start, .inline_start:
return css_keyword_style_value_create(css_data.Keyword.start)
.block_end, .inline_end:
return css_keyword_style_value_create(css_data.Keyword.end)
.self_block_start, .self_inline_start:
return css_keyword_style_value_create(css_data.Keyword.self_start)
.self_block_end, .self_inline_end:
return css_keyword_style_value_create(css_data.Keyword.self_end)
.span_block_start, .span_inline_start:
return css_keyword_style_value_create(css_data.Keyword.span_start)
.span_block_end, .span_inline_end:
return css_keyword_style_value_create(css_data.Keyword.span_end)
.span_self_block_start, .span_self_inline_start:
return css_keyword_style_value_create(css_data.Keyword.span_self_start)
.span_self_block_end, .span_self_inline_end:
return css_keyword_style_value_create(css_data.Keyword.span_self_end)
_:
return keyword_value
## Ported from Libraries/LibWeb/CSS/StyleComputer.cpp:2795 Web::CSS::StyleComputer::compute_position_area.
# https://drafts.csswg.org/css-anchor-position/#position-area-computed
pub func css_style_computer_compute_position_area(absolutized_value: CssStyleValue*) -> CssStyleValue*:
# The computed value of a <position-area> value is the two keywords indicating the selected tracks in each axis,
# with the long (block-start) and short (start) logical keywords treated as equivalent. It serializes in the order
# given in the grammar (above), with the logical keywords serialized in their short forms (e.g. start start
# instead of block-start inline-start).
if css_style_value_is_keyword(absolutized_value):
return absolutized_value
let values = css_style_value_list_values(css_style_value_as_value_list(absolutized_value))
assert(ak.vector_size(&values) == 2)
let block_value = *ak.vector_at_const(&values, 0)
let inline_value = *ak.vector_at_const(&values, 1)
if css_keyword_style_value_keyword(css_style_value_as_keyword(block_value)) == css_data.Keyword.span_all:
match css_keyword_style_value_keyword(css_style_value_as_keyword(inline_value)):
.start:
return css_keyword_value_of(css_data.Keyword.inline_start)
.end:
return css_keyword_value_of(css_data.Keyword.inline_end)
.self_start:
return css_keyword_value_of(css_data.Keyword.self_inline_start)
.self_end:
return css_keyword_value_of(css_data.Keyword.self_inline_end)
.span_start:
return css_keyword_value_of(css_data.Keyword.span_inline_start)
.span_end:
return css_keyword_value_of(css_data.Keyword.span_inline_end)
.span_self_start:
return css_keyword_value_of(css_data.Keyword.span_self_inline_start)
.span_self_end:
return css_keyword_value_of(css_data.Keyword.span_self_inline_end)
_:
return absolutized_value
if css_keyword_style_value_keyword(css_style_value_as_keyword(inline_value)) == css_data.Keyword.span_all:
match css_keyword_style_value_keyword(css_style_value_as_keyword(block_value)):
.start:
return css_keyword_value_of(css_data.Keyword.block_start)
.end:
return css_keyword_value_of(css_data.Keyword.block_end)
.self_start:
return css_keyword_value_of(css_data.Keyword.self_block_start)
.self_end:
return css_keyword_value_of(css_data.Keyword.self_block_end)
.span_start:
return css_keyword_value_of(css_data.Keyword.span_block_start)
.span_end:
return css_keyword_value_of(css_data.Keyword.span_block_end)
.span_self_start:
return css_keyword_value_of(css_data.Keyword.span_self_block_start)
.span_self_end:
return css_keyword_value_of(css_data.Keyword.span_self_block_end)
_:
return absolutized_value
let short_block_value = css_position_area_to_short_keyword(css_style_value_as_keyword(block_value)).css_style_value()
let short_inline_value = css_position_area_to_short_keyword(css_style_value_as_keyword(inline_value)).css_style_value()
if not css_style_value_equals(block_value, short_block_value) or not css_style_value_equals(inline_value, short_inline_value):
var short_values: ak.Vector[CssStyleValue*]
ak.vector_append_t(&short_values, short_block_value)
ak.vector_append_t(&short_values, short_inline_value)
return css_style_value_list_create(short_values, CssStyleValueListSeparator.space).css_style_value()
return absolutized_value
## Ported from Libraries/LibWeb/CSS/StyleComputer.cpp:2894 Web::CSS::StyleComputer::compute_math_depth.
# https://w3c.github.io/mathml-core/#propdef-math-depth
pub func css_style_computer_compute_math_depth(absolutized_value: CssStyleValue*, inheritance_parent: DomAbstractElement?) -> CssStyleValue*:
var inherited_math_depth = css_initial_values_math_depth()
var inherited_math_style = css_initial_values_math_style()
if let parent = inheritance_parent:
let parent_computed_properties = dom_abstract_element_computed_properties(&parent) else trap("VERIFY: the parent has computed properties")
inherited_math_depth = css_computed_properties_math_depth(parent_computed_properties)
inherited_math_style = css_computed_properties_math_style(parent_computed_properties)
# The computed value of the math-depth value is determined as follows:
# - If the specified value of math-depth is auto-add and the inherited value of math-style is compact
# then the computed value of math-depth of the element is its inherited value plus one.
if css_style_value_to_keyword(absolutized_value) == css_data.Keyword.auto_add and inherited_math_style == css_data.MathStyle.compact:
return css_integer_style_value_create(inherited_math_depth + 1).css_style_value()
# - If the specified value of math-depth is of the form add(<integer>) then the computed value of
# math-depth of the element is its inherited value plus the specified integer.
if css_style_value_is_function(absolutized_value):
return css_integer_style_value_create(inherited_math_depth + css_int_from_style_value(css_function_style_value_value(css_style_value_as_function(absolutized_value)))).css_style_value()
# - If the specified value of math-depth is of the form <integer> then the computed value of math-depth
# of the element is the specified integer.
if css_style_value_is_integer(absolutized_value) or css_style_value_is_calculated(absolutized_value):
return css_integer_style_value_create(css_int_from_style_value(absolutized_value)).css_style_value()
# - Otherwise, the computed value of math-depth of the element is the inherited one.
return css_integer_style_value_create(inherited_math_depth).css_style_value()
# mark: The ancestor filter =====================================================================
## Ported from Libraries/LibWeb/CSS/StyleComputer.cpp:2924 Web::CSS::for_each_element_hash.
func css_for_each_element_hash(element: DomElement*, callback: ak.Function1[u32, unit]):
let local_name = dom_element_local_name(element)
ak.function1_call(&callback, ak.fly_string_ascii_case_insensitive_hash(&local_name))
if let id = dom_element_id(element):
ak.function1_call(&callback, ak.fly_string_hash(&id))
let class_names = dom_element_class_names(element)
for class_ in &ak.vector_span_const(&class_names):
ak.function1_call(&callback, ak.fly_string_hash(class_))
# element.for_each_attribute([&](auto& attribute) { ... })
if let attributes = element.m_attributes:
for i in 0..<dom_named_node_map_length(attributes):
let attribute = dom_named_node_map_item(attributes, (u32)i) else trap("NamedNodeMap::item in range")
let name = dom_attr_name(attribute)
ak.function1_call(&callback, ak.fly_string_ascii_case_insensitive_hash(&name))
## Ported from Libraries/LibWeb/CSS/StyleComputer.cpp:2936 Web::CSS::StyleComputer::reset_ancestor_filter.
pub func css_style_computer_reset_ancestor_filter(this: CssStyleComputer*):
css_counting_bloom_filter_clear(this.m_ancestor_filter else trap("VERIFY: the style computer has an ancestor filter"))
## Ported from Libraries/LibWeb/CSS/StyleComputer.cpp:2941 Web::CSS::StyleComputer::reset_has_result_cache.
pub func css_style_computer_reset_has_result_cache(this: CssStyleComputer*):
if let cache = this.m_has_result_cache:
ak.hash_map_clear(cache)
else:
this.m_has_result_cache = new ak.HashMap[SelectorEngineHasResultCacheKey, SelectorEngineHasMatchResult, SelectorEngineHasResultCacheKeyTraits] else trap("out of memory")
## push_ancestor's callback: `m_ancestor_filter->increment(hash)`.
func css_style_computer_increment_ancestor_hash(context: void*?, hash_: u32):
css_counting_bloom_filter_increment((CssCountingBloomFilter[u8]*)(context else trap("no captures")), hash_)
## pop_ancestor's callback: `m_ancestor_filter->decrement(hash)`.
func css_style_computer_decrement_ancestor_hash(context: void*?, hash_: u32):
css_counting_bloom_filter_decrement((CssCountingBloomFilter[u8]*)(context else trap("no captures")), hash_)
## Ported from Libraries/LibWeb/CSS/StyleComputer.cpp:2949 Web::CSS::StyleComputer::push_ancestor.
pub func css_style_computer_push_ancestor(this: CssStyleComputer*, element: DomElement*):
let ancestor_filter = this.m_ancestor_filter else trap("VERIFY: the style computer has an ancestor filter")
css_for_each_element_hash(element, ak.Function1[u32, unit](call = css_style_computer_increment_ancestor_hash, context = ancestor_filter))
## Ported from Libraries/LibWeb/CSS/StyleComputer.cpp:2956 Web::CSS::StyleComputer::pop_ancestor.
pub func css_style_computer_pop_ancestor(this: CssStyleComputer*, element: DomElement*):
let ancestor_filter = this.m_ancestor_filter else trap("VERIFY: the style computer has an ancestor filter")
css_for_each_element_hash(element, ak.Function1[u32, unit](call = css_style_computer_decrement_ancestor_hash, context = ancestor_filter))
# mark: RuleCache ===============================================================================
## RuleCache::add_rule's `add_to_*_bucket` lambdas: `bucket.ensure(name).append(matching_rule)`.
func css_rule_cache_add_to_bucket(bucket: ak.HashMap[ak.FlyString, ak.Vector[CssMatchingRule], ak.FlyStringTraits]*, name: ak.FlyString, matching_rule: const CssMatchingRule*):
ak.vector_append_t(ak.hash_map_ensure(bucket, name), *matching_rule)
## Ported from Libraries/LibWeb/CSS/StyleComputer.cpp:2963 Web::CSS::RuleCache::add_rule.
pub func css_rule_cache_add_rule(this: CssRuleCache*, matching_rule: const CssMatchingRule*, pseudo_element: css_data.PseudoElement?, contains_root_pseudo_class: bool):
if matching_rule.slotted:
ak.vector_append_t(&this.slotted_rules, *matching_rule)
return
if matching_rule.contains_part_pseudo_element:
ak.vector_append_t(&this.part_rules, *matching_rule)
return
# NOTE: We traverse the simple selectors in reverse order to make sure that class/ID buckets are preferred over tag buckets
# in the common case of div.foo or div#foo selectors.
let compound_selectors = css_selector_compound_selectors(matching_rule.selector)
let last_compound_selector = ak.vector_last_const(&compound_selectors)
var index = ak.vector_size(&last_compound_selector.simple_selectors)
while index > 0:
index -= 1
let simple_selector = ak.vector_at_const(&last_compound_selector.simple_selectors, index)
if simple_selector.type_ == CssSelectorSimpleSelectorType.id:
css_rule_cache_add_to_bucket(&this.rules_by_id, css_selector_simple_selector_name(simple_selector), matching_rule)
return
if simple_selector.type_ == CssSelectorSimpleSelectorType.class_:
css_rule_cache_add_to_bucket(&this.rules_by_class, css_selector_simple_selector_name(simple_selector), matching_rule)
return
if simple_selector.type_ == CssSelectorSimpleSelectorType.tag_name:
css_rule_cache_add_to_bucket(&this.rules_by_tag_name, css_selector_simple_selector_qualified_name(simple_selector).name.lowercase_name, matching_rule)
return
# NOTE: Selectors like `:is/where(.foo)` and `:is/where(.foo .bar)` are bucketed as class selectors for `foo` and `bar` respectively.
if let simplified = css_is_roundabout_selector_bucketable_as_something_simpler(simple_selector):
if simplified.type_ == CssSelectorSimpleSelectorType.tag_name:
css_rule_cache_add_to_bucket(&this.rules_by_tag_name, simplified.name, matching_rule)
return
if simplified.type_ == CssSelectorSimpleSelectorType.class_:
css_rule_cache_add_to_bucket(&this.rules_by_class, simplified.name, matching_rule)
return
if simplified.type_ == CssSelectorSimpleSelectorType.id:
css_rule_cache_add_to_bucket(&this.rules_by_id, simplified.name, matching_rule)
return
if matching_rule.contains_pseudo_element and pseudo_element != none:
let pseudo = pseudo_element else trap("unreachable: checked above")
if css_selector_pseudo_element_selector_is_known_pseudo_element_type(pseudo):
ak.vector_append_t(&this.rules_by_pseudo_element[(usize)pseudo], *matching_rule)
else:
# NOTE: We don't cache rules for unknown pseudo-elements. They can't match anything anyway.
_ = 0
elif contains_root_pseudo_class:
ak.vector_append_t(&this.root_rules, *matching_rule)
else:
for simple_selector in &ak.vector_span_const(&last_compound_selector.simple_selectors):
if simple_selector.type_ == CssSelectorSimpleSelectorType.attribute:
let attribute = css_selector_simple_selector_attribute(simple_selector)
ak.vector_append_t(ak.hash_map_ensure(&this.rules_by_attribute_name, attribute.qualified_name.name.lowercase_name), *matching_rule)
return
ak.vector_append_t(&this.other_rules, *matching_rule)
## Ported from Libraries/LibWeb/CSS/StyleComputer.cpp:3036 Web::CSS::RuleCache::for_each_matching_rules.
pub func css_rule_cache_for_each_matching_rules(this: const CssRuleCache*, abstract_element: DomAbstractElement, callback: ak.Function1[const ak.Vector[CssMatchingRule]*, ak.IterationDecision]):
let element = dom_abstract_element_element_const(&abstract_element)
let class_names = dom_element_class_names(element)
for class_name in ak.vector_span_const(&class_names):
if let rules = ak.hash_map_get_k_const(&this.rules_by_class, class_name):
if ak.function1_call(&callback, rules) == ak.IterationDecision.break_:
return
if let id = dom_element_id(element):
if let rules = ak.hash_map_get_k_const(&this.rules_by_id, id):
if ak.function1_call(&callback, rules) == ak.IterationDecision.break_:
return
if let rules = ak.hash_map_get_k_const(&this.rules_by_tag_name, dom_element_lowercased_local_name(element)):
if ak.function1_call(&callback, rules) == ak.IterationDecision.break_:
return
if let pseudo_element = dom_abstract_element_pseudo_element(&abstract_element):
if css_selector_pseudo_element_selector_is_known_pseudo_element_type(pseudo_element):
if ak.function1_call(&callback, &this.rules_by_pseudo_element[(usize)pseudo_element]) == ak.IterationDecision.break_:
return
else:
# NOTE: We don't cache rules for unknown pseudo-elements. They can't match anything anyway.
_ = 0
if dom_element_is_document_element(element):
if ak.function1_call(&callback, &this.root_rules) == ak.IterationDecision.break_:
return
var decision = ak.IterationDecision.continue_
# abstract_element.element().for_each_attribute([&](auto& name, auto&) { ... })
if let attributes = element.m_attributes:
for i in 0..<dom_named_node_map_length(attributes):
let attribute = dom_named_node_map_item(attributes, (u32)i) else trap("NamedNodeMap::item in range")
if let rules = ak.hash_map_get_k_const(&this.rules_by_attribute_name, dom_attr_name(attribute)):
decision = ak.function1_call(&callback, rules)
if decision == ak.IterationDecision.break_:
return
_ = ak.function1_call(&callback, &this.other_rules)