Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / layout / flex_formatting_context_1.lucb

#==============================================================================================
#
#   flex_formatting_context_1 - Layout::FlexFormattingContext: construction, the layout
#                               algorithm's driver, axes and the items' box model
#
#   DESCRIPTION:
#       Port of LibWeb/Layout/FlexFormattingContext.cpp (lines 1-347) and the inline members of
#       FlexFormattingContext.h: the items' outer sizes, sizes resolved against the available
#       space, the constructor and the automatic content sizes, `run` (the flex layout
#       algorithm, https://www.w3.org/TR/css-flexbox-1/#layout-algorithm, step by step),
#       placing absolutely positioned children once the container is dimensioned, the main and
#       cross axes and their directions, and the margins, borders and padding of an item in
#       axis-agnostic terms.
#
#       Copyright (c) 2021-2025, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
#       Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: FlexFormattingContext.h's inline members ================================================

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.h:19 Web::Layout::FlexFormattingContext::inhibits_floating (override).
pub func layout_flex_formatting_context_inhibits_floating_impl(base: LayoutFormattingContext*) -> bool:
    _ = base
    return true

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.h:25 Web::Layout::FlexFormattingContext::flex_container.
pub func layout_flex_formatting_context_flex_container(this: LayoutFlexFormattingContext*) -> LayoutBox*:
    return layout_formatting_context_context_box(this.layout_formatting_context())

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.h:65 Web::Layout::FlexFormattingContext::FlexItem::hypothetical_cross_size_with_margins.
func layout_flex_formatting_context_flex_item_hypothetical_cross_size_with_margins(this: LayoutFlexFormattingContextFlexItem*) -> gfx.CssPixels:
    return this.hypothetical_cross_size.add(this.margins.cross_before).add(this.margins.cross_after).add(this.borders.cross_after).add(this.borders.cross_before).add(this.padding.cross_after).add(this.padding.cross_before)

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.h:72 Web::Layout::FlexFormattingContext::FlexItem::outer_hypothetical_main_size.
func layout_flex_formatting_context_flex_item_outer_hypothetical_main_size(this: const LayoutFlexFormattingContextFlexItem*) -> gfx.CssPixels:
    return this.hypothetical_main_size.add(this.margins.main_before).add(this.margins.main_after).add(this.borders.main_before).add(this.borders.main_after).add(this.padding.main_before).add(this.padding.main_after)

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.h:77 Web::Layout::FlexFormattingContext::FlexItem::outer_target_main_size.
func layout_flex_formatting_context_flex_item_outer_target_main_size(this: const LayoutFlexFormattingContextFlexItem*) -> gfx.CssPixels:
    return this.target_main_size.add(this.margins.main_before).add(this.margins.main_after).add(this.borders.main_before).add(this.borders.main_after).add(this.padding.main_before).add(this.padding.main_after)

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.h:82 Web::Layout::FlexFormattingContext::FlexItem::outer_flex_base_size.
func layout_flex_formatting_context_flex_item_outer_flex_base_size(this: const LayoutFlexFormattingContextFlexItem*) -> gfx.CssPixels:
    return this.flex_base_size.add(this.margins.main_before).add(this.margins.main_after).add(this.borders.main_before).add(this.borders.main_after).add(this.padding.main_before).add(this.padding.main_after)

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.h:101 Web::Layout::FlexFormattingContext::FlexItem::add_main_margin_box_sizes.
func layout_flex_formatting_context_flex_item_add_main_margin_box_sizes(this: const LayoutFlexFormattingContextFlexItem*, content_size: gfx.CssPixels) -> gfx.CssPixels:
    return content_size.add(this.margins.main_before).add(this.margins.main_after).add(this.borders.main_before).add(this.borders.main_after).add(this.padding.main_before).add(this.padding.main_after)

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.h:106 Web::Layout::FlexFormattingContext::FlexItem::add_cross_margin_box_sizes.
func layout_flex_formatting_context_flex_item_add_cross_margin_box_sizes(this: const LayoutFlexFormattingContextFlexItem*, content_size: gfx.CssPixels) -> gfx.CssPixels:
    return content_size.add(this.margins.cross_before).add(this.margins.cross_after).add(this.borders.cross_before).add(this.borders.cross_after).add(this.padding.cross_before).add(this.padding.cross_after)

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.h:127 Web::Layout::FlexFormattingContext::has_definite_main_size (of a FlexItem).
func layout_flex_formatting_context_has_definite_main_size_layout_flex_formatting_context(this: LayoutFlexFormattingContext*, item: const LayoutFlexFormattingContextFlexItem*) -> bool:
    return layout_flex_formatting_context_has_definite_main_size_layout_state_used_values(this, item.used_values)

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.h:128 Web::Layout::FlexFormattingContext::has_definite_cross_size (of a FlexItem).
func layout_flex_formatting_context_has_definite_cross_size_layout_flex_formatting_context(this: LayoutFlexFormattingContext*, item: const LayoutFlexFormattingContextFlexItem*) -> bool:
    return layout_flex_formatting_context_has_definite_cross_size_layout_state_used_values(this, item.used_values)

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.h:132 Web::Layout::FlexFormattingContext::inner_main_size (of a FlexItem).
func layout_flex_formatting_context_inner_main_size_layout_flex_formatting_context(this: LayoutFlexFormattingContext*, item: const LayoutFlexFormattingContextFlexItem*) -> gfx.CssPixels:
    return layout_flex_formatting_context_inner_main_size_layout_state_used_values(this, item.used_values)

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.h:133 Web::Layout::FlexFormattingContext::inner_cross_size (of a FlexItem).
func layout_flex_formatting_context_inner_cross_size_layout_flex_formatting_context(this: LayoutFlexFormattingContext*, item: const LayoutFlexFormattingContextFlexItem*) -> gfx.CssPixels:
    return layout_flex_formatting_context_inner_cross_size_layout_state_used_values(this, item.used_values)

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.h:207 Web::Layout::FlexFormattingContext::is_row_layout.
func layout_flex_formatting_context_is_row_layout(this: LayoutFlexFormattingContext*) -> bool:
    return this.m_flex_direction == css_data.FlexDirection.row or this.m_flex_direction == css_data.FlexDirection.row_reverse

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.h:208 Web::Layout::FlexFormattingContext::is_single_line.
func layout_flex_formatting_context_is_single_line(this: LayoutFlexFormattingContext*) -> bool:
    return css_computed_values_flex_wrap(layout_box_computed(layout_flex_formatting_context_flex_container(this))) == css_data.FlexWrap.nowrap

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.h:211 Web::Layout::FlexFormattingContext::cross_axis_is_horizontal.
func layout_flex_formatting_context_cross_axis_is_horizontal(this: LayoutFlexFormattingContext*) -> bool:
    return not layout_flex_formatting_context_main_axis_is_horizontal(this)

