Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / css / invalidation / structural_mutation_invalidator.lucb

#==============================================================================================
#
#   structural_mutation_invalidator - style invalidation of the siblings and ancestors a DOM
#                                     insertion, removal or move can affect
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/Invalidation/StructuralMutationInvalidator.cpp (lines 1-128):
#       previous siblings that depend on following-sibling counts, following siblings that
#       depend on sibling combinators or positional pseudo-classes, the ancestors' "child
#       needs style update" flags, and the cheaper path of a move within one parent.
#
#       Copyright (c) 2026-present, the Ladybird developers
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/CSS/Invalidation/StructuralMutationInvalidator.cpp:17 Web::CSS::Invalidation::element_is_affected_by_same_parent_move.
func css_invalidation_element_is_affected_by_same_parent_move(element: DomElement*) -> bool:
    return dom_element_affected_by_forward_structural_changes(element) or dom_element_affected_by_backward_structural_changes(element) or dom_element_affected_by_has_pseudo_class_in_subject_position(element) or dom_element_affected_by_has_pseudo_class_in_non_subject_position(element) or dom_element_affected_by_has_pseudo_class_with_relative_selector_that_has_sibling_combinator(element)

## mark_sibling_for_style_update's for_each_shadow_including_inclusive_descendant callback (the inner lambda of
## `mark_descendant_shadow_roots_for_style_update`).
func css_invalidation_mark_descendant_shadow_root_for_style_update(context: void*?, inclusive_descendant: DomNode*) -> TraversalDecision:
    let descendant_element = as_if_dom_element(inclusive_descendant) else return TraversalDecision.continue_
    let shadow_root = dom_element_shadow_root(descendant_element) else return TraversalDecision.continue_
    dom_node_set_entire_subtree_needs_style_update(shadow_root.dom_node(), true)
    dom_node_set_needs_style_update(shadow_root.dom_node(), true)
    return TraversalDecision.continue_

## mark_sibling_for_style_update's `mark_descendant_shadow_roots_for_style_update` lambda.
func css_invalidation_mark_descendant_shadow_roots_for_style_update(element: DomElement*):
    _ = dom_node_for_each_shadow_including_inclusive_descendant(element.dom_node(), ak.Function1[DomNode*, TraversalDecision](call = css_invalidation_mark_descendant_shadow_root_for_style_update, context = none))

## Ported from Libraries/LibWeb/CSS/Invalidation/StructuralMutationInvalidator.cpp:26 Web::CSS::Invalidation::mark_sibling_for_style_update.
func css_invalidation_mark_sibling_for_style_update(element: DomElement*):
    # The structural change might flip whether `element` matches selectors like :nth-child or `~`/`+`, but those
    # pseudo-classes and combinators only affect `element` itself unless some descendant selector also depends on
    # `element`'s position. Mark just `element` when descendants don't observe its position; mark the entire
    # subtree only when they do.
    if dom_element_affected_by_structural_pseudo_class_in_non_subject_position(element) or dom_element_affected_by_sibling_combinator_in_non_subject_position(element):
        dom_node_set_entire_subtree_needs_style_update(element.dom_node(), true)
    else:
        dom_node_set_needs_style_update(element.dom_node(), true)
        css_invalidation_mark_descendant_shadow_roots_for_style_update(element)

## Ported from Libraries/LibWeb/CSS/Invalidation/StructuralMutationInvalidator.cpp:54 Web::CSS::Invalidation::invalidate_structurally_affected_siblings.
pub func css_invalidation_invalidate_structurally_affected_siblings(node: DomNode*, reason: DomStyleInvalidationReason):
    let is_insertion_or_removal = reason == DomStyleInvalidationReason.node_insert_before or reason == DomStyleInvalidationReason.node_remove

    if is_insertion_or_removal:
        # OPTIMIZATION: Only walk previous siblings if the parent has been observed to contain a child that matches a
        #               pseudo-class whose match result can depend on siblings after that element. Otherwise, no
        #               previous sibling can possibly need invalidation due to this insertion or removal.
        if let parent = tree_node_parent(node):
            if let parent_node = as_if_dom_parent_node(parent):
                if dom_parent_node_has_child_affected_by_backward_structural_changes(parent_node):
                    let counters = dom_document_style_invalidation_counters(dom_node_document(node))
                    var sibling = node.tree_node.m_previous_sibling
                    while let current = sibling:
                        counters.previous_sibling_invalidation_walk_visits += 1
                        if let element = as_if_dom_element(current):
                            if dom_element_affected_by_backward_structural_changes(element):
                                css_invalidation_mark_sibling_for_style_update(element)
                        sibling = current.tree_node.m_previous_sibling

    var current_sibling_distance: usize = 1
    var sibling = node.tree_node.m_next_sibling
    while let current = sibling:
        sibling = current.tree_node.m_next_sibling
        let element = as_if_dom_element(current) else continue

        var needs_to_invalidate = false
        if is_insertion_or_removal:
            if dom_element_affected_by_indirect_sibling_combinator(element) or dom_element_affected_by_first_child_pseudo_class(element) or dom_element_affected_by_forward_positional_pseudo_class(element):
                needs_to_invalidate = true
            elif dom_element_affected_by_direct_sibling_combinator(element) and current_sibling_distance <= dom_element_sibling_invalidation_distance(element):
                needs_to_invalidate = true
        elif dom_element_affected_by_indirect_sibling_combinator(element) or dom_element_affected_by_forward_positional_pseudo_class(element):
            needs_to_invalidate = true
        elif dom_element_affected_by_direct_sibling_combinator(element) and current_sibling_distance <= dom_element_sibling_invalidation_distance(element):
            needs_to_invalidate = true

        if needs_to_invalidate:
            css_invalidation_mark_sibling_for_style_update(element)
        current_sibling_distance += 1

## Ported from Libraries/LibWeb/CSS/Invalidation/StructuralMutationInvalidator.cpp:97 Web::CSS::Invalidation::mark_ancestors_as_having_child_needing_style_update.
pub func css_invalidation_mark_ancestors_as_having_child_needing_style_update(node: DomNode*):
    var ancestor = dom_node_parent_or_shadow_host(node)
    while let current = ancestor:
        dom_node_set_child_needs_style_update(current.dom_node(), true)
        ancestor = dom_node_parent_or_shadow_host(current.dom_node())

## Ported from Libraries/LibWeb/CSS/Invalidation/StructuralMutationInvalidator.cpp:103 Web::CSS::Invalidation::invalidate_style_after_same_parent_move.
pub func css_invalidation_invalidate_style_after_same_parent_move(node: DomNode*, reason: DomStyleInvalidationReason):
    if dom_document_needs_full_style_update(dom_node_document(node)):
        return

    css_invalidation_schedule_has_invalidation_for_same_parent_move(node)

    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())

    if let element = as_if_dom_element(node):
        if dom_element_affected_by_structural_pseudo_class_in_non_subject_position(element) or dom_element_affected_by_sibling_combinator_in_non_subject_position(element):
            dom_node_set_entire_subtree_needs_style_update(node, true)
        elif css_invalidation_element_is_affected_by_same_parent_move(element):
            dom_node_set_needs_style_update(node, true)
    elif not dom_node_is_character_data(node):
        dom_node_set_needs_style_update(node, true)
    css_invalidation_invalidate_structurally_affected_siblings(node, reason)
    css_invalidation_mark_ancestors_as_having_child_needing_style_update(node)