dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / css / invalidation / style_invalidator.lucb
#==============================================================================================
#
# style_invalidator - CSS::Invalidation::StyleInvalidator: pending descendant and sibling
# invalidation rules, applied in one pass over the tree before a style
# update
#
# DESCRIPTION:
# Port of LibWeb/CSS/Invalidation/StyleInvalidator.cpp (lines 1-188) and the inline
# member of StyleInvalidator.h (line 23). enqueue_invalidation_plan applies a plan's self
# and sibling parts at once and keeps its descendant rules per node;
# perform_pending_style_invalidations walks the tree carrying the active descendant
# rules and turns "entire subtree needs style update" into per-node flags.
#
# Copyright (c) 2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Matching and pending rules ==============================================================
## Ported from Libraries/LibWeb/CSS/Invalidation/StyleInvalidator.cpp:18 Web::CSS::Invalidation::element_matches_invalidation_rule.
func css_invalidation_element_matches_invalidation_rule(element: DomElement*, match_set: const CssInvalidationSet*, match_any: bool) -> bool:
return match_any or css_invalidation_element_matches_any_invalidation_set_property(element, match_set)
## Ported from Libraries/LibWeb/CSS/Invalidation/StyleInvalidator.h:30 Web::CSS::Invalidation::StyleInvalidator::PendingDescendantInvalidation::operator==
## (defaulted: the reason, then the rule).
func css_style_invalidator_pending_descendant_invalidation_eq(a: const CssStyleInvalidatorPendingDescendantInvalidation*, b: const CssStyleInvalidatorPendingDescendantInvalidation*) -> bool:
return a.reason == b.reason and css_descendant_invalidation_rule_eq(&a.rule, b.rule)
## `Vector<PendingDescendantInvalidation>::contains_slow(pending_invalidation)`.
func css_style_invalidator_pending_invalidations_contain(pending_invalidations: const ak.Vector[CssStyleInvalidatorPendingDescendantInvalidation]*, pending_invalidation: const CssStyleInvalidatorPendingDescendantInvalidation*) -> bool:
for existing in &ak.vector_span_const(pending_invalidations):
if css_style_invalidator_pending_descendant_invalidation_eq(existing, pending_invalidation):
return true
return false
## Ported from Libraries/LibWeb/CSS/Invalidation/StyleInvalidator.cpp:23 Web::CSS::Invalidation::StyleInvalidator::visit_edges (override).
pub func css_style_invalidator_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (CssStyleInvalidator*)base
gc.cell_visit_edges_impl(base, visitor)
for it in ak.hash_map_begin(&this.m_pending_invalidations):
gc.cell_visitor_visit_t(visitor, it.key)
## Ported from Libraries/LibWeb/CSS/Invalidation/StyleInvalidator.h:23 Web::CSS::Invalidation::StyleInvalidator::has_pending_invalidations.
pub func css_style_invalidator_has_pending_invalidations(this: CssStyleInvalidator*) -> bool:
return not ak.hash_map_is_empty(&this.m_pending_invalidations)
## Ported from Libraries/LibWeb/CSS/Invalidation/StyleInvalidator.cpp:30 Web::CSS::Invalidation::StyleInvalidator::invalidate.
pub func css_style_invalidator_invalidate(this: CssStyleInvalidator*, node: DomNode*):
css_style_invalidator_perform_pending_style_invalidations(this, node, false)
ak.hash_map_clear(&this.m_pending_invalidations)
## Ported from Libraries/LibWeb/CSS/Invalidation/StyleInvalidator.cpp:36 Web::CSS::Invalidation::StyleInvalidator::enqueue_invalidation_plan.
pub func css_style_invalidator_enqueue_invalidation_plan(this: CssStyleInvalidator*, node: DomNode*, reason: DomStyleInvalidationReason, plan: const CssInvalidationPlan*) -> bool:
if css_invalidation_plan_is_empty(plan):
return false
if plan.invalidate_whole_subtree:
dom_node_invalidate_style(node, reason)
return true
if plan.invalidate_self:
dom_node_set_needs_style_update(node, true)
css_style_invalidator_add_pending_invalidation(this, node, reason, plan)
if let element = as_if_dom_element(node):
for sibling_rule in &ak.vector_span_const(&plan.sibling_rules):
css_style_invalidator_apply_sibling_invalidation(this, element, reason, sibling_rule)
return false
## Ported from Libraries/LibWeb/CSS/Invalidation/StyleInvalidator.cpp:59 Web::CSS::Invalidation::StyleInvalidator::add_pending_invalidation.
func css_style_invalidator_add_pending_invalidation(this: CssStyleInvalidator*, node: DomNode*, reason: DomStyleInvalidationReason, plan: const CssInvalidationPlan*):
if ak.vector_is_empty(&plan.descendant_rules):
return
let pending_invalidations = ak.hash_map_ensure(&this.m_pending_invalidations, node)
for descendant_rule in ak.vector_span_const(&plan.descendant_rules):
let pending_invalidation = CssStyleInvalidatorPendingDescendantInvalidation(reason = reason, rule = descendant_rule)
if not css_style_invalidator_pending_invalidations_contain(pending_invalidations, &pending_invalidation):
ak.vector_append_t(pending_invalidations, pending_invalidation)
## Ported from Libraries/LibWeb/CSS/Invalidation/StyleInvalidator.cpp:74 Web::CSS::Invalidation::StyleInvalidator::apply_invalidation_plan.
func css_style_invalidator_apply_invalidation_plan(this: CssStyleInvalidator*, element: DomElement*, reason: DomStyleInvalidationReason, plan: const CssInvalidationPlan*, invalidate_entire_subtree: bool*):
if css_invalidation_plan_is_empty(plan):
return
if plan.invalidate_whole_subtree:
dom_node_invalidate_style(element.dom_node(), reason)
*invalidate_entire_subtree = true
dom_node_set_needs_style_update_internal(element.dom_node(), true)
if dom_node_has_child_nodes(element.dom_node()):
dom_node_set_child_needs_style_update(element.dom_node(), true)
return
if plan.invalidate_self:
dom_node_set_needs_style_update(element.dom_node(), true)
for descendant_rule in ak.vector_span_const(&plan.descendant_rules):
let pending_invalidation = CssStyleInvalidatorPendingDescendantInvalidation(reason = reason, rule = descendant_rule)
if not css_style_invalidator_pending_invalidations_contain(&this.m_active_descendant_invalidations, &pending_invalidation):
ak.vector_append_t(&this.m_active_descendant_invalidations, pending_invalidation)
for sibling_rule in &ak.vector_span_const(&plan.sibling_rules):
css_style_invalidator_apply_sibling_invalidation(this, element, reason, sibling_rule)
## apply_sibling_invalidation's `apply_if_matching` lambda.
func css_style_invalidator_apply_if_matching(this: CssStyleInvalidator*, reason: DomStyleInvalidationReason, sibling_rule: const CssSiblingInvalidationRule*, sibling: DomElement*?):
let sibling_ = sibling else return
if not css_invalidation_element_matches_invalidation_rule(sibling_, &sibling_rule.match_set, sibling_rule.match_any):
return
_ = css_style_invalidator_enqueue_invalidation_plan(this, sibling_.dom_node(), reason, sibling_rule.payload)
## Ported from Libraries/LibWeb/CSS/Invalidation/StyleInvalidator.cpp:101 Web::CSS::Invalidation::StyleInvalidator::apply_sibling_invalidation.
func css_style_invalidator_apply_sibling_invalidation(this: CssStyleInvalidator*, element: DomElement*, reason: DomStyleInvalidationReason, sibling_rule: const CssSiblingInvalidationRule*):
match sibling_rule.reach:
.adjacent:
css_style_invalidator_apply_if_matching(this, reason, sibling_rule, dom_non_document_type_child_node_next_element_sibling(element.dom_node()))
.subsequent:
var sibling = dom_non_document_type_child_node_next_element_sibling(element.dom_node())
while let current = sibling:
css_style_invalidator_apply_if_matching(this, reason, sibling_rule, current)
sibling = dom_non_document_type_child_node_next_element_sibling(current.dom_node())
_:
trap("VERIFY_NOT_REACHED")
# mark: The pass over the tree ==================================================================
## Ported from Libraries/LibWeb/CSS/Invalidation/StyleInvalidator.cpp:127 Web::CSS::Invalidation::StyleInvalidator::perform_pending_style_invalidations.
# This function makes a full pass over the entire DOM and:
# - converts "entire subtree needs style update" into "needs style update" for each inclusive descendant where it's found.
# - applies descendant invalidation rules to matching elements
func css_style_invalidator_perform_pending_style_invalidations(this: CssStyleInvalidator*, node: DomNode*, invalidate_entire_subtree_: bool):
var invalidate_entire_subtree = invalidate_entire_subtree_ or dom_node_entire_subtree_needs_style_update(node)
let element = as_if_dom_element(node)
if invalidate_entire_subtree:
dom_node_set_needs_style_update_internal(node, true)
if dom_node_has_child_nodes(node):
dom_node_set_child_needs_style_update(node, true)
let previous_active_descendant_invalidations_size = ak.vector_size(&this.m_active_descendant_invalidations)
# ScopeGuard restore_state
defer ak.vector_shrink(&this.m_active_descendant_invalidations, previous_active_descendant_invalidations_size)
if not invalidate_entire_subtree:
if let pending_invalidations = ak.hash_map_get_k(&this.m_pending_invalidations, node):
ak.vector_extend_2(&this.m_active_descendant_invalidations, pending_invalidations)
if let element_ = element:
var invalidation_index: usize = 0
while invalidation_index < ak.vector_size(&this.m_active_descendant_invalidations):
let pending_invalidation = *ak.vector_at(&this.m_active_descendant_invalidations, invalidation_index)
invalidation_index += 1
if not css_invalidation_element_matches_invalidation_rule(element_, &pending_invalidation.rule.match_set, pending_invalidation.rule.match_any):
continue
css_style_invalidator_apply_invalidation_plan(this, element_, pending_invalidation.reason, pending_invalidation.rule.payload, &invalidate_entire_subtree)
if invalidate_entire_subtree:
break
if invalidate_entire_subtree:
dom_node_set_needs_style_update_internal(node, true)
if dom_node_has_child_nodes(node):
dom_node_set_child_needs_style_update(node, true)
if let element_ = element:
dom_element_clear_removed_attributes_for_style_invalidation(element_)
var child = node.tree_node.m_first_child
while let current = child:
css_style_invalidator_perform_pending_style_invalidations(this, current, invalidate_entire_subtree)
child = current.tree_node.m_next_sibling
if let element_ = as_if_dom_element(node):
if let shadow_root = dom_element_shadow_root(element_):
css_style_invalidator_perform_pending_style_invalidations(this, shadow_root.dom_node(), invalidate_entire_subtree)
if invalidate_entire_subtree or dom_node_needs_style_update(shadow_root.dom_node()) or dom_node_child_needs_style_update(shadow_root.dom_node()):
var ancestor: DomNode*? = node
while let current = ancestor:
dom_node_set_child_needs_style_update(current, true)
ancestor = (DomNode*?)dom_node_parent_or_shadow_host(current)
dom_node_set_entire_subtree_needs_style_update(node, false)