## `m_available_space.value()` (and `m_available_space->`): VERIFYs that run() has set it.
func layout_flex_formatting_context_available_space(this: LayoutFlexFormattingContext*) -> LayoutAvailableSpace:
    return this.m_available_space else trap("VERIFY: m_available_space has a value")

## `m_available_space_for_items->`: VERIFYs that determine_available_space_for_items() has set it.
func layout_flex_formatting_context_available_space_for_items(this: LayoutFlexFormattingContext*) -> LayoutFlexFormattingContextAxisAgnosticAvailableSpace:
    return this.m_available_space_for_items else trap("VERIFY: m_available_space_for_items has a value")

## `line.items.size() - 1` and friends as CSSPixels: `CSSPixels * size_t` converts the unsigned count with
## CSSPixels(Unsigned), which saturates (so the count of an empty line, SIZE_MAX, is the largest value).
func layout_flex_formatting_context_mul_count(value: gfx.CssPixels, count: usize) -> gfx.CssPixels:
    return value.mul(gfx.css_pixels_from_unsigned((u64)count))

# mark: Sizes against the available space =======================================================

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.cpp:19 Web::Layout::FlexFormattingContext::get_pixel_width.
func layout_flex_formatting_context_get_pixel_width(this: LayoutFlexFormattingContext*, item: const LayoutFlexFormattingContextFlexItem*, size: CssSize) -> gfx.CssPixels:
    return layout_formatting_context_calculate_inner_width(this.layout_formatting_context(), item.box, layout_flex_formatting_context_available_space(this).width, size)

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.cpp:24 Web::Layout::FlexFormattingContext::get_pixel_height.
func layout_flex_formatting_context_get_pixel_height(this: LayoutFlexFormattingContext*, item: const LayoutFlexFormattingContextFlexItem*, size: CssSize) -> gfx.CssPixels:
    if layout_flex_formatting_context_main_axis_is_horizontal(this) and css_size_is_intrinsic_sizing_constraint(&size):
        # NOTE: When the main axis is horizontal, after we've determined the main size, we use that as the
        #       available width for any
        #       intrinsic sizing layout needed to resolve the height.
        var available_width = layout_available_size_make_indefinite()
        if let main_size = item.main_size:
            available_width = layout_available_size_make_definite(layout_clamp_to_max_dimension_value(main_size))
        let available_height = layout_available_size_make_indefinite()
        let available_space = LayoutAvailableSpace(width = available_width, height = available_height)
        return layout_formatting_context_calculate_inner_height(this.layout_formatting_context(), item.box, available_space, size)
    return layout_formatting_context_calculate_inner_height(this.layout_formatting_context(), item.box, layout_flex_formatting_context_available_space(this), size)

