dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / css / style_invalidation.lucb
#==============================================================================================
#
# style_invalidation - CSS::compute_property_invalidation: what a change of one computed
# property requires (repaint, relayout, a new layout tree, ...)
#
# DESCRIPTION:
# Port of LibWeb/CSS/StyleInvalidation.cpp (lines 1-155) and the inline members of
# StyleInvalidation.h (lines 13-31): RequiredInvalidationAfterStyleChange and the
# per-property decision, including whether a value creates a stacking context.
#
# Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
# Copyright (c) 2025, Manuel Zahariev <manuel@duck.com>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: RequiredInvalidationAfterStyleChange ====================================================
## Ported from Libraries/LibWeb/CSS/StyleInvalidation.h:20 Web::CSS::RequiredInvalidationAfterStyleChange::operator|=.
pub func css_required_invalidation_after_style_change_or_assign(this: CssRequiredInvalidationAfterStyleChange*, other: CssRequiredInvalidationAfterStyleChange):
this.repaint = this.repaint or other.repaint
this.rebuild_stacking_context_tree = this.rebuild_stacking_context_tree or other.rebuild_stacking_context_tree
this.relayout = this.relayout or other.relayout
this.rebuild_layout_tree = this.rebuild_layout_tree or other.rebuild_layout_tree
this.rebuild_accumulated_visual_contexts = this.rebuild_accumulated_visual_contexts or other.rebuild_accumulated_visual_contexts
## Ported from Libraries/LibWeb/CSS/StyleInvalidation.h:29 Web::CSS::RequiredInvalidationAfterStyleChange::is_none.
pub func css_required_invalidation_after_style_change_is_none(this: const CssRequiredInvalidationAfterStyleChange*) -> bool:
return not this.repaint and not this.rebuild_stacking_context_tree and not this.relayout and not this.rebuild_layout_tree and not this.rebuild_accumulated_visual_contexts
## Ported from Libraries/LibWeb/CSS/StyleInvalidation.h:30 Web::CSS::RequiredInvalidationAfterStyleChange::is_full.
pub func css_required_invalidation_after_style_change_is_full(this: const CssRequiredInvalidationAfterStyleChange*) -> bool:
return this.repaint and this.rebuild_stacking_context_tree and this.relayout and this.rebuild_layout_tree
## Ported from Libraries/LibWeb/CSS/StyleInvalidation.h:31 Web::CSS::RequiredInvalidationAfterStyleChange::full.
pub func css_required_invalidation_after_style_change_full() -> CssRequiredInvalidationAfterStyleChange:
return CssRequiredInvalidationAfterStyleChange(repaint = true, rebuild_stacking_context_tree = true, relayout = true, rebuild_layout_tree = true, rebuild_accumulated_visual_contexts = false)
# mark: compute_property_invalidation ===========================================================
## Ported from Libraries/LibWeb/CSS/StyleInvalidation.cpp:18 Web::CSS::is_stacking_context_creating_value.
func css_is_stacking_context_creating_value(property_id: css_data.PropertyId, value: CssStyleValue*?) -> bool:
let value_ = value else return false
match property_id:
.opacity:
return css_opacity_value_style_value_resolved(css_style_value_as_opacity_value(value_)) < 1.0
.transform:
if css_style_value_to_keyword(value_) == css_data.Keyword.none_:
return false
if css_style_value_is_value_list(value_):
return css_style_value_list_size(css_style_value_as_value_list(value_)) > 0
return css_style_value_is_transformation(value_)
.translate, .rotate, .scale:
return css_style_value_to_keyword(value_) != css_data.Keyword.none_
.filter, .backdrop_filter:
if css_style_value_is_keyword(value_):
return css_style_value_to_keyword(value_) != css_data.Keyword.none_
return css_style_value_is_filter_value_list(value_)
.clip_path, .mask, .mask_image, .view_transition_name:
return css_style_value_to_keyword(value_) != css_data.Keyword.none_
.isolation:
return css_style_value_to_keyword(value_) == css_data.Keyword.isolate
.mix_blend_mode:
return css_style_value_to_keyword(value_) != css_data.Keyword.normal
.z_index:
return css_style_value_to_keyword(value_) != css_data.Keyword.auto
.perspective, .transform_style:
return css_style_value_to_keyword(value_) != css_data.Keyword.none_ and css_style_value_to_keyword(value_) != css_data.Keyword.flat
_:
# For properties we haven't optimized (contain, container-type, will-change, all),
# assume any value creates stacking context to be safe
return true
## Ported from Libraries/LibWeb/CSS/StyleInvalidation.cpp:62 Web::CSS::compute_property_invalidation.
pub func css_compute_property_invalidation(property_id: css_data.PropertyId, old_value: CssStyleValue*?, new_value: CssStyleValue*?) -> CssRequiredInvalidationAfterStyleChange:
var invalidation: CssRequiredInvalidationAfterStyleChange
if old_value == new_value:
return invalidation
if let old_value_ = old_value:
if let new_value_ = new_value:
if css_style_value_equals(old_value_, new_value_):
return invalidation
# NOTE: If the computed CSS display, position, content, or content-visibility property changes, we have to rebuild the entire layout tree.
# In the future, we should figure out ways to rebuild a smaller part of the tree.
if property_id == css_data.PropertyId.display or property_id == css_data.PropertyId.position or property_id == css_data.PropertyId.content or property_id == css_data.PropertyId.content_visibility:
return css_required_invalidation_after_style_change_full()
# NOTE: If the text-transform property changes, it may affect layout. Furthermore, since the
# Layout::TextNode caches the post-transform text, we have to update the layout tree.
if property_id == css_data.PropertyId.text_transform:
invalidation.rebuild_layout_tree = true
invalidation.relayout = true
invalidation.repaint = true
return invalidation
# NOTE: If one of the overflow properties change, we rebuild the entire layout tree.
# This ensures that overflow propagation from root/body to viewport happens correctly.
# In the future, we can make this invalidation narrower.
if property_id == css_data.PropertyId.overflow_x or property_id == css_data.PropertyId.overflow_y:
return css_required_invalidation_after_style_change_full()
if property_id == css_data.PropertyId.counter_reset or property_id == css_data.PropertyId.counter_set or property_id == css_data.PropertyId.counter_increment:
invalidation.rebuild_layout_tree = true
return invalidation
# OPTIMIZATION: Special handling for CSS `visibility`:
if property_id == css_data.PropertyId.visibility:
# We don't need to relayout if the visibility changes from visible to hidden or vice versa. Only collapse requires relayout.
let old_is_collapse = old_value != none and css_style_value_to_keyword(old_value else trap("unreachable")) == css_data.Keyword.collapse
let new_is_collapse = new_value != none and css_style_value_to_keyword(new_value else trap("unreachable")) == css_data.Keyword.collapse
if old_is_collapse != new_is_collapse:
invalidation.relayout = true
# Of course, we still have to repaint on any visibility change.
invalidation.repaint = true
elif css_data.property_affects_layout(property_id):
invalidation.relayout = true
if css_data.property_affects_stacking_context(property_id):
# z-index changes always require rebuilding the stacking context tree because
# the value determines painting order within the tree, not just whether a
# stacking context is created. During tree construction, elements with
# z-index 0/auto are placed in m_positioned_descendants_and_stacking_contexts_
# with_stack_level_0, while elements with non-zero z-index are painted from
# m_children (negative z-index at step 3, positive at step 9 of CSS 2.1
# Appendix E). If z-index changes between non-auto values (e.g. 0 -> 10),
# both old and new values create stacking contexts, so the generic optimization
# below would skip the rebuild. But the element remains in the wrong list,
# causing it to be painted from both step 8 (m_positioned_descendants) and
# step 9 (m_children with z >= 1), resulting in double painting.
if property_id == css_data.PropertyId.z_index:
invalidation.rebuild_stacking_context_tree = true
else:
# OPTIMIZATION: Only rebuild stacking context tree when property crosses from a neutral value (doesn't create
# stacking context) to a creating value or vice versa.
let old_creates = css_is_stacking_context_creating_value(property_id, old_value)
let new_creates = css_is_stacking_context_creating_value(property_id, new_value)
if old_creates != new_creates:
invalidation.rebuild_stacking_context_tree = true
invalidation.repaint = true
# Transform, perspective, clip, clip-path, and effects properties require rebuilding AccumulatedVisualContext tree.
let affects_accumulated_visual_contexts = match property_id:
.transform, .rotate, .scale, .translate, .perspective, .transform_origin, .perspective_origin, .clip, .clip_path, .opacity, .mix_blend_mode, .filter => true
_ => false
if affects_accumulated_visual_contexts:
invalidation.rebuild_accumulated_visual_contexts = true
return invalidation