dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / layout / block_formatting_context_1.lucb
#==============================================================================================
#
# block_formatting_context_1 - Layout::BlockFormattingContext: construction, the margin
# state, running the context and its automatic sizes
#
# DESCRIPTION:
# Port of LibWeb/Layout/BlockFormattingContext.cpp (lines 1-183) and the inline members of
# BlockFormattingContext.h: the collapsible margin state (BlockMarginState), the float
# side data, the constructor and the destructor (which lays out absolutely positioned
# children its parent never asked for), the automatic content sizes, whether margins
# collapse through a box, run, positioning floats once the root box is dimensioned, and
# which boxes avoid floats.
#
# 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: BlockMarginState ========================================================================
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.h:157 Web::Layout::BlockFormattingContext::BlockMarginState::add_margin.
pub func layout_block_formatting_context_block_margin_state_add_margin(this: LayoutBlockFormattingContextBlockMarginState*, margin: gfx.CssPixels):
if margin.lt(layout_zero_px()):
this.m_current_negative_collapsible_margin = ak.min(margin, this.m_current_negative_collapsible_margin)
else:
this.m_current_positive_collapsible_margin = ak.max(margin, this.m_current_positive_collapsible_margin)
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.h:166 Web::Layout::BlockFormattingContext::BlockMarginState::register_block_container_y_position_update_callback.
pub func layout_block_formatting_context_block_margin_state_register_block_container_y_position_update_callback(this: LayoutBlockFormattingContextBlockMarginState*, callback: ak.Function1[gfx.CssPixels, unit]):
this.m_block_container_y_position_update_callback = callback
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.h:171 Web::Layout::BlockFormattingContext::BlockMarginState::unregister_block_container_y_position_update_callback.
pub func layout_block_formatting_context_block_margin_state_unregister_block_container_y_position_update_callback(this: LayoutBlockFormattingContextBlockMarginState*):
this.m_block_container_y_position_update_callback = ak.Function1[gfx.CssPixels, unit](call = none, context = none)
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.h:176 Web::Layout::BlockFormattingContext::BlockMarginState::current_collapsed_margin.
pub func layout_block_formatting_context_block_margin_state_current_collapsed_margin(this: const LayoutBlockFormattingContextBlockMarginState*) -> gfx.CssPixels:
return this.m_current_positive_collapsible_margin.add(this.m_current_negative_collapsible_margin)
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.h:181 Web::Layout::BlockFormattingContext::BlockMarginState::has_block_container_waiting_for_final_y_position.
pub func layout_block_formatting_context_block_margin_state_has_block_container_waiting_for_final_y_position(this: const LayoutBlockFormattingContextBlockMarginState*) -> bool:
return this.m_block_container_y_position_update_callback.call != none
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.h:186 Web::Layout::BlockFormattingContext::BlockMarginState::update_block_waiting_for_final_y_position.
pub func layout_block_formatting_context_block_margin_state_update_block_waiting_for_final_y_position(this: const LayoutBlockFormattingContextBlockMarginState*):
if let call = this.m_block_container_y_position_update_callback.call:
let collapsed_margin = layout_block_formatting_context_block_margin_state_current_collapsed_margin(this)
call(this.m_block_container_y_position_update_callback.context, collapsed_margin)
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.h:194 Web::Layout::BlockFormattingContext::BlockMarginState::reset.
pub func layout_block_formatting_context_block_margin_state_reset(this: LayoutBlockFormattingContextBlockMarginState*):
this.m_block_container_y_position_update_callback = ak.Function1[gfx.CssPixels, unit](call = none, context = none)
this.m_current_negative_collapsible_margin = layout_zero_px()
this.m_current_positive_collapsible_margin = layout_zero_px()
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.h:201 Web::Layout::BlockFormattingContext::BlockMarginState::box_last_in_flow_child_margin_bottom_collapsed.
pub func layout_block_formatting_context_block_margin_state_box_last_in_flow_child_margin_bottom_collapsed(this: const LayoutBlockFormattingContextBlockMarginState*) -> bool:
return this.m_box_last_in_flow_child_margin_bottom_collapsed
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.h:202 Web::Layout::BlockFormattingContext::BlockMarginState::set_box_last_in_flow_child_margin_bottom_collapsed.
pub func layout_block_formatting_context_block_margin_state_set_box_last_in_flow_child_margin_bottom_collapsed(this: LayoutBlockFormattingContextBlockMarginState*, v: bool):
this.m_box_last_in_flow_child_margin_bottom_collapsed = v
# mark: FloatSideData and the header's members ==================================================
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.h:148 Web::Layout::BlockFormattingContext::FloatSideData::clear.
pub func layout_block_formatting_context_float_side_data_clear(this: LayoutBlockFormattingContextFloatSideData*):
ak.vector_clear(&this.current_boxes)
this.current_width = layout_zero_px()
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.h:29 Web::Layout::BlockFormattingContext::left_side_floats.
pub func layout_block_formatting_context_left_side_floats(this: LayoutBlockFormattingContext*) -> const LayoutBlockFormattingContextFloatSideData*:
return &this.m_left_floats
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.h:30 Web::Layout::BlockFormattingContext::right_side_floats.
pub func layout_block_formatting_context_right_side_floats(this: LayoutBlockFormattingContext*) -> const LayoutBlockFormattingContextFloatSideData*:
return &this.m_right_floats
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.h:37 Web::Layout::BlockFormattingContext::root.
# https://www.w3.org/TR/css-display/#block-formatting-context-root
pub func layout_block_formatting_context_root(this: LayoutBlockFormattingContext*) -> LayoutBlockContainer*:
return (LayoutBlockContainer*)layout_formatting_context_context_box(this.layout_formatting_context())
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.h:45 Web::Layout::BlockFormattingContext::for_each_floating_box: `for
## floating_box in layout_block_formatting_context_for_each_floating_box(context)` visits the left floats, then the
## right ones (a `break` is IterationDecision::Break).
pub func layout_block_formatting_context_for_each_floating_box(this: LayoutBlockFormattingContext*) -> LayoutBlockFormattingContextFloatingBoxes:
return LayoutBlockFormattingContextFloatingBoxes(context = this)
## Every floating box of a block formatting context, as an iterable (for_each_floating_box's loop).
pub struct LayoutBlockFormattingContextFloatingBoxes: luce.Iterable[LayoutBlockFormattingContextFloatingBox*, LayoutBlockFormattingContextFloatingBoxIterator]:
pub var context: LayoutBlockFormattingContext*
func iterator() -> LayoutBlockFormattingContextFloatingBoxIterator:
return LayoutBlockFormattingContextFloatingBoxIterator(context = self.context, right_side = false, index = 0)
## The iterator of LayoutBlockFormattingContextFloatingBoxes: m_left_floats.all_boxes, then m_right_floats.all_boxes.
pub struct LayoutBlockFormattingContextFloatingBoxIterator: luce.Iterator[LayoutBlockFormattingContextFloatingBox*]:
pub var context: LayoutBlockFormattingContext*
pub var right_side: bool
pub var index: usize
func next() -> LayoutBlockFormattingContextFloatingBox*?:
if not self.right_side:
if self.index < ak.vector_size(&self.context.m_left_floats.all_boxes):
let floating_box = *ak.vector_at(&self.context.m_left_floats.all_boxes, self.index)
self.index += 1
return floating_box
self.right_side = true
self.index = 0
if self.index < ak.vector_size(&self.context.m_right_floats.all_boxes):
let floating_box = *ak.vector_at(&self.context.m_right_floats.all_boxes, self.index)
self.index += 1
return floating_box
return none
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.h:77 Web::Layout::BlockFormattingContext::reset_margin_state.
pub func layout_block_formatting_context_reset_margin_state(this: LayoutBlockFormattingContext*):
layout_block_formatting_context_block_margin_state_reset(&this.m_margin_state)
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.h:96 Web::Layout::BlockFormattingContext::last_inserted_float.
pub func layout_block_formatting_context_last_inserted_float(this: LayoutBlockFormattingContext*) -> LayoutBlockFormattingContextFloatingBox*?:
return this.m_last_inserted_float
# mark: Construction ============================================================================
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.cpp:34 Web::Layout::BlockFormattingContext::BlockFormattingContext.
pub func layout_block_formatting_context_construct(this: LayoutBlockFormattingContext*, state: LayoutState*, layout_mode: LayoutMode, root: LayoutBlockContainer*, parent: LayoutFormattingContext*?):
layout_formatting_context_construct(this.layout_formatting_context(), LayoutFormattingContextType.block, layout_mode, state, root.layout_box(), parent)
layout_formatting_context_set_class(this.layout_formatting_context(), &layout_block_formatting_context_class)
# The members' default initializers.
this.m_y_offset_of_current_block_container = none
this.m_margin_state = LayoutBlockFormattingContextBlockMarginState(
m_current_positive_collapsible_margin = layout_zero_px(),
m_current_negative_collapsible_margin = layout_zero_px(),
m_block_container_y_position_update_callback = ak.Function1[gfx.CssPixels, unit](call = none, context = none),
m_box_last_in_flow_child_margin_bottom_collapsed = false,
)
this.m_left_floats = LayoutBlockFormattingContextFloatSideData()
this.m_right_floats = LayoutBlockFormattingContextFloatSideData()
this.m_last_inserted_float = none
this.m_was_notified_after_parent_dimensioned_my_root_box = false
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.cpp:39 Web::Layout::BlockFormattingContext::~BlockFormattingContext.
## (Added by region r17: the destructor has an effect, so its callers run it with `defer`.)
pub func layout_block_formatting_context_destruct(this: LayoutBlockFormattingContext*):
if not this.m_was_notified_after_parent_dimensioned_my_root_box:
# HACK: The parent formatting context never notified us after assigning dimensions to our root box.
# Pretend that it did anyway, to make sure absolutely positioned children get laid out.
# FIXME: Get rid of this hack once parent contexts behave properly.
layout_formatting_context_parent_context_did_dimension_child_root_box(this.layout_formatting_context())
# mark: Automatic sizes =========================================================================
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.cpp:49 Web::Layout::BlockFormattingContext::automatic_content_width (override).
pub func layout_block_formatting_context_automatic_content_width_impl(base: LayoutFormattingContext*) -> gfx.CssPixels:
let this = (LayoutBlockFormattingContext*)base
let root = layout_block_formatting_context_root(this)
if layout_node_children_are_inline(root.layout_node()):
return layout_state_used_values_content_width(layout_state_get(base.m_state, root.layout_node_with_style()))
return layout_formatting_context_greatest_child_width(base, root.layout_box())
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.cpp:56 Web::Layout::BlockFormattingContext::automatic_content_height (override).
pub func layout_block_formatting_context_automatic_content_height_impl(base: LayoutFormattingContext*) -> gfx.CssPixels:
let this = (LayoutBlockFormattingContext*)base
return layout_formatting_context_compute_auto_height_for_block_formatting_context_root(base, layout_block_formatting_context_root(this).layout_box())
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.cpp:61 Web::Layout::margins_collapse_through.
func layout_margins_collapse_through(box: LayoutBox*, state: LayoutState*) -> bool:
# https://drafts.csswg.org/css2/#adjoining-margins
# Two margins are adjoining if and only if:
# - both belong to in-flow block-level boxes that participate in the same block formatting context
# NB: Yes, we're dealing with one and the same box here.
# - no line boxes, no clearance, no padding and no border separate them (Note that certain zero-height line boxes
# (see 9.4.2) are ignored for this purpose.)
# NB: Border and padding are handled further down.
if css_computed_values_clear(layout_box_computed(box)) != css_data.Clear.none_:
return false
# - both belong to vertically-adjacent box edges, i.e. form one of the following pairs:
# - top and bottom margins of a box that does not establish a new block formatting context and that has zero
# computed 'min-height', zero or 'auto' computed 'height', and no in-flow children
if layout_formatting_context_creates_block_formatting_context(box):
return false
# NB: This should take care of the height and min-height constraints.
# ( also see https://github.com/w3c/csswg-drafts/pull/13699#issuecomment-4103045370 for spec ambiguity )
if not gfx.eq_css_pixels_int(layout_state_used_values_border_box_height(layout_state_get(state, box.layout_node_with_style())), 0):
return false
# https://drafts.csswg.org/css-sizing-4/#aspect-ratio-margin-collapse
# FIXME: For the purpose of margin collapsing (CSS 2 §8.3.1 Collapsing margins), if the block axis is the
# ratio-dependent axis, it is not considered to have a computed block-size of auto.
# AD-HOC: The "and no in-flow children" above is wrong. (see https://github.com/w3c/csswg-drafts/pull/13699 )
var child_or_none = tree_node_first_child_of_type(box.layout_node(), &layout_box_class)
while let child = child_or_none:
child_or_none = tree_node_next_sibling_of_type(child, &layout_box_class)
if layout_node_is_out_of_flow(child):
continue
if not layout_margins_collapse_through((LayoutBox*)child, state):
return false
return true
# mark: Running the context =====================================================================
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.cpp:100 Web::Layout::BlockFormattingContext::run (override).
pub func layout_block_formatting_context_run_impl(base: LayoutFormattingContext*, available_space: LayoutAvailableSpace):
let this = (LayoutBlockFormattingContext*)base
let root = layout_block_formatting_context_root(this)
# https://drafts.csswg.org/css-multicol-2/#the-multi-column-model
let root_state = layout_state_get(base.m_state, root.layout_node_with_style())
let column_count = layout_block_formatting_context_determine_used_value_for_column_count(this, layout_state_used_values_content_width(root_state))
if let count = column_count:
let column_width = layout_block_formatting_context_determine_used_value_for_column_width(this, layout_state_used_values_content_width(root_state), count)
# FIXME: Do multi-column layout.
_ = column_width
if let fieldset_box = as_if_layout_field_set_box(root):
if layout_field_set_box_rendered_legend(fieldset_box) != none:
layout_block_formatting_context_layout_fieldset_with_rendered_legend(this, fieldset_box, available_space)
return
if layout_node_children_are_inline(root.layout_node()):
layout_block_formatting_context_layout_inline_children(this, root, available_space)
else:
layout_block_formatting_context_layout_block_level_children(this, root, available_space)
# Fieldsets without a rendered legend skip collapsed margin assignment.
if is_layout_field_set_box(root):
return
# Assign collapsed margin left after children layout of formatting context to the last child box
if not gfx.eq_css_pixels_int(layout_block_formatting_context_block_margin_state_current_collapsed_margin(&this.m_margin_state), 0):
var child_box_or_none = tree_node_last_child_of_type(root.layout_node(), &layout_box_class)
while let child_node = child_box_or_none:
child_box_or_none = tree_node_previous_sibling_of_type(child_node, &layout_box_class)
let child_box = (LayoutBox*)child_node
if layout_node_is_absolutely_positioned(child_node) or layout_node_is_floating(child_node):
continue
if layout_margins_collapse_through(child_box, base.m_state):
continue
layout_state_get_mutable(base.m_state, child_box.layout_node_with_style()).margin_bottom = layout_block_formatting_context_block_margin_state_current_collapsed_margin(&this.m_margin_state)
break
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.cpp:139 Web::Layout::BlockFormattingContext::parent_context_did_dimension_child_root_box (override).
pub func layout_block_formatting_context_parent_context_did_dimension_child_root_box_impl(base: LayoutFormattingContext*):
let this = (LayoutBlockFormattingContext*)base
this.m_was_notified_after_parent_dimensioned_my_root_box = true
# Left-side floats: offset_from_edge is from left edge (0) to left content edge of floating_box.
for floating_box in ak.vector_span(&this.m_left_floats.all_boxes):
layout_state_used_values_set_content_x(floating_box.used_values, floating_box.offset_from_edge)
# Right-side floats: offset_from_edge is from right edge (float_containing_block_width) to the left content edge of floating_box.
for floating_box in ak.vector_span(&this.m_right_floats.all_boxes):
let float_containing_block_width = layout_formatting_context_containing_block_width_for(base, floating_box.box.layout_node_with_style_and_box_model_metrics())
layout_state_used_values_set_content_x(floating_box.used_values, float_containing_block_width.sub(floating_box.offset_from_edge))
layout_formatting_context_layout_absolutely_positioned_children(base)
## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.cpp:157 Web::Layout::BlockFormattingContext::box_should_avoid_floats_because_it_establishes_fc.
pub func layout_block_formatting_context_box_should_avoid_floats_because_it_establishes_fc(this: LayoutBlockFormattingContext*, box: LayoutBox*) -> bool:
_ = this
# https://drafts.csswg.org/css2/#floats
# The border box of a table, a block-level replaced element, or an element in the normal flow that establishes
# a new block formatting context (such as an element with 'overflow' other than 'visible') must not overlap the
# margin box of any floats in the same block formatting context as the element itself. If necessary,
# implementations should clear the said element by placing it below any preceding floats, but may place it
# adjacent to such floats if there is sufficient space. They may even make the border box of said element
# narrower than defined by section 10.3.3. CSS2 does not define when a UA may put said element next to the
# float or by how much said element may become narrower.
# https://drafts.csswg.org/css-flexbox/#flex-containers
# A flex container establishes a new flex formatting context for its contents. This is the same as establishing
# a block formatting context, except that flex layout is used instead of block layout. For example, floats do
# not intrude into the flex container, and the flex container’s margins do not collapse with the margins of its
# contents.
# https://drafts.csswg.org/css-grid/#grid-containers
# A grid container that is not a subgrid establishes an independent grid formatting context for its contents.
# This is the same as establishing an independent block formatting context, except that grid layout is used
# instead of block layout: floats do not intrude into the grid container, and the grid container’s margins do
# not collapse with the margins of its contents.
let formatting_context_type = layout_formatting_context_formatting_context_type_created_by_box(box) else return false
return formatting_context_type == LayoutFormattingContextType.block or formatting_context_type == LayoutFormattingContextType.flex or formatting_context_type == LayoutFormattingContextType.grid