# mark: Construction ============================================================================

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.cpp:38 Web::Layout::FlexFormattingContext::FlexFormattingContext.
pub func layout_flex_formatting_context_construct(this: LayoutFlexFormattingContext*, state: LayoutState*, layout_mode: LayoutMode, flex_container: LayoutBox*, parent: LayoutFormattingContext*?):
    layout_formatting_context_construct(this.layout_formatting_context(), LayoutFormattingContextType.flex, layout_mode, state, flex_container, parent)
    layout_formatting_context_set_class(this.layout_formatting_context(), &layout_flex_formatting_context_class)
    this.m_flex_container_state = layout_state_get_mutable(state, flex_container.layout_node_with_style())
    this.m_flex_lines = ak.Vector[LayoutFlexFormattingContextFlexLine]()
    this.m_flex_items = ak.Vector[LayoutFlexFormattingContextFlexItem]()
    this.m_flex_direction = css_computed_values_flex_direction(layout_box_computed(flex_container))
    this.m_available_space_for_items = none
    this.m_available_space = none

# FlexFormattingContext::~FlexFormattingContext() is the default destructor: layout_formatting_context_destruct has
# nothing to run for this context.

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.cpp:47 Web::Layout::FlexFormattingContext::automatic_content_width (override).
pub func layout_flex_formatting_context_automatic_content_width_impl(base: LayoutFormattingContext*) -> gfx.CssPixels:
    let this = (LayoutFlexFormattingContext*)base
    return layout_state_used_values_content_width(this.m_flex_container_state)

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.cpp:52 Web::Layout::FlexFormattingContext::automatic_content_height (override).
pub func layout_flex_formatting_context_automatic_content_height_impl(base: LayoutFormattingContext*) -> gfx.CssPixels:
    let this = (LayoutFlexFormattingContext*)base
    return layout_state_used_values_content_height(this.m_flex_container_state)

# mark: The layout algorithm ====================================================================

## The `should_skip_automatic_minimum_size_clamp` lambda of FlexFormattingContext::run.
func layout_flex_formatting_context_should_skip_automatic_minimum_size_clamp(this: LayoutFlexFormattingContext*) -> bool:
    if this.formatting_context.m_layout_mode == LayoutMode.intrinsic_sizing:
        return false
    if not layout_flex_formatting_context_is_single_line(this):
        return false
    if not layout_flex_formatting_context_has_definite_main_size_layout_state_used_values(this, this.m_flex_container_state):
        return false
    var sum_of_item_flex_base_sizes = layout_zero_px()
    let items = ak.vector_span(&this.m_flex_items)
    for item in &items:
        if not layout_flex_formatting_context_has_definite_main_size_layout_flex_formatting_context(this, item):
            return false
        sum_of_item_flex_base_sizes = sum_of_item_flex_base_sizes.add(item.flex_base_size)
    let available_main = layout_flex_formatting_context_available_space_for_items(this).main
    if sum_of_item_flex_base_sizes.gt(layout_available_size_to_px_or_zero(&available_main)):
        return false
    return true

