dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / css / style_invalidation_data_1.lucb
#==============================================================================================
#
# style_invalidation_data_1 - CSS::StyleInvalidationData I: invalidation rules and plans,
# simple-selector groups, and the :has() metadata of selectors
#
# DESCRIPTION:
# Port of LibWeb/CSS/StyleInvalidationData.cpp (lines 1-329) and the inline members of
# StyleInvalidationData.h (lines 19-97): merging descendant and sibling rules, plan
# equality and merging, the walk over groups of simple selectors, :has() argument scopes
# and the collection of the features :has() arguments use.
#
# Rules hold an InvalidationSet (a HashTable) by value: appending a rule copies it in C++,
# so the port appends a clone (css_*_invalidation_rule_clone) because the vector's rule is
# merged into later (DESIGN ยง3.3 rule 2).
#
# Copyright (c) 2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Invalidation rules ======================================================================
## A copy of `rule` with its own match set (the C++ copy constructor).
func css_descendant_invalidation_rule_clone(rule: const CssDescendantInvalidationRule*) -> CssDescendantInvalidationRule:
return CssDescendantInvalidationRule(match_set = css_invalidation_set_clone(&rule.match_set), match_any = rule.match_any, payload = rule.payload)
## A copy of `rule` with its own match set (the C++ copy constructor).
func css_sibling_invalidation_rule_clone(rule: const CssSiblingInvalidationRule*) -> CssSiblingInvalidationRule:
return CssSiblingInvalidationRule(reach = rule.reach, match_set = css_invalidation_set_clone(&rule.match_set), match_any = rule.match_any, payload = rule.payload)
## Ported from Libraries/LibWeb/CSS/StyleInvalidationData.cpp:14 Web::CSS::append_or_merge_descendant_rule.
func css_append_or_merge_descendant_rule(rules: ak.Vector[CssDescendantInvalidationRule]*, rule: const CssDescendantInvalidationRule*):
for existing_rule in &ak.vector_span(rules):
if existing_rule.match_any != rule.match_any:
continue
if not css_invalidation_plan_eq(existing_rule.payload, *rule.payload):
continue
if existing_rule.match_any:
return
css_invalidation_set_include_all_from(&existing_rule.match_set, rule.match_set)
return
ak.vector_append_t(rules, css_descendant_invalidation_rule_clone(rule))
## Ported from Libraries/LibWeb/CSS/StyleInvalidationData.cpp:31 Web::CSS::append_or_merge_sibling_rule.
func css_append_or_merge_sibling_rule(rules: ak.Vector[CssSiblingInvalidationRule]*, rule: const CssSiblingInvalidationRule*):
for existing_rule in &ak.vector_span(rules):
if existing_rule.reach != rule.reach:
continue
if existing_rule.match_any != rule.match_any:
continue
if not css_invalidation_plan_eq(existing_rule.payload, *rule.payload):
continue
if existing_rule.match_any:
return
css_invalidation_set_include_all_from(&existing_rule.match_set, rule.match_set)
return
ak.vector_append_t(rules, css_sibling_invalidation_rule_clone(rule))
## Ported from Libraries/LibWeb/CSS/StyleInvalidationData.cpp:50 Web::CSS::InvalidationPlan::is_empty.
pub func css_invalidation_plan_is_empty(this: const CssInvalidationPlan*) -> bool:
return not this.invalidate_self and not this.invalidate_whole_subtree and ak.vector_is_empty(&this.descendant_rules) and ak.vector_is_empty(&this.sibling_rules)
## Ported from Libraries/LibWeb/CSS/StyleInvalidationData.cpp:55 Web::CSS::DescendantInvalidationRule::operator==.
pub func css_descendant_invalidation_rule_eq(this: const CssDescendantInvalidationRule*, other: CssDescendantInvalidationRule) -> bool:
return css_invalidation_set_eq(&this.match_set, other.match_set) and this.match_any == other.match_any and css_invalidation_plan_eq(this.payload, *other.payload)
## Ported from Libraries/LibWeb/CSS/StyleInvalidationData.cpp:62 Web::CSS::SiblingInvalidationRule::operator==.
pub func css_sibling_invalidation_rule_eq(this: const CssSiblingInvalidationRule*, other: CssSiblingInvalidationRule) -> bool:
return this.reach == other.reach and css_invalidation_set_eq(&this.match_set, other.match_set) and this.match_any == other.match_any and css_invalidation_plan_eq(this.payload, *other.payload)
# mark: Invalidation plans ======================================================================
## Ported from Libraries/LibWeb/CSS/StyleInvalidationData.h:62 Web::CSS::InvalidationPlan::create.
pub func css_invalidation_plan_create() -> CssInvalidationPlan*:
return new CssInvalidationPlan else trap("out of memory")
## Ported from Libraries/LibWeb/CSS/StyleInvalidationData.cpp:71 Web::CSS::rule_lists_are_equal_ignoring_order, for
## descendant rules.
func css_rule_lists_are_equal_ignoring_order_descendant(a: const ak.Vector[CssDescendantInvalidationRule]*, b: const ak.Vector[CssDescendantInvalidationRule]*) -> bool:
if ak.vector_size(a) != ak.vector_size(b):
return false
var matched_rules: ak.Vector[bool]
ak.vector_resize(&matched_rules, ak.vector_size(b))
let b_rules = ak.vector_span_const(b)
for rule in &ak.vector_span_const(a):
var found_match = false
for i in 0..<b_rules.length:
if *ak.vector_at(&matched_rules, i):
continue
if not css_descendant_invalidation_rule_eq(rule, b_rules[i]):
continue
*ak.vector_at(&matched_rules, i) = true
found_match = true
break
if not found_match:
return false
return true
## Ported from Libraries/LibWeb/CSS/StyleInvalidationData.cpp:71 Web::CSS::rule_lists_are_equal_ignoring_order, for
## sibling rules.
func css_rule_lists_are_equal_ignoring_order_sibling(a: const ak.Vector[CssSiblingInvalidationRule]*, b: const ak.Vector[CssSiblingInvalidationRule]*) -> bool:
if ak.vector_size(a) != ak.vector_size(b):
return false
var matched_rules: ak.Vector[bool]
ak.vector_resize(&matched_rules, ak.vector_size(b))
let b_rules = ak.vector_span_const(b)
for rule in &ak.vector_span_const(a):
var found_match = false
for i in 0..<b_rules.length:
if *ak.vector_at(&matched_rules, i):
continue
if not css_sibling_invalidation_rule_eq(rule, b_rules[i]):
continue
*ak.vector_at(&matched_rules, i) = true
found_match = true
break
if not found_match:
return false
return true
## Ported from Libraries/LibWeb/CSS/StyleInvalidationData.cpp:97 Web::CSS::InvalidationPlan::operator==.
pub func css_invalidation_plan_eq(this: const CssInvalidationPlan*, other: CssInvalidationPlan) -> bool:
if this.invalidate_self != other.invalidate_self:
return false
if this.invalidate_whole_subtree != other.invalidate_whole_subtree:
return false
return css_rule_lists_are_equal_ignoring_order_descendant(&this.descendant_rules, &other.descendant_rules) and css_rule_lists_are_equal_ignoring_order_sibling(&this.sibling_rules, &other.sibling_rules)
## Ported from Libraries/LibWeb/CSS/StyleInvalidationData.cpp:108 Web::CSS::InvalidationPlan::include_all_from.
pub func css_invalidation_plan_include_all_from(this: CssInvalidationPlan*, other: CssInvalidationPlan):
this.invalidate_self = this.invalidate_self or other.invalidate_self
if this.invalidate_whole_subtree:
return
if other.invalidate_whole_subtree:
this.invalidate_whole_subtree = true
ak.vector_clear(&this.descendant_rules)
ak.vector_clear(&this.sibling_rules)
return
for descendant_rule in &ak.vector_span_const(&other.descendant_rules):
css_append_or_merge_descendant_rule(&this.descendant_rules, descendant_rule)
for sibling_rule in &ak.vector_span_const(&other.sibling_rules):
css_append_or_merge_sibling_rule(&this.sibling_rules, sibling_rule)
# mark: Groups of simple selectors ==============================================================
## Ported from Libraries/LibWeb/CSS/StyleInvalidationData.cpp:132 Web::CSS::for_each_consecutive_simple_selector_group.
## Iterates over the given selector, grouping consecutive simple selectors that have no combinator (Combinator::None).
## For example, given "div:not(.a) + .b[foo]", the loop body runs twice:
## once for "div:not(.a)" and once for ".b[foo]".
## (The C++ calls back; the port is the loop of CssConsecutiveSimpleSelectorGroups.)
pub func css_for_each_consecutive_simple_selector_group(selector: const CssSelector*) -> CssConsecutiveSimpleSelectorGroups:
return CssConsecutiveSimpleSelectorGroups(selector = selector)
## The loop body of for_each_consecutive_simple_selector_group between two callbacks: the next group, or none at the
## end. The C++ calls back when it reaches the next compound with selectors collected, then goes on with that
## compound; the iterator collects that compound before answering the group.
pub func css_consecutive_simple_selector_group_iterator_next(this: CssConsecutiveSimpleSelectorGroupIterator*) -> CssSimpleSelectorGroup?:
while this.compound_selector_index >= 0:
var group: CssSimpleSelectorGroup? = none
if not ak.vector_is_empty(&this.simple_selectors):
group = CssSimpleSelectorGroup(simple_selectors = this.simple_selectors, combinator = this.combinator, is_rightmost = this.is_rightmost)
this.simple_selectors = ak.Vector[const CssSelectorSimpleSelector*]()
this.is_rightmost = false
let compound_selector = ak.vector_at(&this.compound_selectors, (usize)this.compound_selector_index)
for simple_selector in &ak.vector_span(&compound_selector.simple_selectors):
ak.vector_append_t(&this.simple_selectors, (const CssSelectorSimpleSelector*)simple_selector)
this.combinator = compound_selector.combinator
this.compound_selector_index -= 1
if group != none:
return group
if this.finished:
return none
this.finished = true
if not ak.vector_is_empty(&this.simple_selectors):
return CssSimpleSelectorGroup(simple_selectors = this.simple_selectors, combinator = this.combinator, is_rightmost = this.is_rightmost)
return none
# mark: :has() metadata =========================================================================
## Ported from Libraries/LibWeb/CSS/StyleInvalidationData.cpp:157 Web::CSS::classify_has_argument_scope.
func css_classify_has_argument_scope(selector: const CssSelector*) -> CssHasArgumentScope:
if ak.vector_is_empty(&selector.m_compound_selectors):
return CssHasArgumentScope.complex
let compound_selectors = css_selector_compound_selectors(selector)
let leftmost_combinator = ak.vector_span_const(&compound_selectors)[0].combinator
let is_single_compound = ak.vector_size(&compound_selectors) == 1
match leftmost_combinator:
.descendant:
return CssHasArgumentScope.all_descendants
.immediate_child:
return CssHasArgumentScope.children_only if is_single_compound else CssHasArgumentScope.complex
.next_sibling:
return CssHasArgumentScope.next_sibling_only if is_single_compound else CssHasArgumentScope.complex
.subsequent_sibling:
return CssHasArgumentScope.all_following_siblings if is_single_compound else CssHasArgumentScope.complex
_:
return CssHasArgumentScope.complex
## Ported from Libraries/LibWeb/CSS/StyleInvalidationData.cpp:178 Web::CSS::append_has_invalidation_metadata.
func css_append_has_invalidation_metadata[TKey, TKeyTraits: ak.Traits[TKey]](map: ak.HashMap[TKey, ak.Vector[CssHasInvalidationMetadata], TKeyTraits]*, key: TKey, metadata: CssHasInvalidationMetadata):
let bucket = ak.hash_map_ensure(map, key)
for existing in ak.vector_span(bucket):
if existing == metadata:
return
ak.vector_append_t(bucket, metadata)
## Ported from Libraries/LibWeb/CSS/StyleInvalidationData.cpp:187 Web::CSS::pseudo_class_can_be_used_as_has_invalidation_feature.
func css_pseudo_class_can_be_used_as_has_invalidation_feature(pseudo_class: css_data.PseudoClass) -> bool:
match pseudo_class:
.enabled, .disabled, .defined, .placeholder_shown, .checked, .required, .optional, .link, .any_link, .local_link:
return true
_:
return false
## Ported from Libraries/LibWeb/CSS/StyleInvalidationData.cpp:202 Web::CSS::collect_properties_used_in_has.
func css_collect_properties_used_in_has(selector: const CssSelectorSimpleSelector*, style_invalidation_data: CssStyleInvalidationData*, metadata: CssHasInvalidationMetadata?):
match selector.type_:
.id:
if let metadata_ = metadata:
css_append_has_invalidation_metadata(&style_invalidation_data.ids_used_in_has_selectors, css_selector_simple_selector_name(selector), metadata_)
.class_:
if let metadata_ = metadata:
css_append_has_invalidation_metadata(&style_invalidation_data.class_names_used_in_has_selectors, css_selector_simple_selector_name(selector), metadata_)
.attribute:
if let metadata_ = metadata:
css_append_has_invalidation_metadata(&style_invalidation_data.attribute_names_used_in_has_selectors, css_selector_simple_selector_attribute(selector).qualified_name.name.lowercase_name, metadata_)
.tag_name:
if let metadata_ = metadata:
css_append_has_invalidation_metadata(&style_invalidation_data.tag_names_used_in_has_selectors, css_selector_simple_selector_qualified_name(selector).name.lowercase_name, metadata_)
.pseudo_class:
let pseudo_class = css_selector_simple_selector_pseudo_class(selector)
if css_pseudo_class_can_be_used_as_has_invalidation_feature(pseudo_class.type_):
if let metadata_ = metadata:
css_append_has_invalidation_metadata(&style_invalidation_data.pseudo_classes_used_in_has_selectors, pseudo_class.type_, metadata_)
elif metadata != none and not (pseudo_class.type_ == css_data.PseudoClass.has or pseudo_class.type_ == css_data.PseudoClass.is or pseudo_class.type_ == css_data.PseudoClass.where):
# The structural subtree filter can only compare concrete features and the pseudo-classes listed above.
# For other pseudo-classes, such as :focus, :default, and :valid, a featureless node can still start or
# stop matching. Keep the old conservative walk instead of trying to probe them generically.
style_invalidation_data.has_selectors_sensitive_to_featureless_subtree_changes = true
for child_selector in ak.vector_span_const(&pseudo_class.argument_selector_list):
var child_metadata = metadata
if pseudo_class.type_ == css_data.PseudoClass.has:
child_metadata = CssHasInvalidationMetadata(relative_selector = child_selector, scope = css_classify_has_argument_scope(child_selector))
# These selectors can match because a featureless node is inserted, removed, or moved.
# Since there is no concrete tag/class/id/attribute/pseudo-class feature to compare against
# later, structural invalidation must keep walking conservatively for them.
if css_selector_contains_featureless_subtree_sensitive_selector(child_selector):
style_invalidation_data.has_selectors_sensitive_to_featureless_subtree_changes = true
for compound_selector in &ak.vector_span_const(&child_selector.m_compound_selectors):
for simple_selector in &ak.vector_span_const(&compound_selector.simple_selectors):
css_collect_properties_used_in_has(simple_selector, style_invalidation_data, child_metadata)
.pseudo_element:
# Pseudo-elements like ::slotted(.x:has(...)) carry a compound selector argument whose contents need the same
# recursive collection.
let pseudo_element = css_selector_simple_selector_pseudo_element(selector)
if css_selector_pseudo_element_selector_type(&pseudo_element) == css_data.PseudoElement.slotted:
let compound = css_selector_pseudo_element_selector_compound_selector(&pseudo_element)
for compound_selector in &ak.vector_span_const(&compound.m_compound_selectors):
for simple_selector in &ak.vector_span_const(&compound_selector.simple_selectors):
css_collect_properties_used_in_has(simple_selector, style_invalidation_data, metadata)
_:
return
## Ported from Libraries/LibWeb/CSS/StyleInvalidationData.cpp:273 Web::CSS::simple_selector_is_featureless_subtree_sensitive.
func css_simple_selector_is_featureless_subtree_sensitive(selector: const CssSelectorSimpleSelector*) -> bool:
match selector.type_:
.universal:
return true
.pseudo_class:
let pseudo_class = css_selector_simple_selector_pseudo_class(selector)
match pseudo_class.type_:
.not_, .empty, .first_child, .last_child, .only_child, .first_of_type, .last_of_type, .only_of_type, .nth_child, .nth_last_child, .nth_of_type, .nth_last_of_type:
return true
.is, .where:
for child_selector in ak.vector_span_const(&pseudo_class.argument_selector_list):
if css_selector_contains_featureless_subtree_sensitive_selector(child_selector):
return true
return false
_:
return false
_:
return false
## Ported from Libraries/LibWeb/CSS/StyleInvalidationData.cpp:310 Web::CSS::selector_contains_featureless_subtree_sensitive_selector.
func css_selector_contains_featureless_subtree_sensitive_selector(selector: const CssSelector*) -> bool:
for compound_selector in &ak.vector_span_const(&selector.m_compound_selectors):
for simple_selector in &ak.vector_span_const(&compound_selector.simple_selectors):
if css_simple_selector_is_featureless_subtree_sensitive(simple_selector):
return true
return false