dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / layout / node_5.lucb
#==============================================================================================
#
# node_5 - Layout::Node: DOM node, document, user-select, containment, continuations
#
# DESCRIPTION:
# Port of LibWeb/Layout/Node.cpp (lines 1292-1619): is_anonymous, dom_node,
# pseudo_element_generator, document, user_select_used_value, the containment
# predicates, NodeWithStyleAndBoxModelMetrics's inline continuations and edges, and
# set_needs_layout_update.
#
# Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.org>
# Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
# Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: DOM node and document ===================================================================
## Ported from Libraries/LibWeb/Layout/Node.cpp:1292 Web::Layout::Node::is_anonymous.
pub func layout_node_is_anonymous(this: LayoutNode*) -> bool:
return this.m_anonymous
## Ported from Libraries/LibWeb/Layout/Node.cpp:1297 Web::Layout::Node::dom_node.
pub func layout_node_dom_node_const(this: LayoutNode*) -> DomNode*?:
if this.m_anonymous:
return none
return this.m_dom_node
## Ported from Libraries/LibWeb/Layout/Node.cpp:1304 Web::Layout::Node::dom_node.
pub func layout_node_dom_node(this: LayoutNode*) -> DomNode*?:
if this.m_anonymous:
return none
return this.m_dom_node
## Ported from Libraries/LibWeb/Layout/Node.cpp:1311 Web::Layout::Node::pseudo_element_generator.
pub func layout_node_pseudo_element_generator_const(this: LayoutNode*) -> DomElement*?:
assert(this.m_generated_for != none)
return this.m_pseudo_element_generator
## Ported from Libraries/LibWeb/Layout/Node.cpp:1317 Web::Layout::Node::pseudo_element_generator.
pub func layout_node_pseudo_element_generator(this: LayoutNode*) -> DomElement*?:
assert(this.m_generated_for != none)
return this.m_pseudo_element_generator
## Ported from Libraries/LibWeb/Layout/Node.cpp:1323 Web::Layout::Node::document.
pub func layout_node_document(this: LayoutNode*) -> DomDocument*:
return dom_node_document(this.m_dom_node)
## Ported from Libraries/LibWeb/Layout/Node.cpp:1328 Web::Layout::Node::document.
pub func layout_node_document_const(this: LayoutNode*) -> DomDocument*:
return dom_node_document(this.m_dom_node)
# mark: user-select =============================================================================
## Ported from Libraries/LibWeb/Layout/Node.cpp:1334 Web::Layout::Node::user_select_used_value.
# https://drafts.csswg.org/css-ui/#propdef-user-select
pub func layout_node_user_select_used_value(this: LayoutNode*) -> css_data.UserSelect:
# The used value is the same as the computed value, except:
let computed_value = css_computed_values_user_select(&layout_node_computed_values(this).computed_values)
# 1. on editable elements where the used value is always 'contain' regardless of the computed value
# 2. when the computed value is 'auto', in which case the used value is one of the other values as defined below
# For the purpose of this specification, an editable element is either an editing host or a mutable form control with
# textual content, such as textarea.
var form_control: HtmlFormAssociatedTextControlElement*? = none
if let dom_node = layout_node_dom_node(this):
form_control = as_if_form_associated_text_control_element(dom_node)
# FIXME: Check if this needs to exclude input elements with types such as color or range, and if so, which ones exactly.
var is_editable = false
if let dom_node = layout_node_dom_node(this):
is_editable = dom_node_is_editing_host(dom_node)
if let control = form_control:
if not is_editable and html_form_associated_element_is_mutable(&html_form_associated_text_control_element_text_control_to_html_element(control).form_associated_element):
is_editable = true
if is_editable:
return css_data.UserSelect.contain
elif computed_value == css_data.UserSelect.auto:
# The used value of 'auto' is determined as follows:
# - On the '::before' and '::after' pseudo-elements, the used value is 'none'
if layout_node_is_generated_for_before_pseudo_element(this) or layout_node_is_generated_for_after_pseudo_element(this):
return css_data.UserSelect.none_
# - If the element is an editable element, the used value is 'contain'
# NOTE: We already handled this above.
if let parent_element = layout_node_parent(this):
let parent_used_value = layout_node_user_select_used_value(parent_element.layout_node())
# - Otherwise, if the used value of user-select on the parent of this element is 'all', the used value is 'all'
if parent_used_value == css_data.UserSelect.all:
return css_data.UserSelect.all
# - Otherwise, if the used value of user-select on the parent of this element is 'none', the used value is
# 'none'
if parent_used_value == css_data.UserSelect.none_:
return css_data.UserSelect.none_
# - Otherwise, the used value is 'text'
return css_data.UserSelect.text
return computed_value
# mark: Containment =============================================================================
## Ported from Libraries/LibWeb/Layout/Node.cpp:1383 Web::Layout::Node::has_size_containment.
# https://drafts.csswg.org/css-contain-2/#containment-size
pub func layout_node_has_size_containment(this: LayoutNode*) -> bool:
# However, giving an element size containment has no effect if any of the following are true:
# - if the element does not generate a principal box (as is the case with 'display: contents' or 'display: none')
# Note: This is the principal box
# - if its inner display type is 'table'
let display = layout_node_display(this)
if css_display_is_table_inside(&display):
return false
# - if its principal box is an internal table box
if css_display_is_internal_table(&display):
return false
# - if its principal box is an internal ruby box or a non-atomic inline-level box
# FIXME: Implement this.
let computed_values = &layout_node_computed_values(this).computed_values
if css_computed_values_contain(computed_values).size_containment:
return true
if css_computed_values_container_type(computed_values).is_size_container:
return true
return false
## Ported from Libraries/LibWeb/Layout/Node.cpp:1410 Web::Layout::Node::has_inline_size_containment.
# https://drafts.csswg.org/css-contain-2/#containment-inline-size
pub func layout_node_has_inline_size_containment(this: LayoutNode*) -> bool:
# Giving an element inline-size containment has no effect if any of the following are true:
# - if the element does not generate a principal box (as is the case with 'display: contents' or 'display: none')
# Note: This is the principal box
# - if its inner display type is 'table'
let display = layout_node_display(this)
if css_display_is_table_inside(&display):
return false
# - if its principal box is an internal table box
if css_display_is_internal_table(&display):
return false
# - if its principal box is an internal ruby box or a non-atomic inline-level box
# FIXME: Implement this.
let computed_values = &layout_node_computed_values(this).computed_values
if css_computed_values_contain(computed_values).inline_size_containment:
return true
if css_computed_values_container_type(computed_values).is_inline_size_container:
return true
return false
## Ported from Libraries/LibWeb/Layout/Node.cpp:1437 Web::Layout::Node::has_layout_containment.
# https://drafts.csswg.org/css-contain-2/#containment-layout
pub func layout_node_has_layout_containment(this: LayoutNode*) -> bool:
# However, giving an element layout containment has no effect if any of the following are true:
# - if the element does not generate a principal box (as is the case with 'display: contents' or 'display: none')
# Note: This is the principal box
# - if its principal box is an internal table box other than 'table-cell'
let display = layout_node_display(this)
if css_display_is_internal_table(&display) and not css_display_is_table_cell(&display):
return false
# - if its principal box is an internal ruby box or a non-atomic inline-level box
# FIXME: Implement this.
let computed_values = &layout_node_computed_values(this).computed_values
if css_computed_values_contain(computed_values).layout_containment:
return true
# https://drafts.csswg.org/css-contain-2/#valdef-content-visibility-auto
# Changes the used value of the 'contain' property so as to turn on layout containment, style containment, and
# paint containment for the element.
if css_computed_values_content_visibility(computed_values) == css_data.ContentVisibility.auto:
return true
return false
## Ported from Libraries/LibWeb/Layout/Node.cpp:1463 Web::Layout::Node::has_style_containment.
# https://drafts.csswg.org/css-contain-2/#containment-style
pub func layout_node_has_style_containment(this: LayoutNode*) -> bool:
# However, giving an element style containment has no effect if any of the following are true:
# - if the element does not generate a principal box (as is the case with 'display: contents' or 'display: none')
# Note: This is the principal box
let computed_values = &layout_node_computed_values(this).computed_values
if css_computed_values_contain(computed_values).style_containment:
return true
let container_type = css_computed_values_container_type(computed_values)
if container_type.is_size_container or container_type.is_inline_size_container:
return true
# https://drafts.csswg.org/css-contain-2/#valdef-content-visibility-auto
# Changes the used value of the 'contain' property so as to turn on layout containment, style containment, and
# paint containment for the element.
if css_computed_values_content_visibility(computed_values) == css_data.ContentVisibility.auto:
return true
return false
## Ported from Libraries/LibWeb/Layout/Node.cpp:1485 Web::Layout::Node::has_paint_containment.
# https://drafts.csswg.org/css-contain-2/#containment-paint
pub func layout_node_has_paint_containment(this: LayoutNode*) -> bool:
# However, giving an element paint containment has no effect if any of the following are true:
# - if the element does not generate a principal box (as is the case with 'display: contents' or 'display: none')
# Note: This is the principal box
# - if its principal box is an internal table box other than 'table-cell'
let display = layout_node_display(this)
if css_display_is_internal_table(&display) and not css_display_is_table_cell(&display):
return false
# - if its principal box is an internal ruby box or a non-atomic inline-level box
# FIXME: Implement this
let computed_values = &layout_node_computed_values(this).computed_values
if css_computed_values_contain(computed_values).paint_containment:
return true
# https://drafts.csswg.org/css-contain-2/#valdef-content-visibility-auto
# Changes the used value of the 'contain' property so as to turn on layout containment, style containment, and
# paint containment for the element.
if css_computed_values_content_visibility(computed_values) == css_data.ContentVisibility.auto:
return true
return false
# mark: Inline continuations ====================================================================
## Ported from Libraries/LibWeb/Layout/Node.cpp:1511 Web::Layout::NodeWithStyleAndBoxModelMetrics::should_create_inline_continuation.
pub func layout_node_with_style_and_box_model_metrics_should_create_inline_continuation(this: LayoutNodeWithStyleAndBoxModelMetrics*) -> bool:
let node = this.layout_node()
# This node must have an inline parent.
let parent = layout_node_parent(node) else return false
let parent_display = layout_node_display(parent.layout_node())
if not css_display_is_inline_outside(&parent_display) or not css_display_is_flow_inside(&parent_display):
return false
# This node must not be inline itself or out of flow (which gets handled separately).
let display = layout_node_display(node)
if css_display_is_inline_outside(&display) or layout_node_is_out_of_flow(node):
return false
# This node must not have `display: contents`; inline continuation gets handled by its children.
if css_display_is_contents(&display):
return false
# Internal table display types and table captions are handled by the table fixup algorithm.
if css_display_is_internal_table(&display) or css_display_is_table_caption(&display):
return false
# Parent element must not be <foreignObject>
if let parent_dom_node = layout_node_dom_node(parent.layout_node()):
if is_svg_foreign_object_element(parent_dom_node):
return false
# Non-root SVG elements and foreign object boxes should never be split.
if layout_node_is_svg_box(node) or layout_node_is_svg_foreign_object_box(node):
return false
# Nested SVG roots should never be split, but a top-level SVG root inside an HTML inline element should be.
if layout_node_is_svg_svg_box(node) and (layout_node_is_svg_box(parent.layout_node()) or layout_node_is_svg_svg_box(parent.layout_node())):
return false
# Replaced boxes with children (e.g. media elements with shadow DOM controls)
# have their own formatting context; don't split them.
if layout_node_is_replaced_box_with_children(parent.layout_node()):
return false
return true
## Ported from Libraries/LibWeb/Layout/Node.cpp:1552 Web::Layout::NodeWithStyleAndBoxModelMetrics::propagate_style_along_continuation.
pub func layout_node_with_style_and_box_model_metrics_propagate_style_along_continuation(this: LayoutNodeWithStyleAndBoxModelMetrics*, computed_style: CssComputedProperties*):
var continuation = layout_node_with_style_and_box_model_metrics_continuation_of_node(this)
while let current = continuation:
if not layout_node_is_anonymous(current.layout_node()):
break
continuation = layout_node_with_style_and_box_model_metrics_continuation_of_node(current)
if let found = continuation:
layout_node_with_style_apply_style(found.layout_node_with_style(), computed_style)
## Ported from Libraries/LibWeb/Layout/Node.cpp:1561 Web::Layout::NodeWithStyleAndBoxModelMetrics::visit_edges (override).
pub func layout_node_with_style_and_box_model_metrics_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (LayoutNodeWithStyleAndBoxModelMetrics*)base
layout_node_with_style_visit_edges_impl(base, visitor)
gc.cell_visitor_visit_t(visitor, this.m_continuation_of_node)
# mark: Layout updates ==========================================================================
## Ported from Libraries/LibWeb/Layout/Node.cpp:1567 Web::Layout::Node::set_needs_layout_update.
pub func layout_node_set_needs_layout_update(this: LayoutNode*, reason: DomSetNeedsLayoutReason):
if this.m_needs_layout_update:
return
if update_layout_debug:
# NOTE: We check some conditions here to avoid debug spam in documents that don't do layout.
if let navigable = layout_node_navigable(this):
if html_navigable_active_document(navigable) == layout_node_document(this):
ak.dbgln(f"NEED LAYOUT {ak.string_view_text(dom_to_string_dom_set_needs_layout_reason(reason))}")
this.m_needs_layout_update = true
if let box = as_if_layout_box(this):
layout_box_reset_cached_intrinsic_sizes(box)
# Mark any anonymous children generated by this node for layout update.
# NOTE: if this node generated an anonymous parent, all ancestors are indiscriminately marked below.
for child_node in layout_node_children(this):
let child = as_if_layout_box(child_node) else continue
if layout_node_is_anonymous(child.layout_node()) and not is_layout_table_wrapper(child):
child.node_with_style_and_box_model_metrics.node_with_style.node.m_needs_layout_update = true
layout_box_reset_cached_intrinsic_sizes(child)
var ancestor = layout_node_parent(this)
while let current = ancestor:
if current.node.m_needs_layout_update:
break
current.node.m_needs_layout_update = true
if let svg_box = as_if_layout_svgsvg_box(current):
dom_document_mark_svg_root_as_needing_relayout(layout_node_document(this), svg_box)
break
ancestor = layout_node_parent(current.layout_node())
# Reset intrinsic size caches for ancestors up to abspos or SVG root boundary.
# Absolutely positioned elements don't contribute to ancestor intrinsic sizes,
# so changes inside an abspos box don't require resetting ancestor caches.
# SVG root elements have intrinsic sizes determined solely by their own attributes
# (width, height, viewBox), not by their children, so the same logic applies.
ancestor = layout_node_parent(this)
while let current = ancestor:
ancestor = layout_node_parent(current.layout_node())
let box = as_if_layout_box(current) else continue
layout_box_reset_cached_intrinsic_sizes(box)
if layout_node_is_absolutely_positioned(box.layout_node()) or layout_node_is_svg_svg_box(box.layout_node()):
break