## The `can_skip_automatic_minimum_size_for_item` lambda of FlexFormattingContext::run.
func layout_flex_formatting_context_can_skip_automatic_minimum_size_for_item(this: LayoutFlexFormattingContext*, item: const LayoutFlexFormattingContextFlexItem*) -> bool:
    if layout_node_with_style_is_scroll_container(item.box.layout_node_with_style()):
        return false
    if layout_node_is_replaced_box(item.box.layout_node()) or layout_box_has_preferred_aspect_ratio(item.box):
        return false
    if not item.used_flex_basis_is_definite:
        return false
    # `flex_base_size == specified_size_suggestion(item)`: an empty Optional equals nothing.
    let specified_size_suggestion = layout_flex_formatting_context_specified_size_suggestion(this, item) else return false
    return item.flex_base_size == specified_size_suggestion

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.cpp:57 Web::Layout::FlexFormattingContext::run (override).
pub func layout_flex_formatting_context_run_impl(base: LayoutFormattingContext*, available_space: LayoutAvailableSpace):
    let this = (LayoutFlexFormattingContext*)base
    # This implements https://www.w3.org/TR/css-flexbox-1/#layout-algorithm

    # OPTIMIZATION: If we're in intrinsic sizing layout, but the flex container is not the
    #               box being measured, we can skip everything here.
    #               The parent formatting context has already figured out our size anyway.
    if base.m_layout_mode == LayoutMode.intrinsic_sizing and not layout_available_size_is_intrinsic_sizing_constraint(&available_space.width) and not layout_available_size_is_intrinsic_sizing_constraint(&available_space.height):
        return

    # FORMATTING_CONTEXT_TRACE() traces only in LIBWEB_LAYOUT_DEBUG builds.
    this.m_available_space = available_space

    # 1. Generate anonymous flex items
    layout_flex_formatting_context_generate_anonymous_flex_items(this)

    # 2. Determine the available main and cross space for the flex items
    layout_flex_formatting_context_determine_available_space_for_items(this, available_space)

    let items = ak.vector_span(&this.m_flex_items)

    # https://drafts.csswg.org/css-flexbox-1/#definite-sizes
    # 3. If a single-line flex container has a definite cross size,
    #    the automatic preferred outer cross size of any stretched flex items is the flex container’s inner cross size
    #    (clamped to the flex item’s min and max cross size) and is considered definite.
    if layout_flex_formatting_context_is_single_line(this) and layout_flex_formatting_context_has_definite_cross_size_layout_state_used_values(this, this.m_flex_container_state):
        let flex_container_inner_cross_size = layout_flex_formatting_context_inner_cross_size_layout_state_used_values(this, this.m_flex_container_state)
        for stretched_item in &items:
            if not layout_flex_formatting_context_flex_item_is_stretched(this, stretched_item):
                continue
            let item_min_cross_size = layout_flex_formatting_context_specified_cross_min_size(this, stretched_item) if layout_flex_formatting_context_has_cross_min_size(this, stretched_item.box) else layout_zero_px()
            let item_max_cross_size = layout_flex_formatting_context_specified_cross_max_size(this, stretched_item) if layout_flex_formatting_context_has_cross_max_size(this, stretched_item.box) else gfx.css_pixels_max()
            let item_preferred_outer_cross_size = layout_css_clamp(flex_container_inner_cross_size, item_min_cross_size, item_max_cross_size)
            let item_inner_cross_size = item_preferred_outer_cross_size.sub(stretched_item.margins.cross_before).sub(stretched_item.margins.cross_after).sub(stretched_item.padding.cross_before).sub(stretched_item.padding.cross_after).sub(stretched_item.borders.cross_before).sub(stretched_item.borders.cross_after)
            layout_flex_formatting_context_set_cross_size_layout_flex_formatting_context(this, stretched_item, item_inner_cross_size)
            layout_flex_formatting_context_set_has_definite_cross_size(this, stretched_item)

    # 3. Determine the flex base size and hypothetical main size of each item
    for base_size_item in &items:
        layout_flex_formatting_context_determine_flex_base_size(this, base_size_item)

    # OPTIMIZATION: We try to avoid calculating the "automatic minimum size" if possible,
    #               since it may require expensive intrinsic sizing layout.
    #               If this is a single-line flex container and the sum of flex bases sizes
    #               won't exceed the available space in the main axis, we know the layout
    #               algorithm won't have to shrink anything, thus not needing the minimum size.
    let should_skip_automatic_minimum_size_clamp = layout_flex_formatting_context_should_skip_automatic_minimum_size_clamp(this)

    # OPTIMIZATION: Skip automatic_minimum_size calculation when we can prove it won't affect the result.
    #               We compute:
    #                   hypothetical_main_size = clamp(flex_base_size, automatic_minimum_size, max_size)
    #               For non-replaced elements without aspect ratio, the spec defines:
    #                   automatic_minimum_size = content_based_minimum_size
    #                   content_based_minimum_size = min(content_size_suggestion, specified_size_suggestion)
    #               This means:
    #                   automatic_minimum_size <= specified_size_suggestion (when specified exists).
    #.              So if flex_base_size == specified_size_suggestion, then automatic_minimum_size <= flex_base_size,
    #               and the lower clamp becomes a no-op. We can substitute 0 and get the same result.
    #.              We exclude: scroll containers (content can overflow, different behavior), replaced elements (use
    #               different formula with intrinsic sizes), and items with aspect-ratio (transferred_size_suggestion
    #               complicates the calculation).
    # (can_skip_automatic_minimum_size_for_item is layout_flex_formatting_context_can_skip_automatic_minimum_size_for_item.)

    for hypothetical_item in &items:
        # The hypothetical main size is the item’s flex base size clamped according to its used min and max main sizes (and flooring the content box size at zero).
        let clamp_max = layout_flex_formatting_context_specified_main_max_size(this, hypothetical_item) if layout_flex_formatting_context_has_main_max_size(this, hypothetical_item.box) else gfx.css_pixels_max()
        var clamp_min: gfx.CssPixels
        if layout_flex_formatting_context_has_main_min_size(this, hypothetical_item.box):
            clamp_min = layout_flex_formatting_context_specified_main_min_size(this, hypothetical_item)
        elif should_skip_automatic_minimum_size_clamp or layout_flex_formatting_context_can_skip_automatic_minimum_size_for_item(this, hypothetical_item):
            clamp_min = layout_zero_px()
        else:
            clamp_min = layout_flex_formatting_context_automatic_minimum_size(this, hypothetical_item)
        hypothetical_item.hypothetical_main_size = ak.max(layout_zero_px(), layout_css_clamp(hypothetical_item.flex_base_size, clamp_min, clamp_max))

    # (Both branches of the C++ `if` below hold only comments.)
    # if (available_space.width.is_intrinsic_sizing_constraint() || available_space.height.is_intrinsic_sizing_constraint()):
    #     We're computing intrinsic size for the flex container. This happens at the end of run().
    # else:
    #     4. Determine the main size of the flex container
    #     Determine the main size of the flex container using the rules of the formatting context in which it participates.
    #     NOTE: The automatic block size of a block-level flex container is its max-content size.
    #
    #     NOTE: We've already handled this in the parent formatting context.
    #           Specifically, all formatting contexts will have assigned width & height to the flex container
    #           before this formatting context runs.

    # 5. Collect flex items into flex lines:
    # After this step no additional items are to be added to flex_lines or any of its items!
    layout_flex_formatting_context_collect_flex_items_into_flex_lines(this)

    # 6. Resolve the flexible lengths
    layout_flex_formatting_context_resolve_flexible_lengths(this)

    # Cross Size Determination
    # 7. Determine the hypothetical cross size of each item
    for cross_item in &items:
        layout_flex_formatting_context_determine_hypothetical_cross_size_of_item(this, cross_item)

    # 8. Calculate the cross size of each flex line.
    layout_flex_formatting_context_calculate_cross_size_of_each_flex_line(this)

    # 9. Handle 'align-content: stretch'.
    layout_flex_formatting_context_handle_align_content_stretch(this)

    # 10. Collapse visibility:collapse items.
    # FIXME: This

    # 11. Determine the used cross size of each flex item.
    layout_flex_formatting_context_determine_used_cross_size_of_each_flex_item(this)

    # 12. Distribute any remaining free space.
    layout_flex_formatting_context_distribute_any_remaining_free_space(this)

    # 13. Resolve cross-axis auto margins.
    layout_flex_formatting_context_resolve_cross_axis_auto_margins(this)

    # 14. Align all flex items along the cross-axis
    layout_flex_formatting_context_align_all_flex_items_along_the_cross_axis(this)

    # 15. Determine the flex container’s used cross size
    # NOTE: This is handled by the parent formatting context.

    # https://drafts.csswg.org/css-flexbox-1/#definite-sizes
    # 4. Once the cross size of a flex line has been determined,
    #    the cross sizes of items in auto-sized flex containers are also considered definite for the purpose of layout.
    if layout_flex_formatting_context_should_treat_cross_size_as_auto(this, layout_flex_formatting_context_flex_container(this)):
        for definite_item in &items:
            layout_flex_formatting_context_set_cross_size_layout_flex_formatting_context(this, definite_item, definite_item.cross_size else trap("VERIFY: the item's cross size is determined"))
            layout_flex_formatting_context_set_has_definite_cross_size(this, definite_item)

    # 16. Align all flex lines (per align-content)
    layout_flex_formatting_context_align_all_flex_lines(this)

    if base.m_layout_mode == LayoutMode.intrinsic_sizing:
        # We're computing intrinsic size for the flex container.
        layout_flex_formatting_context_determine_intrinsic_size_of_flex_container(this)
    else:
        # This is a normal layout (not intrinsic sizing).
        # AD-HOC: Finally, layout the inside of all flex items.
        layout_flex_formatting_context_copy_dimensions_from_flex_items_to_boxes(this)
        let space = layout_flex_formatting_context_available_space_for_items(this).space
        for laid_out_item in &items:
            let independent_formatting_context = layout_formatting_context_layout_inside(base, laid_out_item.box, LayoutMode.normal, layout_state_used_values_available_inner_space_or_constraints_from(laid_out_item.used_values, space))
            if let context = independent_formatting_context:
                # The OwnPtr goes out of scope at the end of the `if`.
                defer layout_formatting_context_destruct(context)
                layout_formatting_context_parent_context_did_dimension_child_root_box(context)

            let content_box_rect = layout_formatting_context_content_box_rect_layout_state_used_values(base, this.m_flex_container_state)
            layout_formatting_context_compute_inset(base, laid_out_item.box.layout_node_with_style_and_box_model_metrics(), gfx.css_pixel_rect_size(&content_box_rect))

        layout_flex_formatting_context_resolve_baseline_aligned_items(this)

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.cpp:244 Web::Layout::FlexFormattingContext::parent_context_did_dimension_child_root_box (override).
pub func layout_flex_formatting_context_parent_context_did_dimension_child_root_box_impl(base: LayoutFormattingContext*):
    let this = (LayoutFlexFormattingContext*)base
    if base.m_layout_mode != LayoutMode.normal:
        return

    var child_or_none = tree_node_first_child_of_type(layout_flex_formatting_context_flex_container(this).layout_node(), &layout_box_class)
    while let child = child_or_none:
        child_or_none = tree_node_next_sibling_of_type(child, &layout_box_class)
        let box = (LayoutBox*)child
        if layout_node_is_absolutely_positioned(box.layout_node()):
            layout_state_used_values_set_static_position_rect(layout_state_get_mutable(base.m_state, box.layout_node_with_style()), layout_flex_formatting_context_calculate_static_position_rect(this, box))

    layout_formatting_context_layout_absolutely_positioned_children(base)

