dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / layout / node_4.lucb
#==============================================================================================
#
# node_4 - Layout::NodeWithStyle: style scope, anonymous wrappers and table wrappers
#
# DESCRIPTION:
# Port of LibWeb/Layout/Node.cpp (lines 1072-1290): NodeWithStyle::style_scope, the style
# propagation to anonymous wrappers, Node::is_root_element, debug_description, display and
# the inline predicates, NodeWithStyle::create_anonymous_wrapper, the table wrapper's
# computed values, scroll containers and the paintable list.
#
# Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.org>
# Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Style scope and style propagation ========================================================
## Ported from Libraries/LibWeb/Layout/Node.cpp:1072 Web::Layout::NodeWithStyle::style_scope.
pub func layout_node_with_style_style_scope(this: LayoutNodeWithStyle*) -> const CssStyleScope*:
if let dom_node = layout_node_dom_node(this.layout_node()):
return dom_node_style_scope(dom_node)
if layout_node_is_generated_for_pseudo_element(this.layout_node()):
let generator = layout_node_pseudo_element_generator(this.layout_node()) else trap("VERIFY: a pseudo-element generator")
return dom_node_style_scope(generator.dom_node())
if let parent = layout_node_parent(this.layout_node()):
return layout_node_with_style_style_scope(parent)
return dom_document_style_scope(layout_node_document(this.layout_node()))
## Ported from Libraries/LibWeb/Layout/Node.cpp:1086 Web::Layout::NodeWithStyle::propagate_non_inherit_values.
pub func layout_node_with_style_propagate_non_inherit_values(this: LayoutNodeWithStyle*, target_node: LayoutNodeWithStyle*):
# NOTE: These properties are not inherited, but we still have to propagate them to anonymous wrappers.
let computed_values = &layout_node_with_style_computed_values(this).computed_values
let target_values = layout_node_with_style_mutable_computed_values(target_node)
css_mutable_computed_values_set_text_decoration_line(target_values, css_computed_values_text_decoration_line(computed_values))
css_mutable_computed_values_set_text_decoration_thickness(target_values, css_computed_values_text_decoration_thickness(computed_values))
css_mutable_computed_values_set_text_decoration_color(target_values, css_computed_values_text_decoration_color(computed_values))
css_mutable_computed_values_set_text_decoration_style(target_values, css_computed_values_text_decoration_style(computed_values))
## Ported from Libraries/LibWeb/Layout/Node.cpp:1095 Web::Layout::NodeWithStyle::propagate_style_to_anonymous_wrappers.
pub func layout_node_with_style_propagate_style_to_anonymous_wrappers(this: LayoutNodeWithStyle*):
# Update the style of any anonymous wrappers that inherit from this node.
# FIXME: This is pretty hackish. It would be nicer if they shared the inherited style
# data structure somehow, so this wasn't necessary.
# If this is a `display:table` box with an anonymous wrapper parent,
# the parent inherits style from *this* node, not the other way around.
let display = layout_node_display(this.layout_node())
if let parent = layout_node_parent(this.layout_node()):
if is_layout_table_wrapper(parent) and css_display_is_table_inside(&display):
let table_wrapper = parent
css_mutable_computed_values_inherit_from(layout_node_with_style_mutable_computed_values(table_wrapper), &layout_node_with_style_computed_values(this).computed_values)
layout_node_with_style_transfer_table_box_computed_values_to_wrapper_computed_values(this, &layout_node_with_style_mutable_computed_values(table_wrapper).computed_values)
# Propagate style to all anonymous children (except table wrappers!)
for child_node in layout_node_children(this.layout_node()):
let child = as_if_layout_node_with_style(child_node) else continue
if layout_node_is_anonymous(child.layout_node()) and not is_layout_table_wrapper(child):
let child_computed_values = layout_node_with_style_mutable_computed_values(child)
css_mutable_computed_values_inherit_from(child_computed_values, &layout_node_with_style_computed_values(this).computed_values)
layout_node_with_style_propagate_non_inherit_values(this, child)
layout_node_with_style_propagate_style_to_anonymous_wrappers(child)
# mark: Root element, debug description and display =============================================
## Ported from Libraries/LibWeb/Layout/Node.cpp:1120 Web::Layout::Node::is_root_element.
pub func layout_node_is_root_element(this: LayoutNode*) -> bool:
if layout_node_is_anonymous(this):
return false
return is_html_html_element(this.m_dom_node)
## Ported from Libraries/LibWeb/Layout/Node.cpp:1127 Web::Layout::Node::debug_description.
pub func layout_node_debug_description(this: LayoutNode*) -> ak.String:
var builder = ak.string_builder_new()
ak.string_builder_append_string_view(&builder, ak.sv(this.cell().vtable.class_info.name))
if let dom_node = layout_node_dom_node(this):
let node_name = dom_node_node_name(dom_node)
ak.string_builder_append(&builder, "<")
ak.string_builder_append_string_view(&builder, ak.fly_string_bytes_as_string_view(&node_name))
ak.string_builder_append(&builder, ">")
if dom_node_is_element(dom_node):
let element = (DomElement*)dom_node
if let id = dom_element_id(element):
ak.string_builder_append(&builder, "#")
ak.string_builder_append_string_view(&builder, ak.fly_string_bytes_as_string_view(&id))
let class_names = dom_element_class_names(element)
for class_name in ak.vector_span_const(&class_names):
ak.string_builder_append(&builder, ".")
ak.string_builder_append_string_view(&builder, ak.fly_string_bytes_as_string_view(&class_name))
else:
ak.string_builder_append(&builder, "(anonymous)")
return ak.string_builder_to_string(&builder) else trap("MUST: StringBuilder::to_string")
## `Display(DisplayOutside::Inline, DisplayInside::Flow)`: the display of a node without style (text).
func layout_inline_flow_display() -> CssDisplay:
return css_display_new(css_data.DisplayOutside.inline, css_data.DisplayInside.flow)
## Ported from Libraries/LibWeb/Layout/Node.cpp:1146 Web::Layout::Node::display.
pub func layout_node_display(this: LayoutNode*) -> CssDisplay:
if not layout_node_has_style(this):
# NOTE: No style means this is dumb text content.
return layout_inline_flow_display()
return css_computed_values_display(&layout_node_computed_values(this).computed_values)
## Ported from Libraries/LibWeb/Layout/Node.cpp:1156 Web::Layout::Node::display_before_box_type_transformation.
pub func layout_node_display_before_box_type_transformation(this: LayoutNode*) -> CssDisplay:
if not layout_node_has_style(this):
return layout_inline_flow_display()
return css_computed_values_display_before_box_type_transformation(&layout_node_computed_values(this).computed_values)
## Ported from Libraries/LibWeb/Layout/Node.cpp:1165 Web::Layout::Node::is_inline.
pub func layout_node_is_inline(this: LayoutNode*) -> bool:
let display = layout_node_display(this)
return css_display_is_inline_outside(&display)
## Ported from Libraries/LibWeb/Layout/Node.cpp:1170 Web::Layout::Node::is_inline_block.
pub func layout_node_is_inline_block(this: LayoutNode*) -> bool:
let display = layout_node_display(this)
return css_display_is_inline_outside(&display) and css_display_is_flow_root_inside(&display)
## Ported from Libraries/LibWeb/Layout/Node.cpp:1176 Web::Layout::Node::is_inline_table.
pub func layout_node_is_inline_table(this: LayoutNode*) -> bool:
let display = layout_node_display(this)
return css_display_is_inline_outside(&display) and css_display_is_table_inside(&display)
## Ported from Libraries/LibWeb/Layout/Node.cpp:1182 Web::Layout::Node::is_atomic_inline.
pub func layout_node_is_atomic_inline(this: LayoutNode*) -> bool:
if layout_node_is_replaced_box(this):
return true
let display = layout_node_display(this)
return css_display_is_inline_outside(&display) and not css_display_is_flow_inside(&display)
# mark: Anonymous wrappers and table wrappers ===================================================
## The realm of `document`, where `heap().allocate<T>(document, ...)` makes layout nodes: the generated creation
## helpers are realm.create<T>, and a layout node's initialize is JS::Cell's, which does nothing.
func layout_document_realm(document: DomDocument*) -> Realm*:
return bind_platform_object_realm(document.bind_platform_object())
## Ported from Libraries/LibWeb/Layout/Node.cpp:1190 Web::Layout::NodeWithStyle::create_anonymous_wrapper.
pub func layout_node_with_style_create_anonymous_wrapper(this: LayoutNodeWithStyle*) -> LayoutNodeWithStyle*:
let document = layout_node_document(this.layout_node())
let wrapper = realm_create_layout_block_container_css_computed_values(layout_document_realm(document), document, none, css_computed_values_clone_inherited_values(&layout_node_with_style_computed_values(this).computed_values))
css_mutable_computed_values_set_display(layout_node_with_style_mutable_computed_values(wrapper.layout_node_with_style()), css_display_new(css_data.DisplayOutside.block, css_data.DisplayInside.flow))
layout_node_with_style_propagate_non_inherit_values(this, wrapper.layout_node_with_style())
# CSS 2.2 9.2.1.1 creates anonymous block boxes, but 9.4.1 states inline-block creates a BFC.
# Set wrapper to inline-block to participate correctly in the IFC within the parent inline-block.
let display = layout_node_display(this.layout_node())
if css_display_is_inline_block(&display) and not tree_node_has_children(this.layout_node()):
css_mutable_computed_values_set_display(layout_node_with_style_mutable_computed_values(wrapper.layout_node_with_style()), css_display_from_short(CssDisplayShort.inline_block))
return wrapper.layout_node_with_style()
## Ported from Libraries/LibWeb/Layout/Node.cpp:1203 Web::Layout::NodeWithStyle::set_computed_values.
pub func layout_node_with_style_set_computed_values(this: LayoutNodeWithStyle*, computed_values: CssComputedValues*):
this.m_computed_values = computed_values
## Ported from Libraries/LibWeb/Layout/Node.cpp:1208 Web::Layout::NodeWithStyle::reset_table_box_computed_values_used_by_wrapper_to_init_values.
func layout_node_with_style_reset_table_box_computed_values_used_by_wrapper_to_init_values(this: LayoutNodeWithStyle*):
let display = layout_node_display(this.layout_node())
assert(css_display_is_table_inside(&display))
let mutable_computed_values = layout_node_with_style_mutable_computed_values(this)
css_mutable_computed_values_set_position(mutable_computed_values, css_initial_values_position())
css_mutable_computed_values_set_position_anchor(mutable_computed_values, css_initial_values_position_anchor())
css_mutable_computed_values_set_float(mutable_computed_values, css_initial_values_float())
css_mutable_computed_values_set_clear(mutable_computed_values, css_initial_values_clear())
css_mutable_computed_values_set_inset(mutable_computed_values, css_initial_values_inset())
css_mutable_computed_values_set_margin(mutable_computed_values, css_initial_values_margin())
# AD-HOC:
# To match other browsers, z-index needs to be moved to the wrapper box as well,
# even if the spec does not mention that: https://github.com/w3c/csswg-drafts/issues/11689
# Note that there may be more properties that need to be added to this list.
css_mutable_computed_values_set_z_index(mutable_computed_values, css_initial_values_z_index())
## Ported from Libraries/LibWeb/Layout/Node.cpp:1226 Web::Layout::NodeWithStyle::transfer_table_box_computed_values_to_wrapper_computed_values.
pub func layout_node_with_style_transfer_table_box_computed_values_to_wrapper_computed_values(this: LayoutNodeWithStyle*, wrapper_computed_values: CssComputedValues*):
# The computed values of properties 'position', 'float', 'margin-*', 'top', 'right', 'bottom', and 'left' on the table element are used on the table wrapper box and not the table box;
# all other values of non-inheritable properties are used on the table box and not the table wrapper box.
# (Where the table element's values are not used on the table and table wrapper boxes, the initial values are used instead.)
let mutable_wrapper_computed_values = (CssMutableComputedValues*)wrapper_computed_values
let display = layout_node_display(this.layout_node())
if css_display_is_inline_outside(&display):
css_mutable_computed_values_set_display(mutable_wrapper_computed_values, css_display_from_short(CssDisplayShort.inline_block))
else:
css_mutable_computed_values_set_display(mutable_wrapper_computed_values, css_display_from_short(CssDisplayShort.flow_root))
let computed_values = &layout_node_with_style_computed_values(this).computed_values
css_mutable_computed_values_set_position(mutable_wrapper_computed_values, css_computed_values_position(computed_values))
css_mutable_computed_values_set_position_anchor(mutable_wrapper_computed_values, css_computed_values_position_anchor(computed_values))
css_mutable_computed_values_set_inset(mutable_wrapper_computed_values, css_computed_values_inset(computed_values))
css_mutable_computed_values_set_float(mutable_wrapper_computed_values, css_computed_values_float(computed_values))
css_mutable_computed_values_set_clear(mutable_wrapper_computed_values, css_computed_values_clear(computed_values))
css_mutable_computed_values_set_margin(mutable_wrapper_computed_values, css_computed_values_margin(computed_values))
# AD-HOC:
# To match other browsers, z-index needs to be moved to the wrapper box as well,
# even if the spec does not mention that: https://github.com/w3c/csswg-drafts/issues/11689
# Note that there may be more properties that need to be added to this list.
css_mutable_computed_values_set_z_index(mutable_wrapper_computed_values, css_computed_values_z_index(computed_values))
layout_node_with_style_reset_table_box_computed_values_used_by_wrapper_to_init_values(this)
# mark: Scroll containers =======================================================================
## Ported from Libraries/LibWeb/Layout/Node.cpp:1251 Web::Layout::overflow_value_makes_box_a_scroll_container.
pub func layout_overflow_value_makes_box_a_scroll_container(overflow: css_data.Overflow) -> bool:
match overflow:
.clip, .visible:
return false
.auto, .hidden, .scroll:
return true
_:
trap("VERIFY_NOT_REACHED")
## Ported from Libraries/LibWeb/Layout/Node.cpp:1265 Web::Layout::NodeWithStyle::is_scroll_container.
pub func layout_node_with_style_is_scroll_container(this: LayoutNodeWithStyle*) -> bool:
# NOTE: This isn't in the spec, but we want the viewport to behave like a scroll container.
if layout_node_is_viewport(this.layout_node()):
return true
let computed_values = &layout_node_with_style_computed_values(this).computed_values
return layout_overflow_value_makes_box_a_scroll_container(css_computed_values_overflow_x(computed_values)) or layout_overflow_value_makes_box_a_scroll_container(css_computed_values_overflow_y(computed_values))
# mark: Paintables ==============================================================================
## Ported from Libraries/LibWeb/Layout/Node.cpp:1275 Web::Layout::Node::add_paintable.
pub func layout_node_add_paintable(this: LayoutNode*, paintable: PaintPaintable*?):
let added = paintable else return
ak.intrusive_list_append(&this.m_paintable, added, memory.offset_of(PaintPaintable, m_list_node))
## Ported from Libraries/LibWeb/Layout/Node.cpp:1282 Web::Layout::Node::clear_paintables.
pub func layout_node_clear_paintables(this: LayoutNode*):
ak.intrusive_list_clear(&this.m_paintable)
## Ported from Libraries/LibWeb/Layout/Node.cpp:1287 Web::Layout::Node::create_paintable (virtual).
pub func layout_node_create_paintable_impl(this: LayoutNode*) -> PaintPaintable*?:
_ = this
return none