Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / layout / replaced_with_children_formatting_context.lucb

#==============================================================================================
#
#   replaced_with_children_formatting_context - Layout::ReplacedWithChildrenFormattingContext:
#                                               a replaced box whose shadow tree is laid out
#
#   DESCRIPTION:
#       Port of LibWeb/Layout/ReplacedWithChildrenFormattingContext.cpp (lines 1-76): the root
#       box keeps the size its parent formatting context gave it, and the anonymous block
#       container the TreeBuilder wraps its shadow DOM children in is laid out by a
#       BlockFormattingContext.
#
#       Copyright (c) 2026, Gregory Bertilson <gregory@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/Layout/ReplacedWithChildrenFormattingContext.cpp:12 Web::Layout::ReplacedWithChildrenFormattingContext::ReplacedWithChildrenFormattingContext.
pub func layout_replaced_with_children_formatting_context_construct(this: LayoutReplacedWithChildrenFormattingContext*, state: LayoutState*, layout_mode: LayoutMode, box: LayoutBox*, parent: LayoutFormattingContext*?):
    layout_formatting_context_construct(this.layout_formatting_context(), LayoutFormattingContextType.replaced_with_children, layout_mode, state, box, parent)
    layout_formatting_context_set_class(this.layout_formatting_context(), &layout_replaced_with_children_formatting_context_class)
    this.m_automatic_content_width = layout_zero_px()
    this.m_automatic_content_height = layout_zero_px()

## Ported from Libraries/LibWeb/Layout/ReplacedWithChildrenFormattingContext.cpp:17 Web::Layout::ReplacedWithChildrenFormattingContext::run (override).
pub func layout_replaced_with_children_formatting_context_run_impl(base: LayoutFormattingContext*, available_space: LayoutAvailableSpace):
    let this = (LayoutReplacedWithChildrenFormattingContext*)base
    let context_box = layout_formatting_context_context_box(base)
    let root_state = layout_state_get_mutable(base.m_state, context_box.layout_node_with_style())
    let content_width = layout_state_used_values_content_width(root_state)

    # Mark the replaced element as having definite dimensions when the parent FC has
    # computed them from intrinsic size, so children with percentage sizes can resolve.
    let natural_size = layout_box_natural_size(context_box)
    if css_size_with_aspect_ratio_has_width(&natural_size):
        layout_state_used_values_set_has_definite_width(root_state, true)
    if css_size_with_aspect_ratio_has_height(&natural_size):
        layout_state_used_values_set_has_definite_height(root_state, true)

    # For height, use the parent-set content height if it's been resolved (e.g. explicit height
    # or intrinsic height), otherwise use the available space from the parent formatting context.
    let child_available_height = layout_available_size_make_definite(layout_state_used_values_content_height(root_state)) if layout_state_used_values_has_definite_height(root_state) else available_space.height

    let child_available_space = LayoutAvailableSpace(width = layout_available_size_make_definite(content_width), height = child_available_height)

    # The TreeBuilder wraps shadow DOM children in an anonymous BlockContainer.
    # Delegate layout to a BFC for that wrapper.
    let wrapper_node = tree_node_first_child_of_type(context_box.layout_node(), &layout_block_container_class) else return
    let wrapper = (LayoutBlockContainer*)wrapper_node

    let wrapper_state = layout_state_get_mutable(base.m_state, wrapper.layout_node_with_style())
    layout_state_used_values_set_content_width(wrapper_state, content_width)
    layout_state_used_values_set_content_offset(wrapper_state, gfx.CssPixelPoint(layout_zero_px(), layout_zero_px()))

    let bfc = layout_make_block_formatting_context(base.m_state, base.m_layout_mode, wrapper, base)
    defer layout_formatting_context_destruct(bfc.layout_formatting_context())
    layout_formatting_context_run(bfc.layout_formatting_context(), child_available_space)

    this.m_automatic_content_width = content_width
    this.m_automatic_content_height = layout_formatting_context_automatic_content_height(bfc.layout_formatting_context())

    layout_state_used_values_set_content_height(wrapper_state, this.m_automatic_content_height)

    layout_formatting_context_parent_context_did_dimension_child_root_box(bfc.layout_formatting_context())

## Ported from Libraries/LibWeb/Layout/ReplacedWithChildrenFormattingContext.cpp:61 Web::Layout::ReplacedWithChildrenFormattingContext::automatic_content_width (override).
pub func layout_replaced_with_children_formatting_context_automatic_content_width_impl(base: LayoutFormattingContext*) -> gfx.CssPixels:
    return ((LayoutReplacedWithChildrenFormattingContext*)base).m_automatic_content_width

## Ported from Libraries/LibWeb/Layout/ReplacedWithChildrenFormattingContext.cpp:66 Web::Layout::ReplacedWithChildrenFormattingContext::automatic_content_height (override).
pub func layout_replaced_with_children_formatting_context_automatic_content_height_impl(base: LayoutFormattingContext*) -> gfx.CssPixels:
    return ((LayoutReplacedWithChildrenFormattingContext*)base).m_automatic_content_height

## Ported from Libraries/LibWeb/Layout/ReplacedWithChildrenFormattingContext.cpp:71 Web::Layout::ReplacedWithChildrenFormattingContext::parent_context_did_dimension_child_root_box (override).
pub func layout_replaced_with_children_formatting_context_parent_context_did_dimension_child_root_box_impl(base: LayoutFormattingContext*):
    layout_formatting_context_layout_absolutely_positioned_children(base)