dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / layout / block_formatting_context_4.lucb
#==============================================================================================
#
# block_formatting_context_4 - Layout::BlockFormattingContext: fieldsets, box model metrics,
# clearance and placing block-level boxes
#
# DESCRIPTION:
# Port of LibWeb/Layout/BlockFormattingContext.cpp (lines 1029-1228): the layout of a
# fieldset with a rendered legend, the vertical and horizontal box model metrics of a box,
# clearing floats (clearance), and placing a block-level box in normal flow vertically and
# horizontally (beside the floats it must avoid, honoring the -libweb-center/right
# text-align values).
#
# Copyright (c) 2020-2022, Andreas Kling <andreas@ladybird.org>
# Copyright (c) 2025-2026, Jelle Raaijmakers <jelle@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Fieldsets ===============================================================================
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.cpp:1030 Web::Layout::BlockFormattingContext::layout_fieldset_with_rendered_legend.
# https://html.spec.whatwg.org/multipage/rendering.html#the-fieldset-and-legend-elements
pub func layout_block_formatting_context_layout_fieldset_with_rendered_legend(this: LayoutBlockFormattingContext*, fieldset_box: LayoutFieldSetBox*, available_space: LayoutAvailableSpace):
let base = this.layout_formatting_context()
let fieldset_container = fieldset_box.layout_block_container()
let fieldset_state = layout_state_get_mutable(base.m_state, fieldset_container.layout_node_with_style())
let legend = layout_field_set_box_rendered_legend(fieldset_box) else trap("VERIFY: a fieldset without a rendered legend")
# Lay out the legend to determine its dimensions.
if true:
# TemporaryChange<Optional<CSSPixels>> change { m_y_offset_of_current_block_container, CSSPixels(0) };
let saved_y_offset = this.m_y_offset_of_current_block_container
this.m_y_offset_of_current_block_container = layout_zero_px()
defer this.m_y_offset_of_current_block_container = saved_y_offset
var dummy_bottom = layout_zero_px()
layout_block_formatting_context_layout_block_level_box(this, legend.layout_box(), fieldset_container, &dummy_bottom, available_space)
# If the computed value of 'inline-size' is 'auto', then the used value is the fit-content inline size.
let legend_state = layout_state_get_mutable(base.m_state, legend.layout_node_with_style())
let legend_width = css_computed_values_width(layout_box_computed(legend.layout_box()))
if css_size_is_auto(&legend_width):
let width = layout_formatting_context_calculate_fit_content_width(base, legend.layout_box(), available_space)
layout_state_used_values_set_content_width(legend_state, width)
# The space allocated for the element's border on the block-start side is expected to be the element's
# 'border-block-start-width' or the rendered legend's margin box size in the fieldset's block-flow direction,
# whichever is greater.
let effective_border = ak.max(fieldset_state.border_top, layout_state_used_values_margin_box_height(legend_state))
let extra_top = effective_border.sub(fieldset_state.border_top)
# Lay out non-legend children below the legend accommodation.
layout_block_formatting_context_block_margin_state_reset(&this.m_margin_state)
var bottom_of_lowest_margin_box = layout_zero_px()
if true:
# TemporaryChange<Optional<CSSPixels>> change { m_y_offset_of_current_block_container, extra_top };
let saved_y_offset = this.m_y_offset_of_current_block_container
this.m_y_offset_of_current_block_container = extra_top
defer this.m_y_offset_of_current_block_container = saved_y_offset
for child_node in layout_node_children(fieldset_container.layout_node()):
let child = as_if_layout_box(child_node) else continue
if child_node == legend.layout_node():
continue
layout_block_formatting_context_layout_block_level_box(this, child, fieldset_container, &bottom_of_lowest_margin_box, available_space)
if base.m_layout_mode == LayoutMode.intrinsic_sizing and not layout_state_used_values_has_definite_width(fieldset_state):
var width = layout_formatting_context_greatest_child_width(base, fieldset_container.layout_box())
let computed_values = layout_box_computed(fieldset_container.layout_box())
if fieldset_state.width_constraint == LayoutSizeConstraint.none_:
if not layout_formatting_context_should_treat_max_width_as_none(base, fieldset_container.layout_box(), available_space.width):
let max_width = layout_formatting_context_calculate_inner_width(base, fieldset_container.layout_box(), available_space.width, css_computed_values_max_width(computed_values))
width = ak.min(width, max_width)
let computed_min_width = css_computed_values_min_width(computed_values)
if not css_size_is_auto(&computed_min_width):
let min_width = layout_formatting_context_calculate_inner_width(base, fieldset_container.layout_box(), available_space.width, computed_min_width)
width = ak.max(width, min_width)
layout_state_used_values_set_content_width(fieldset_state, width)
layout_state_used_values_set_content_height(fieldset_state, bottom_of_lowest_margin_box)
# The element is expected to be positioned in the block-flow direction such that its border box is centered over
# the border on the block-start side of the fieldset element.
# FIXME: Take writing modes into consideration.
let legend_border_box_centering_offset = layout_div_int(effective_border.sub(layout_state_used_values_border_box_height(legend_state)), 2)
let fieldset_border_box_top_in_content = fieldset_state.border_top.add(fieldset_state.padding_top).neg()
let legend_content_y = fieldset_border_box_top_in_content.add(legend_border_box_centering_offset).add(layout_state_used_values_border_box_top(legend_state))
layout_state_used_values_set_content_y(legend_state, legend_content_y)
# mark: Box model metrics =======================================================================
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.cpp:1097 Web::Layout::BlockFormattingContext::resolve_vertical_box_model_metrics.
pub func layout_block_formatting_context_resolve_vertical_box_model_metrics(this: LayoutBlockFormattingContext*, box: LayoutBox*, width_of_containing_block: gfx.CssPixels):
let box_state = layout_state_get_mutable(this.formatting_context.m_state, box.layout_node_with_style())
let computed_values = layout_box_computed(box)
let margin = css_computed_values_margin(computed_values)
let padding = css_computed_values_padding(computed_values)
let margin_top = css_length_box_top_const(&margin)
let margin_bottom = css_length_box_bottom_const(&margin)
let padding_top = css_length_box_top_const(&padding)
let padding_bottom = css_length_box_bottom_const(&padding)
box_state.margin_top = css_length_percentage_or_auto_to_px_or_zero(&margin_top, box.layout_node(), width_of_containing_block)
box_state.margin_bottom = css_length_percentage_or_auto_to_px_or_zero(&margin_bottom, box.layout_node(), width_of_containing_block)
box_state.border_top = css_computed_values_border_top(computed_values).width
box_state.border_bottom = css_computed_values_border_bottom(computed_values).width
box_state.padding_top = css_length_percentage_or_auto_to_px_or_zero(&padding_top, box.layout_node(), width_of_containing_block)
box_state.padding_bottom = css_length_percentage_or_auto_to_px_or_zero(&padding_bottom, box.layout_node(), width_of_containing_block)
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.cpp:1110 Web::Layout::BlockFormattingContext::resolve_horizontal_box_model_metrics.
pub func layout_block_formatting_context_resolve_horizontal_box_model_metrics(this: LayoutBlockFormattingContext*, box: LayoutBox*, width_of_containing_block: gfx.CssPixels):
let box_state = layout_state_get_mutable(this.formatting_context.m_state, box.layout_node_with_style())
let computed_values = layout_box_computed(box)
let margin = css_computed_values_margin(computed_values)
let padding = css_computed_values_padding(computed_values)
let margin_left = css_length_box_left_const(&margin)
let margin_right = css_length_box_right_const(&margin)
let padding_left = css_length_box_left_const(&padding)
let padding_right = css_length_box_right_const(&padding)
box_state.margin_left = css_length_percentage_or_auto_to_px_or_zero(&margin_left, box.layout_node(), width_of_containing_block)
box_state.margin_right = css_length_percentage_or_auto_to_px_or_zero(&margin_right, box.layout_node(), width_of_containing_block)
box_state.border_left = css_computed_values_border_left(computed_values).width
box_state.border_right = css_computed_values_border_right(computed_values).width
box_state.padding_left = css_length_percentage_or_auto_to_px_or_zero(&padding_left, box.layout_node(), width_of_containing_block)
box_state.padding_right = css_length_percentage_or_auto_to_px_or_zero(&padding_right, box.layout_node(), width_of_containing_block)
# mark: Clearance ===============================================================================
## The `clear_floating_boxes` lambda of BlockFormattingContext::clear_floating_boxes, for one float side.
func layout_block_formatting_context_clear_floating_boxes_on_side(this: LayoutBlockFormattingContext*, child_box: LayoutNode*, inline_formatting_context: LayoutInlineFormattingContext*?, float_side: LayoutBlockFormattingContextFloatSideData*, result: LayoutBlockFormattingContextDidIntroduceClearance*):
if ak.vector_is_empty(&float_side.current_boxes):
return
# NOTE: Floating boxes are globally relevant within this BFC, *but* their offset coordinates
# are relative to their containing block.
# This means that we have to first convert to a root-space Y coordinate before clearing,
# and then convert back to a local Y coordinate when assigning the cleared offset to
# the `child_box` layout state.
# First, find the lowest margin box edge on this float side and calculate the Y offset just below it.
# NOTE: We must look at all_boxes here, not current_boxes, because current_boxes gets cleared
# when floats are stacked (e.g. multiple width:100% floats). The clear property needs to
# clear past ALL preceding floats, not just the ones on the current "float line".
var clearance_y_in_root = layout_zero_px()
for floating_box in ak.vector_span(&float_side.all_boxes):
clearance_y_in_root = ak.max(clearance_y_in_root, gfx.css_pixel_rect_bottom(&floating_box.margin_box_rect_in_root_coordinate_space))
# Then, convert the clearance Y to a coordinate relative to the containing block of `child_box`.
var clearance_y_in_containing_block = clearance_y_in_root
let root = layout_block_formatting_context_root(this)
var containing_block_or_none = layout_node_containing_block(child_box)
while let containing_block = containing_block_or_none:
if containing_block == root.layout_box():
break
clearance_y_in_containing_block = clearance_y_in_containing_block.sub(layout_state_get(this.formatting_context.m_state, containing_block.layout_node_with_style()).offset.m_y)
containing_block_or_none = layout_node_containing_block(containing_block.layout_node())
if let inline_context = inline_formatting_context:
if clearance_y_in_containing_block.gt(layout_inline_formatting_context_vertical_float_clearance(inline_context)):
*result = LayoutBlockFormattingContextDidIntroduceClearance.yes
layout_inline_formatting_context_set_vertical_float_clearance(inline_context, clearance_y_in_containing_block)
elif clearance_y_in_containing_block.gt(this.m_y_offset_of_current_block_container else trap("no current block container y offset")):
*result = LayoutBlockFormattingContextDidIntroduceClearance.yes
this.m_y_offset_of_current_block_container = clearance_y_in_containing_block
if not layout_node_is_floating(child_box):
layout_block_formatting_context_float_side_data_clear(float_side)
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.cpp:1123 Web::Layout::BlockFormattingContext::clear_floating_boxes.
pub func layout_block_formatting_context_clear_floating_boxes(this: LayoutBlockFormattingContext*, child_box: LayoutNode*, inline_formatting_context: LayoutInlineFormattingContext*?) -> LayoutBlockFormattingContextDidIntroduceClearance:
let clear = css_computed_values_clear(&layout_node_computed_values(child_box).computed_values)
var result = LayoutBlockFormattingContextDidIntroduceClearance.no
# FIXME: Honor writing-mode, direction and text-orientation.
if clear == css_data.Clear.left or clear == css_data.Clear.both or clear == css_data.Clear.inline_start:
layout_block_formatting_context_clear_floating_boxes_on_side(this, child_box, inline_formatting_context, &this.m_left_floats, &result)
if clear == css_data.Clear.right or clear == css_data.Clear.both or clear == css_data.Clear.inline_end:
layout_block_formatting_context_clear_floating_boxes_on_side(this, child_box, inline_formatting_context, &this.m_right_floats, &result)
return result
# mark: Placing block-level boxes ===============================================================
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.cpp:1174 Web::Layout::BlockFormattingContext::place_block_level_element_in_normal_flow_vertically.
pub func layout_block_formatting_context_place_block_level_element_in_normal_flow_vertically(this: LayoutBlockFormattingContext*, child_box: LayoutBox*, y: gfx.CssPixels):
let base = this.layout_formatting_context()
let box_state = layout_state_get_mutable(base.m_state, child_box.layout_node_with_style())
let new_y = y.add(layout_state_used_values_border_box_top(box_state))
layout_state_used_values_set_content_y(box_state, new_y)
let root = layout_block_formatting_context_root(this).layout_box()
for float_box in ak.vector_span(&this.m_left_floats.all_boxes):
float_box.margin_box_rect_in_root_coordinate_space = layout_formatting_context_margin_box_rect_in_ancestor_coordinate_space_layout_state_used_values(base, float_box.used_values, root)
for float_box in ak.vector_span(&this.m_right_floats.all_boxes):
float_box.margin_box_rect_in_root_coordinate_space = layout_formatting_context_margin_box_rect_in_ancestor_coordinate_space_layout_state_used_values(base, float_box.used_values, root)
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.cpp:1186 Web::Layout::BlockFormattingContext::place_block_level_element_in_normal_flow_horizontally.
pub func layout_block_formatting_context_place_block_level_element_in_normal_flow_horizontally(this: LayoutBlockFormattingContext*, child_box: LayoutBox*, available_space: LayoutAvailableSpace):
let base = this.layout_formatting_context()
let box_state = layout_state_get_mutable(base.m_state, child_box.layout_node_with_style())
var x = layout_zero_px()
var available_width_within_containing_block = layout_available_size_to_px_or_zero(&available_space.width)
if (not ak.vector_is_empty(&this.m_left_floats.current_boxes) or not ak.vector_is_empty(&this.m_right_floats.current_boxes)) and layout_block_formatting_context_box_should_avoid_floats_because_it_establishes_fc(this, child_box):
let box_in_root_rect = layout_formatting_context_margin_box_rect_in_ancestor_coordinate_space_layout_state_used_values(base, box_state, layout_block_formatting_context_root(this).layout_box())
let space_and_containing_margin = layout_block_formatting_context_space_used_and_containing_margin_for_floats(this, gfx.css_pixel_rect_y(&box_in_root_rect))
available_width_within_containing_block = available_width_within_containing_block.sub(space_and_containing_margin.left_used_space.add(space_and_containing_margin.right_used_space))
# Since this box has a FC, it should avoid floats which means we cannot have its border box overlap with any
# float's margin box. We start off at the right-most border of the floats, and if this box' margin-left is not
# auto, we must overlap that margin with the floats as far as possible.
x = space_and_containing_margin.left_used_space
let margin = css_computed_values_margin(layout_box_computed(child_box))
let margin_left = css_length_box_left_const(&margin)
if not css_length_percentage_or_auto_is_auto(&margin_left):
x = ak.max(x.sub(ak.max(box_state.margin_left, layout_zero_px())), layout_zero_px())
# All non-anonymous containing blocks that are ancestors of the child box, but are not ancestors of the left
# float box, might have left margins set that overlap with the used float space.
if let matching_left_float_box = space_and_containing_margin.matching_left_float_box:
var current_ancestor = layout_node_non_anonymous_containing_block(child_box.layout_node()) else trap("a box without a non-anonymous containing block")
while not tree_node_is_ancestor_of(current_ancestor.layout_node(), matching_left_float_box.layout_node()):
x = x.sub(layout_state_get(base.m_state, current_ancestor.layout_node_with_style()).margin_left)
current_ancestor = layout_node_non_anonymous_containing_block(current_ancestor.layout_node()) else trap("a box without a non-anonymous containing block")
x = ak.max(x, layout_zero_px())
let containing_block = layout_node_containing_block(child_box.layout_node()) else trap("a block-level box without a containing block")
let text_align = css_computed_values_text_align(layout_box_computed(containing_block))
if text_align == css_data.TextAlign.libweb_center:
x = x.add(layout_div_int(available_width_within_containing_block, 2).sub(layout_div_int(layout_state_used_values_content_width(box_state), 2)))
elif text_align == css_data.TextAlign.libweb_right:
# Subtracting the left margin here because left and right margins need to be swapped when aligning to the right
x = x.add(available_width_within_containing_block.sub(layout_state_used_values_content_width(box_state)).sub(layout_state_used_values_margin_box_left(box_state)))
else:
x = x.add(layout_state_used_values_margin_box_left(box_state))
layout_state_used_values_set_content_x(box_state, x)