# mark: Axes ====================================================================================

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.cpp:260 Web::Layout::FlexFormattingContext::inline_axis_is_horizontal.
# https://www.w3.org/TR/css-flexbox-1/#flex-direction-property
func layout_flex_formatting_context_inline_axis_is_horizontal(this: LayoutFlexFormattingContext*, box: LayoutBox*) -> bool:
    _ = this
    return css_computed_values_writing_mode(layout_box_computed(box)) == css_data.WritingMode.horizontal_tb

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.cpp:265 Web::Layout::FlexFormattingContext::main_axis_is_horizontal.
func layout_flex_formatting_context_main_axis_is_horizontal(this: LayoutFlexFormattingContext*) -> bool:
    let flex_container = layout_flex_formatting_context_flex_container(this)
    if layout_flex_formatting_context_is_row_layout(this):
        return layout_flex_formatting_context_inline_axis_is_horizontal(this, flex_container)
    return not layout_flex_formatting_context_inline_axis_is_horizontal(this, flex_container)

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.cpp:270 Web::Layout::FlexFormattingContext::main_axis_is_parallel_to_inline_axis.
func layout_flex_formatting_context_main_axis_is_parallel_to_inline_axis(this: LayoutFlexFormattingContext*, box: LayoutBox*) -> bool:
    return layout_flex_formatting_context_main_axis_is_horizontal(this) == layout_flex_formatting_context_inline_axis_is_horizontal(this, box)

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.cpp:275 Web::Layout::FlexFormattingContext::cross_axis_is_reverse.
func layout_flex_formatting_context_cross_axis_is_reverse(this: LayoutFlexFormattingContext*) -> bool:
    let flex_container = layout_flex_formatting_context_flex_container(this)
    let computed_values = layout_box_computed(flex_container)
    var reverse = css_computed_values_block_axis_is_reverse(computed_values) if layout_flex_formatting_context_main_axis_is_parallel_to_inline_axis(this, flex_container) else css_computed_values_inline_axis_is_reverse(computed_values)
    if css_computed_values_flex_wrap(computed_values) == css_data.FlexWrap.wrap_reverse:
        reverse = not reverse
    return reverse

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.cpp:286 Web::Layout::FlexFormattingContext::is_direction_reverse.
func layout_flex_formatting_context_is_direction_reverse(this: LayoutFlexFormattingContext*) -> bool:
    let computed_values = layout_box_computed(layout_flex_formatting_context_flex_container(this))
    var reverse = css_computed_values_inline_axis_is_reverse(computed_values) if layout_flex_formatting_context_is_row_layout(this) else css_computed_values_block_axis_is_reverse(computed_values)
    if this.m_flex_direction == css_data.FlexDirection.column_reverse or this.m_flex_direction == css_data.FlexDirection.row_reverse:
        reverse = not reverse
    return reverse

