dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / css / invalidation / node_invalidator.lucb
#==============================================================================================
#
# node_invalidator - style invalidation of a node: the whole-subtree path of every DOM
# mutation, and the targeted path of feature changes
#
# DESCRIPTION:
# Port of LibWeb/CSS/Invalidation/NodeInvalidator.cpp (lines 1-134):
# invalidate_node_style (Node::invalidate_style(reason)) marks the node's subtree, its
# structurally affected siblings and its ancestors; invalidate_node_style_for_properties
# (Node::invalidate_style(reason, properties, options)) schedules :has() invalidation
# and enqueues the invalidation plans of the changed features in each observing scope.
#
# Copyright (c) 2026-present, the Ladybird developers
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## `dbgln_if(STYLE_INVALIDATION_DEBUG, ...)` (AK/Debug.h; off by default).
let css_invalidation_style_invalidation_debug: bool = false
## Ported from Libraries/LibWeb/CSS/Invalidation/NodeInvalidator.cpp:21 Web::CSS::Invalidation::to_string.
pub func css_invalidation_to_string(reason: DomStyleInvalidationReason) -> ak.StringView:
match reason:
.adopted_style_sheets_list: return ak.sv("AdoptedStyleSheetsList")
.base_url_changed: return ak.sv("BaseURLChanged")
.counter_style_cache_invalidated: return ak.sv("CounterStyleCacheInvalidated")
.css_font_loaded: return ak.sv("CSSFontLoaded")
.css_import_rule: return ak.sv("CSSImportRule")
.css_style_properties_remove_property: return ak.sv("CSSStylePropertiesRemoveProperty")
.css_style_properties_set_property: return ak.sv("CSSStylePropertiesSetProperty")
.css_style_properties_set_property_style_value: return ak.sv("CSSStylePropertiesSetPropertyStyleValue")
.css_style_properties_text_change: return ak.sv("CSSStylePropertiesTextChange")
.custom_element_state_change: return ak.sv("CustomElementStateChange")
.custom_state_set_change: return ak.sv("CustomStateSetChange")
.editing_insertion: return ak.sv("EditingInsertion")
.editing_deletion: return ak.sv("EditingDeletion")
.element_attribute_change: return ak.sv("ElementAttributeChange")
.element_set_shadow_root: return ak.sv("ElementSetShadowRoot")
.element_set_active: return ak.sv("ElementSetActive")
.fullscreen: return ak.sv("Fullscreen")
.html_dialog_element_set_is_modal: return ak.sv("HTMLDialogElementSetIsModal")
.html_details_or_dialog_open_attribute_change: return ak.sv("HTMLDetailsOrDialogOpenAttributeChange")
.html_hyperlink_element_href_change: return ak.sv("HTMLHyperlinkElementHrefChange")
.htmli_frame_element_geometry_change: return ak.sv("HTMLIFrameElementGeometryChange")
.html_input_element_set_checked: return ak.sv("HTMLInputElementSetChecked")
.html_input_element_set_is_open: return ak.sv("HTMLInputElementSetIsOpen")
.html_input_element_set_type: return ak.sv("HTMLInputElementSetType")
.html_object_element_update_layout_and_child_objects: return ak.sv("HTMLObjectElementUpdateLayoutAndChildObjects")
.html_option_element_selected_change: return ak.sv("HTMLOptionElementSelectedChange")
.html_select_element_set_is_open: return ak.sv("HTMLSelectElementSetIsOpen")
.media_list_set_media_text: return ak.sv("MediaListSetMediaText")
.media_list_append_medium: return ak.sv("MediaListAppendMedium")
.media_list_delete_medium: return ak.sv("MediaListDeleteMedium")
.media_query_changed_match_state: return ak.sv("MediaQueryChangedMatchState")
.navigable_set_viewport_size: return ak.sv("NavigableSetViewportSize")
.node_insert_before: return ak.sv("NodeInsertBefore")
.node_remove: return ak.sv("NodeRemove")
.node_set_text_content: return ak.sv("NodeSetTextContent")
.other: return ak.sv("Other")
.set_selector_text: return ak.sv("SetSelectorText")
.settings_change: return ak.sv("SettingsChange")
.style_sheet_disabled_state_change: return ak.sv("StyleSheetDisabledStateChange")
.style_sheet_delete_rule: return ak.sv("StyleSheetDeleteRule")
.style_sheet_insert_rule: return ak.sv("StyleSheetInsertRule")
.style_sheet_list_add_sheet: return ak.sv("StyleSheetListAddSheet")
.style_sheet_list_remove_sheet: return ak.sv("StyleSheetListRemoveSheet")
.style_sheet_replace: return ak.sv("StyleSheetReplace")
_: trap("VERIFY_NOT_REACHED")
## Ported from Libraries/LibWeb/CSS/Invalidation/NodeInvalidator.cpp:33 Web::CSS::Invalidation::invalidate_node_style.
pub func css_invalidation_invalidate_node_style(node: DomNode*, reason: DomStyleInvalidationReason):
css_invalidation_schedule_has_invalidation_for_node(node, reason)
# Character data nodes have no style of their own, so once :has() ancestor invalidation has been scheduled there
# is nothing else to do.
if dom_node_is_character_data(node):
return
if not dom_node_needs_style_update(node) and not dom_document_needs_full_style_update(dom_node_document(node)):
if css_invalidation_style_invalidation_debug:
let reason_name = css_invalidation_to_string(reason)
let description = dom_node_debug_description(node)
let description_view = ak.string_bytes_as_string_view(&description)
ak.dbgln(f"Invalidate style ({(str)ak.string_view_bytes(&reason_name)}): {(str)ak.string_view_bytes(&description_view)}")
if let document = as_if_dom_document(node):
dom_document_record_full_style_invalidation(document)
dom_document_set_needs_full_style_update(document, true)
return
# If the document is already marked for a full style update, there's no need to do anything here.
if dom_document_needs_full_style_update(dom_node_document(node)):
return
# If any ancestor is already marked for an entire subtree update, there's no need to do anything here.
var ancestor = dom_node_parent_or_shadow_host(node)
while let current = ancestor:
if dom_node_entire_subtree_needs_style_update(current.dom_node()):
return
ancestor = dom_node_parent_or_shadow_host(current.dom_node())
# When invalidating style for a node, we actually invalidate:
# - the node itself
# - all of its descendants
# - preceding siblings that depend on following-sibling counts (only on DOM insert/remove)
# - subsequent siblings that depend on previous siblings or sibling combinators
# FIXME: This is a lot of invalidation and we should implement more sophisticated invalidation to do less work!
dom_node_set_entire_subtree_needs_style_update(node, true)
css_invalidation_invalidate_structurally_affected_siblings(node, reason)
css_invalidation_mark_ancestors_as_having_child_needing_style_update(node)
## The captures of invalidate_node_style_for_properties' for_each_style_scope_which_may_observe_the_node lambda.
struct CssInvalidationInvalidateNodeStyleForPropertiesScopes:
var style_scope: CssStyleScope*
var additional_scopes: ak.Vector[CssStyleScope*]
## invalidate_node_style_for_properties' for_each_style_scope_which_may_observe_the_node lambda.
func css_invalidation_invalidate_node_style_for_properties_collect_scope(context: void*?, scope: CssStyleScope*):
let captures = (CssInvalidationInvalidateNodeStyleForPropertiesScopes*)(context else trap("no captures"))
if scope == captures.style_scope:
return
ak.vector_append_t(&captures.additional_scopes, scope)
## invalidate_node_style_for_properties' `invalidate_for_style_scope` lambda.
func css_invalidation_invalidate_for_style_scope(node: DomNode*, reason: DomStyleInvalidationReason, properties: const ak.Vector[CssInvalidationSetProperty]*, style_scope: CssStyleScope*) -> bool:
let document = dom_node_document(node)
let plan = css_style_computer_invalidation_plan_for_properties(dom_document_style_computer(document), properties, style_scope)
return css_style_invalidator_enqueue_invalidation_plan(dom_document_style_invalidator(document), node, reason, plan)
## Ported from Libraries/LibWeb/CSS/Invalidation/NodeInvalidator.cpp:75 Web::CSS::Invalidation::invalidate_node_style_for_properties.
pub func css_invalidation_invalidate_node_style_for_properties(node: DomNode*, reason: DomStyleInvalidationReason, properties: ak.Vector[CssInvalidationSetProperty], options: DomStyleInvalidationOptions):
if dom_node_is_character_data(node):
return
let style_scope = dom_node_style_scope(node)
# Collect every shadow scope this mutation can flip, in addition to the root scope. This includes:
# - The element's own shadow root (if it's a shadow host).
# - Every enclosing shadow host's shadow root, for :host(...:has(...)) and ::slotted(...) rules in those scopes
# that observe property changes on this element or its light-DOM children.
# - The document scope and any outer shadow root scopes when this element lives inside a shadow tree, for
# ::part(...:has(...)) rules in the outer document or containing shadow root.
var scopes = CssInvalidationInvalidateNodeStyleForPropertiesScopes(style_scope = style_scope)
dom_node_for_each_style_scope_which_may_observe_the_node(node, ak.Function1[CssStyleScope*, unit](call = css_invalidation_invalidate_node_style_for_properties_collect_scope, context = &scopes))
let additional_scopes = ak.vector_span(&scopes.additional_scopes)
let document = dom_node_document(node)
var properties_used_in_has_selectors = false
let counters = dom_document_style_invalidation_counters(document)
for property in &ak.vector_span_const(&properties):
if let metadata = css_style_computer_has_invalidation_metadata_for_property(dom_document_style_computer(document), property, style_scope):
properties_used_in_has_selectors = true
counters.has_invalidation_metadata_candidates += (u64)ak.vector_size(metadata)
for scope in additional_scopes:
if let metadata = css_style_computer_has_invalidation_metadata_for_property(dom_document_style_computer(document), property, scope):
properties_used_in_has_selectors = true
counters.has_invalidation_metadata_candidates += (u64)ak.vector_size(metadata)
if properties_used_in_has_selectors:
css_style_scope_record_pending_has_invalidation_mutation_features_vector(style_scope, node, &properties)
css_style_scope_schedule_ancestors_style_invalidation_due_to_presence_of_has(style_scope, node)
for scope in additional_scopes:
css_style_scope_record_pending_has_invalidation_mutation_features_vector(scope, node, &properties)
css_style_scope_schedule_ancestors_style_invalidation_due_to_presence_of_has(scope, node)
if options.invalidate_self:
dom_node_set_needs_style_update(node, true)
if css_invalidation_invalidate_for_style_scope(node, reason, &properties, style_scope):
return
for scope in additional_scopes:
if css_invalidation_invalidate_for_style_scope(node, reason, &properties, scope):
return