# mark: Margins, borders and padding ============================================================

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.cpp:297 Web::Layout::FlexFormattingContext::populate_specified_margins.
func layout_flex_formatting_context_populate_specified_margins(this: LayoutFlexFormattingContext*, item: LayoutFlexFormattingContextFlexItem*, arg1: css_data.FlexDirection):
    _ = arg1
    let width_of_containing_block = layout_state_used_values_content_width(this.m_flex_container_state)
    let node = item.box.layout_node()
    let computed_values = layout_box_computed(item.box)
    let padding = css_computed_values_padding(computed_values)
    let padding_left = css_length_box_left_const(&padding)
    let padding_right = css_length_box_right_const(&padding)
    let padding_top = css_length_box_top_const(&padding)
    let padding_bottom = css_length_box_bottom_const(&padding)
    let margin = css_computed_values_margin(computed_values)
    let margin_left = css_length_box_left_const(&margin)
    let margin_right = css_length_box_right_const(&margin)
    let margin_top = css_length_box_top_const(&margin)
    let margin_bottom = css_length_box_bottom_const(&margin)

    item.used_values.padding_left = css_length_percentage_or_auto_to_px_or_zero(&padding_left, node, width_of_containing_block)
    item.used_values.padding_right = css_length_percentage_or_auto_to_px_or_zero(&padding_right, node, width_of_containing_block)
    item.used_values.padding_top = css_length_percentage_or_auto_to_px_or_zero(&padding_top, node, width_of_containing_block)
    item.used_values.padding_bottom = css_length_percentage_or_auto_to_px_or_zero(&padding_bottom, node, width_of_containing_block)

    if layout_flex_formatting_context_main_axis_is_horizontal(this):
        item.borders.main_before = css_computed_values_border_left(computed_values).width
        item.borders.main_after = css_computed_values_border_right(computed_values).width
        item.borders.cross_before = css_computed_values_border_top(computed_values).width
        item.borders.cross_after = css_computed_values_border_bottom(computed_values).width

        item.padding.main_before = css_length_percentage_or_auto_to_px_or_zero(&padding_left, node, width_of_containing_block)
        item.padding.main_after = css_length_percentage_or_auto_to_px_or_zero(&padding_right, node, width_of_containing_block)
        item.padding.cross_before = css_length_percentage_or_auto_to_px_or_zero(&padding_top, node, width_of_containing_block)
        item.padding.cross_after = css_length_percentage_or_auto_to_px_or_zero(&padding_bottom, node, width_of_containing_block)

        item.margins.main_before = css_length_percentage_or_auto_to_px_or_zero(&margin_left, node, width_of_containing_block)
        item.margins.main_after = css_length_percentage_or_auto_to_px_or_zero(&margin_right, node, width_of_containing_block)
        item.margins.cross_before = css_length_percentage_or_auto_to_px_or_zero(&margin_top, node, width_of_containing_block)
        item.margins.cross_after = css_length_percentage_or_auto_to_px_or_zero(&margin_bottom, node, width_of_containing_block)

        item.margins.main_before_is_auto = css_length_percentage_or_auto_is_auto(&margin_left)
        item.margins.main_after_is_auto = css_length_percentage_or_auto_is_auto(&margin_right)
        item.margins.cross_before_is_auto = css_length_percentage_or_auto_is_auto(&margin_top)
        item.margins.cross_after_is_auto = css_length_percentage_or_auto_is_auto(&margin_bottom)
    else:
        item.borders.main_before = css_computed_values_border_top(computed_values).width
        item.borders.main_after = css_computed_values_border_bottom(computed_values).width
        item.borders.cross_before = css_computed_values_border_left(computed_values).width
        item.borders.cross_after = css_computed_values_border_right(computed_values).width

        item.padding.main_before = item.used_values.padding_top
        item.padding.main_after = item.used_values.padding_bottom
        item.padding.cross_before = item.used_values.padding_left
        item.padding.cross_after = item.used_values.padding_right

        item.margins.main_before = css_length_percentage_or_auto_to_px_or_zero(&margin_top, node, width_of_containing_block)
        item.margins.main_after = css_length_percentage_or_auto_to_px_or_zero(&margin_bottom, node, width_of_containing_block)
        item.margins.cross_before = css_length_percentage_or_auto_to_px_or_zero(&margin_left, node, width_of_containing_block)
        item.margins.cross_after = css_length_percentage_or_auto_to_px_or_zero(&margin_right, node, width_of_containing_block)

        item.margins.main_before_is_auto = css_length_percentage_or_auto_is_auto(&margin_top)
        item.margins.main_after_is_auto = css_length_percentage_or_auto_is_auto(&margin_bottom)
        item.margins.cross_before_is_auto = css_length_percentage_or_auto_is_auto(&margin_left)
        item.margins.cross_after_is_auto = css_length_percentage_or_auto_is_auto(&margin_right)