Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / layout / block_formatting_context_2.lucb

#==============================================================================================
#
#   block_formatting_context_2 - Layout::BlockFormattingContext: widths of block-level and
#                                floating boxes, used heights and inline children
#
#   DESCRIPTION:
#       Port of LibWeb/Layout/BlockFormattingContext.cpp (lines 185-644): the width of
#       block-level boxes in normal flow (CSS 2.2 §10.3.3, with min-/max-width), avoiding float
#       intrusions, the shrink-to-fit width of floats (§10.3.5), block-level replaced widths
#       (§10.3.4), used heights when the height is or is not treated as auto (with the html and
#       body quirks), and the layout of a block container's inline children by an
#       InlineFormattingContext. The C++ lambdas of compute_width share their state through
#       LayoutBlockWidthSolver.
#
#       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: Block-level widths ======================================================================

## The state compute_width's `try_compute_width` lambda captures by reference.
struct LayoutBlockWidthSolver:
    var context: LayoutBlockFormattingContext*
    var box: LayoutBox*
    var available_space: LayoutAvailableSpace
    var margin_left: CssLengthOrAuto
    var margin_right: CssLengthOrAuto
    var padding_left: CssLengthOrAuto
    var padding_right: CssLengthOrAuto
    var remaining_width_px: gfx.CssPixels

## The `try_compute_width` lambda of BlockFormattingContext::compute_width.
func layout_block_width_solver_try_compute_width(this: LayoutBlockWidthSolver*, a_width: CssLengthOrAuto) -> CssLengthOrAuto:
    let box = this.box
    let node = box.layout_node()
    let computed_values = layout_box_computed(box)
    let zero_value = css_length_make_px_css_pixels(layout_zero_px())

    var width = a_width
    let margin = css_computed_values_margin(computed_values)
    let computed_margin_left = css_length_box_left_const(&margin)
    let computed_margin_right = css_length_box_right_const(&margin)
    this.margin_left = css_length_percentage_or_auto_resolved_or_auto(&computed_margin_left, node, layout_available_size_to_px_or_zero(&this.available_space.width))
    this.margin_right = css_length_percentage_or_auto_resolved_or_auto(&computed_margin_right, node, layout_available_size_to_px_or_zero(&this.available_space.width))
    var total_px = css_computed_values_border_left(computed_values).width.add(css_computed_values_border_right(computed_values).width)
    for value in [this.margin_left, this.padding_left, width, this.padding_right, this.margin_right]:
        total_px = total_px.add(css_length_or_auto_to_px_or_zero(&value, node))

    if not layout_node_is_inline(node):
        # 10.3.3 Block-level, non-replaced elements in normal flow
        # If 'width' is not 'auto' and 'border-left-width' + 'padding-left' + 'width' + 'padding-right' +
        # 'border-right-width' (plus any of 'margin-left' or 'margin-right' that are not 'auto') is larger than the
        # width of the containing block, then any 'auto' values for 'margin-left' or 'margin-right' are, for the
        # following rules, treated as zero.
        if not css_length_or_auto_is_auto(&width) and total_px.gt(this.remaining_width_px):
            if css_length_or_auto_is_auto(&this.margin_left):
                this.margin_left = css_length_or_auto_new(zero_value)
            if css_length_or_auto_is_auto(&this.margin_right):
                this.margin_right = css_length_or_auto_new(zero_value)

        # 10.3.3 cont'd.
        var underflow_px = this.remaining_width_px.sub(total_px)
        if layout_available_size_is_intrinsic_sizing_constraint(&this.available_space.width):
            underflow_px = layout_zero_px()

        if css_length_or_auto_is_auto(&width):
            if css_length_or_auto_is_auto(&this.margin_left):
                this.margin_left = css_length_or_auto_new(zero_value)
            if css_length_or_auto_is_auto(&this.margin_right):
                this.margin_right = css_length_or_auto_new(zero_value)

            if layout_available_size_is_definite(&this.available_space.width):
                if underflow_px.ge(layout_zero_px()):
                    width = layout_length_or_auto_px(underflow_px)
                else:
                    width = css_length_or_auto_new(zero_value)
            elif layout_available_size_is_min_content(&this.available_space.width):
                if layout_formatting_context_formatting_context_type_created_by_box(box) != none:
                    width = layout_length_or_auto_px(layout_formatting_context_calculate_min_content_width(this.context.layout_formatting_context(), box))
            elif layout_available_size_is_max_content(&this.available_space.width):
                if layout_formatting_context_formatting_context_type_created_by_box(box) != none:
                    width = layout_length_or_auto_px(layout_formatting_context_calculate_max_content_width(this.context.layout_formatting_context(), box))
            else:
                trap("VERIFY_NOT_REACHED")
        else:
            if not css_length_or_auto_is_auto(&this.margin_left) and not css_length_or_auto_is_auto(&this.margin_right):
                this.margin_right = layout_length_or_auto_px(css_length_or_auto_to_px_or_zero(&this.margin_right, node).add(underflow_px))
            elif not css_length_or_auto_is_auto(&this.margin_left) and css_length_or_auto_is_auto(&this.margin_right):
                this.margin_right = layout_length_or_auto_px(underflow_px)
            elif css_length_or_auto_is_auto(&this.margin_left) and not css_length_or_auto_is_auto(&this.margin_right):
                this.margin_left = layout_length_or_auto_px(underflow_px)
            # margin_left.is_auto() && margin_right.is_auto()
            else:
                let half_of_the_underflow = layout_length_or_auto_px(layout_div_int(underflow_px, 2))
                this.margin_left = half_of_the_underflow
                this.margin_right = half_of_the_underflow

    return width

## The `input_width` lambda of BlockFormattingContext::compute_width.
func layout_block_formatting_context_compute_width_input_width(this: LayoutBlockFormattingContext*, box: LayoutBox*, box_state: LayoutStateUsedValues*, available_space: LayoutAvailableSpace, remaining_available_space: LayoutAvailableSpace) -> CssLengthOrAuto:
    let base = this.layout_formatting_context()
    let computed_values = layout_box_computed(box)
    if layout_formatting_context_box_is_sized_as_replaced_element(base, box, available_space):
        # NOTE: Replaced elements had their width calculated independently above.
        #       We use that width as the input here to ensure that margins get resolved.
        return layout_length_or_auto_px(layout_state_used_values_content_width(box_state))
    if is_layout_table_wrapper(box):
        return layout_length_or_auto_px(layout_formatting_context_compute_table_box_width_inside_table_wrapper(base, box, remaining_available_space))

    # https://html.spec.whatwg.org/multipage/rendering.html#button-layout
    # If the computed value of 'inline-size' is 'auto', then the used value is the fit-content inline size.
    let computed_width = css_computed_values_width(computed_values)
    if let dom_node = layout_node_dom_node(box.layout_node()):
        if let html_element = as_if_html_element(dom_node):
            if html_element_uses_button_layout(html_element) and css_size_is_auto(&computed_width):
                return layout_length_or_auto_px(layout_formatting_context_calculate_fit_content_width(base, box, available_space))

    if layout_formatting_context_should_treat_width_as_auto(base, box, available_space):
        return css_length_or_auto_make_auto()
    return layout_length_or_auto_px(layout_formatting_context_calculate_inner_width(base, box, available_space.width, computed_width))

## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.cpp:185 Web::Layout::BlockFormattingContext::compute_width.
pub func layout_block_formatting_context_compute_width(this: LayoutBlockFormattingContext*, box: LayoutBox*, available_space: LayoutAvailableSpace):
    let base = this.layout_formatting_context()
    var remaining_available_space = available_space

    # Certain formatting contexts do not allow float intrusions, so reduce the available space for them.
    if layout_available_size_is_definite(&available_space.width) and layout_block_formatting_context_box_should_avoid_floats_because_it_establishes_fc(this, box):
        let intrusion = layout_block_formatting_context_intrusion_by_floats_into_box_layout_box(this, box, layout_zero_px())
        let remaining_width = layout_available_size_to_px_or_zero(&available_space.width).sub(intrusion.left).sub(intrusion.right)
        remaining_available_space.width = layout_available_size_make_definite(remaining_width)

    if layout_formatting_context_box_is_sized_as_replaced_element(base, box, available_space):
        layout_block_formatting_context_compute_width_for_block_level_replaced_element_in_normal_flow(this, box, available_space)
        if layout_node_is_floating(box.layout_node()):
            # 10.3.6 Floating, replaced elements:
            # https://www.w3.org/TR/CSS22/visudet.html#float-replaced-width
            return

    if layout_node_is_floating(box.layout_node()):
        # 10.3.5 Floating, non-replaced elements:
        # https://www.w3.org/TR/CSS22/visudet.html#float-width
        layout_block_formatting_context_compute_width_for_floating_box(this, box, available_space)
        return

    let computed_values = layout_box_computed(box)
    let available_width_px = layout_available_size_to_px_or_zero(&available_space.width)
    let margin = css_computed_values_margin(computed_values)
    let padding = css_computed_values_padding(computed_values)
    let computed_margin_left = css_length_box_left_const(&margin)
    let computed_margin_right = css_length_box_right_const(&margin)
    let computed_padding_left = css_length_box_left_const(&padding)
    let computed_padding_right = css_length_box_right_const(&padding)
    var solver = LayoutBlockWidthSolver(
        context = this,
        box = box,
        available_space = available_space,
        margin_left = css_length_percentage_or_auto_resolved_or_auto(&computed_margin_left, box.layout_node(), available_width_px),
        margin_right = css_length_percentage_or_auto_resolved_or_auto(&computed_margin_right, box.layout_node(), available_width_px),
        padding_left = css_length_percentage_or_auto_resolved_or_auto(&computed_padding_left, box.layout_node(), available_width_px),
        padding_right = css_length_percentage_or_auto_resolved_or_auto(&computed_padding_right, box.layout_node(), available_width_px),
        remaining_width_px = layout_zero_px(),
    )

    let box_state = layout_state_get_mutable(base.m_state, box.layout_node_with_style())
    box_state.margin_left = css_length_or_auto_to_px_or_zero(&solver.margin_left, box.layout_node())
    box_state.margin_right = css_length_or_auto_to_px_or_zero(&solver.margin_right, box.layout_node())
    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_or_auto_to_px_or_zero(&solver.padding_left, box.layout_node())
    box_state.padding_right = css_length_or_auto_to_px_or_zero(&solver.padding_right, box.layout_node())

    # NOTE: If we are calculating the min-content or max-content width of this box,
    #       and the width should be treated as auto, then we can simply return here,
    #       as the preferred width and min/max constraints are irrelevant for intrinsic sizing.
    if box_state.width_constraint != LayoutSizeConstraint.none_:
        return

    solver.remaining_width_px = layout_available_size_to_px_or_zero(&remaining_available_space.width)

    let input_width = layout_block_formatting_context_compute_width_input_width(this, box, box_state, available_space, remaining_available_space)

    # 1. The tentative used width is calculated (without 'min-width' and 'max-width')
    var used_width = layout_block_width_solver_try_compute_width(&solver, input_width)

    # 2. The tentative used width is greater than 'max-width', the rules above are applied again,
    #    but this time using the computed value of 'max-width' as the computed value for 'width'.
    if not layout_formatting_context_should_treat_max_width_as_none(base, box, available_space.width):
        let max_width = layout_formatting_context_calculate_inner_width(base, box, available_space.width, css_computed_values_max_width(computed_values))
        if css_length_or_auto_to_px_or_zero(&used_width, box.layout_node()).gt(max_width):
            used_width = layout_block_width_solver_try_compute_width(&solver, layout_length_or_auto_px(max_width))

    # 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
    #    but this time using the value of 'min-width' as the computed value for 'width'.
    let computed_min_width = css_computed_values_min_width(computed_values)
    if not css_size_is_auto(&computed_min_width) and not css_length_or_auto_is_auto(&used_width):
        let min_width = layout_formatting_context_calculate_inner_width(base, box, available_space.width, computed_min_width)
        if css_length_or_auto_to_px_or_zero(&used_width, box.layout_node()).lt(min_width):
            used_width = layout_block_width_solver_try_compute_width(&solver, layout_length_or_auto_px(min_width))

    if not layout_formatting_context_box_is_sized_as_replaced_element(base, box, available_space) and not css_length_or_auto_is_auto(&used_width):
        layout_state_used_values_set_content_width(box_state, css_length_or_auto_to_px_or_zero(&used_width, box.layout_node()))

    box_state.margin_left = css_length_or_auto_to_px_or_zero(&solver.margin_left, box.layout_node())
    box_state.margin_right = css_length_or_auto_to_px_or_zero(&solver.margin_right, box.layout_node())

## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.cpp:348 Web::Layout::BlockFormattingContext::avoid_float_intrusions.
pub func layout_block_formatting_context_avoid_float_intrusions(this: LayoutBlockFormattingContext*, box: LayoutBox*, available_space: LayoutAvailableSpace):
    let base = this.layout_formatting_context()
    let computed_width = css_computed_values_width(layout_box_computed(box))
    if css_size_is_auto(&computed_width):
        return
    if not layout_available_size_is_definite(&available_space.width):
        return
    if not layout_block_formatting_context_box_should_avoid_floats_because_it_establishes_fc(this, box):
        return

    # https://drafts.csswg.org/css2/#floats
    # 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.
    let root = layout_block_formatting_context_root(this).layout_box()
    let box_state = layout_state_get_mutable(base.m_state, box.layout_node_with_style())
    while true:
        let box_in_root_rect = layout_formatting_context_margin_box_rect_in_ancestor_coordinate_space_layout_state_used_values(base, box_state, root)
        let space_used_by_floats = layout_block_formatting_context_space_used_and_containing_margin_for_floats(this, gfx.css_pixel_rect_y(&box_in_root_rect))
        let remaining_space = layout_available_size_to_px_or_zero(&available_space.width).sub(space_used_by_floats.left_used_space).sub(space_used_by_floats.right_used_space)
        if layout_state_used_values_border_box_width(box_state).le(remaining_space):
            break

        # The `find_topmost_float` lambda, for the matching left float and then the matching right one.
        var topmost_float_bottom: gfx.CssPixels? = none
        for float_box_or_none in [space_used_by_floats.matching_left_float_box, space_used_by_floats.matching_right_float_box]:
            let float_box = float_box_or_none else continue
            let float_used_values = layout_state_get(base.m_state, float_box.layout_node_with_style())
            let float_in_root_rect = layout_formatting_context_margin_box_rect_in_ancestor_coordinate_space_layout_state_used_values(base, float_used_values, root)
            let float_bottom = gfx.css_pixel_rect_bottom(&float_in_root_rect)
            if topmost_float_bottom == none or (topmost_float_bottom else trap("unreachable")).gt(float_bottom):
                topmost_float_bottom = float_bottom
        let bottom = topmost_float_bottom else break

        layout_state_used_values_set_content_y(box_state, box_state.offset.m_y.add(bottom).sub(gfx.css_pixel_rect_y(&box_in_root_rect)))

# mark: Floating and replaced widths ============================================================

## The `compute_width` lambda of compute_width_for_floating_box.
func layout_block_formatting_context_compute_width_for_floating_box_compute_width(this: LayoutBlockFormattingContext*, box: LayoutBox*, box_state: LayoutStateUsedValues*, available_space: LayoutAvailableSpace, margin_left: gfx.CssPixels, margin_right: gfx.CssPixels, width: CssLengthOrAuto) -> CssLengthOrAuto:
    let computed_values = layout_box_computed(box)
    # If 'width' is computed as 'auto', the used value is the "shrink-to-fit" width.
    if css_length_or_auto_is_auto(&width):
        let result = layout_formatting_context_calculate_shrink_to_fit_widths(this.layout_formatting_context(), box)

        if layout_available_size_is_definite(&available_space.width):
            # Find the available width: in this case, this is the width of the containing
            # block minus the used values of 'margin-left', 'border-left-width', 'padding-left',
            # 'padding-right', 'border-right-width', 'margin-right', and the widths of any relevant scroll bars.
            let available_width = (layout_available_size_to_px_or_zero(&available_space.width).sub(margin_left).sub(css_computed_values_border_left(computed_values).width).sub(box_state.padding_left).sub(box_state.padding_right).sub(css_computed_values_border_right(computed_values).width).sub(margin_right))
            # Then the shrink-to-fit width is: min(max(preferred minimum width, available width), preferred width).
            return layout_length_or_auto_px(ak.min(ak.max(result.preferred_minimum_width, available_width), result.preferred_width))
        if layout_available_size_is_indefinite(&available_space.width) or layout_available_size_is_max_content(&available_space.width):
            # Fold the formula for shrink-to-fit width for indefinite and max-content available width.
            return layout_length_or_auto_px(result.preferred_width)
        # Fold the formula for shrink-to-fit width for min-content available width.
        return layout_length_or_auto_px(ak.min(result.preferred_minimum_width, result.preferred_width))

    return width

## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.cpp:387 Web::Layout::BlockFormattingContext::compute_width_for_floating_box.
pub func layout_block_formatting_context_compute_width_for_floating_box(this: LayoutBlockFormattingContext*, box: LayoutBox*, available_space: LayoutAvailableSpace):
    let base = this.layout_formatting_context()
    # 10.3.5 Floating, non-replaced elements
    let computed_values = layout_box_computed(box)

    let width_of_containing_block = layout_available_size_to_px_or_zero(&available_space.width)

    # If 'margin-left', or 'margin-right' are computed as 'auto', their used value is '0'.
    let margin = css_computed_values_margin(computed_values)
    let padding = css_computed_values_padding(computed_values)
    let computed_margin_left = css_length_box_left_const(&margin)
    let computed_margin_right = css_length_box_right_const(&margin)
    let computed_padding_left = css_length_box_left_const(&padding)
    let computed_padding_right = css_length_box_right_const(&padding)
    let margin_left = css_length_percentage_or_auto_to_px_or_zero(&computed_margin_left, box.layout_node(), width_of_containing_block)
    let margin_right = css_length_percentage_or_auto_to_px_or_zero(&computed_margin_right, box.layout_node(), width_of_containing_block)

    let box_state = layout_state_get_mutable(base.m_state, box.layout_node_with_style())
    box_state.padding_left = css_length_percentage_or_auto_to_px_or_zero(&computed_padding_left, box.layout_node(), width_of_containing_block)
    box_state.padding_right = css_length_percentage_or_auto_to_px_or_zero(&computed_padding_right, box.layout_node(), width_of_containing_block)
    box_state.margin_left = margin_left
    box_state.margin_right = margin_right
    box_state.border_left = css_computed_values_border_left(computed_values).width
    box_state.border_right = css_computed_values_border_right(computed_values).width

    # The `input_width` lambda.
    var input_width = css_length_or_auto_make_auto()
    if not layout_formatting_context_should_treat_width_as_auto(base, box, available_space):
        input_width = layout_length_or_auto_px(layout_formatting_context_calculate_inner_width(base, box, available_space.width, css_computed_values_width(computed_values)))

    # 1. The tentative used width is calculated (without 'min-width' and 'max-width')
    var width = layout_block_formatting_context_compute_width_for_floating_box_compute_width(this, box, box_state, available_space, margin_left, margin_right, input_width)

    # 2. The tentative used width is greater than 'max-width', the rules above are applied again,
    #    but this time using the computed value of 'max-width' as the computed value for 'width'.
    if not layout_formatting_context_should_treat_max_width_as_none(base, box, available_space.width):
        let max_width = layout_formatting_context_calculate_inner_width(base, box, available_space.width, css_computed_values_max_width(computed_values))
        if css_length_or_auto_to_px_or_zero(&width, box.layout_node()).gt(max_width):
            width = layout_block_formatting_context_compute_width_for_floating_box_compute_width(this, box, box_state, available_space, margin_left, margin_right, layout_length_or_auto_px(max_width))

    # 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
    #    but this time using the value of 'min-width' as the computed value for '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, box, available_space.width, computed_min_width)
        if css_length_or_auto_to_px_or_zero(&width, box.layout_node()).lt(min_width):
            width = layout_block_formatting_context_compute_width_for_floating_box_compute_width(this, box, box_state, available_space, margin_left, margin_right, layout_length_or_auto_px(min_width))

    layout_state_used_values_set_content_width(box_state, css_length_or_auto_to_px_or_zero(&width, box.layout_node()))

## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.cpp:460 Web::Layout::BlockFormattingContext::compute_width_for_block_level_replaced_element_in_normal_flow.
pub func layout_block_formatting_context_compute_width_for_block_level_replaced_element_in_normal_flow(this: LayoutBlockFormattingContext*, box: LayoutBox*, available_space: LayoutAvailableSpace):
    let base = this.layout_formatting_context()
    # 10.3.6 Floating, replaced elements
    let computed_values = layout_box_computed(box)

    let width_of_containing_block = layout_available_size_to_px_or_zero(&available_space.width)

    # 10.3.4 Block-level, replaced elements in normal flow
    # The used value of 'width' is determined as for inline replaced elements. Then the rules for
    # non-replaced block-level elements are applied to determine the margins.
    # If 'margin-left', or 'margin-right' are computed as 'auto', their used value is '0'.
    let margin = css_computed_values_margin(computed_values)
    let padding = css_computed_values_padding(computed_values)
    let computed_margin_left = css_length_box_left_const(&margin)
    let computed_margin_right = css_length_box_right_const(&margin)
    let computed_padding_left = css_length_box_left_const(&padding)
    let computed_padding_right = css_length_box_right_const(&padding)
    let margin_left = css_length_percentage_or_auto_to_px_or_zero(&computed_margin_left, box.layout_node(), width_of_containing_block)
    let margin_right = css_length_percentage_or_auto_to_px_or_zero(&computed_margin_right, box.layout_node(), width_of_containing_block)
    let padding_left = css_length_percentage_or_auto_to_px_or_zero(&computed_padding_left, box.layout_node(), width_of_containing_block)
    let padding_right = css_length_percentage_or_auto_to_px_or_zero(&computed_padding_right, box.layout_node(), width_of_containing_block)

    let box_state = layout_state_get_mutable(base.m_state, box.layout_node_with_style())
    let width = layout_formatting_context_compute_width_for_replaced_element(base, box, available_space)
    box_state.margin_left = margin_left
    box_state.margin_right = margin_right
    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 = padding_left
    box_state.padding_right = padding_right
    layout_state_used_values_set_content_width(box_state, layout_formatting_context_calculate_inner_width(base, box, available_space.width, css_size_make_px(width)))

# mark: Used heights ============================================================================

## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.cpp:487 Web::Layout::BlockFormattingContext::resolve_used_height_if_not_treated_as_auto.
pub func layout_block_formatting_context_resolve_used_height_if_not_treated_as_auto(this: LayoutBlockFormattingContext*, box: LayoutBox*, available_space: LayoutAvailableSpace):
    let base = this.layout_formatting_context()
    if layout_formatting_context_should_treat_height_as_auto(base, box, available_space):
        return

    let computed_values = layout_box_computed(box)
    let box_state = layout_state_get_mutable(base.m_state, box.layout_node_with_style())

    var height = layout_formatting_context_calculate_inner_height(base, box, available_space, css_computed_values_height(computed_values))

    if not layout_formatting_context_should_treat_max_height_as_none(base, box, available_space.height):
        let computed_max_height = css_computed_values_max_height(computed_values)
        if not css_size_is_auto(&computed_max_height):
            let max_height = layout_formatting_context_calculate_inner_height(base, box, available_space, computed_max_height)
            height = ak.min(height, max_height)
    let computed_min_height = css_computed_values_min_height(computed_values)
    if not css_size_is_auto(&computed_min_height):
        height = ak.max(height, layout_formatting_context_calculate_inner_height(base, box, available_space, computed_min_height))

    layout_state_used_values_set_content_height(box_state, height)
    layout_state_used_values_set_has_definite_height(box_state, true)

## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.cpp:512 Web::Layout::BlockFormattingContext::resolve_used_height_if_treated_as_auto.
pub func layout_block_formatting_context_resolve_used_height_if_treated_as_auto(this: LayoutBlockFormattingContext*, box: LayoutBox*, available_space: LayoutAvailableSpace, box_formatting_context: LayoutFormattingContext*? = none):
    let base = this.layout_formatting_context()
    if not layout_formatting_context_should_treat_height_as_auto(base, box, available_space):
        return

    let computed_values = layout_box_computed(box)
    let box_state = layout_state_get_mutable(base.m_state, box.layout_node_with_style())

    var height = layout_zero_px()
    if layout_formatting_context_box_is_sized_as_replaced_element(base, box, available_space):
        height = layout_formatting_context_compute_height_for_replaced_element(base, box, available_space)
    else:
        if let context = box_formatting_context:
            height = layout_formatting_context_automatic_content_height(context)
        else:
            height = layout_block_formatting_context_compute_auto_height_for_block_level_element(this, box, layout_state_used_values_available_inner_space_or_constraints_from(layout_state_get(base.m_state, box.layout_node_with_style()), available_space))

    if not layout_formatting_context_should_treat_max_height_as_none(base, box, available_space.height):
        let computed_max_height = css_computed_values_max_height(computed_values)
        if not css_size_is_auto(&computed_max_height):
            let max_height = layout_formatting_context_calculate_inner_height(base, box, available_space, computed_max_height)
            height = ak.min(height, max_height)
    let computed_min_height = css_computed_values_min_height(computed_values)
    if not css_size_is_auto(&computed_min_height):
        height = ak.max(height, layout_formatting_context_calculate_inner_height(base, box, available_space, computed_min_height))

    let document = layout_node_document(box.layout_node())
    let computed_height = css_computed_values_height(computed_values)
    let dom_node = layout_node_dom_node(box.layout_node())
    if dom_document_in_quirks_mode(document) and dom_node != none and dom_node_is_html_html_element(dom_node else trap("unreachable")) and css_size_is_auto(&computed_height):
        # 3.6. The html element fills the viewport quirk
        # https://quirks.spec.whatwg.org/#the-html-element-fills-the-viewport-quirk
        # FIXME: Handle vertical writing mode.

        # 1. Let margins be sum of the used values of the margin-left and margin-right properties of element
        #    if element has a vertical writing mode, otherwise let margins be the sum of the used values of
        #    the margin-top and margin-bottom properties of element.
        let margins = box_state.margin_top.add(box_state.margin_bottom)

        # 2. Let size be the size of the initial containing block in the block flow direction minus margins.
        let containing_block_used_values = layout_state_used_values_containing_block_used_values(box_state) else trap("the html element without its containing block's used values")
        let size = layout_state_used_values_content_height(containing_block_used_values).sub(margins)

        # 3. Return the bigger value of size and the normal border box size the element would have
        #    according to the CSS specification.
        height = ak.max(size, height)

        # NOTE: The height of the root element when affected by this quirk is considered to be definite.
        layout_state_used_values_set_has_definite_height(box_state, true)

    if dom_document_in_quirks_mode(document) and dom_node != none and dom_node_is_html_body_element(dom_node else trap("unreachable")) and css_size_is_auto(&computed_height):
        # 3.7. The body element fills the html element quirk
        # https://quirks.spec.whatwg.org/#the-body-element-fills-the-html-element-quirk
        # FIXME: Handle vertical writing mode.

        # The element body must additionally meet the following conditions:
        # - The computed value of the 'position' property of element is neither 'absolute' nor 'fixed'.
        # - The computed value of the 'float' property of element is 'none'.
        # - Element is not an inline-level element.
        # - Element is not a multi-column spanning element.
        # NON-STANDARD: We don't check column-span since no browser actually excludes it.
        if not layout_node_is_absolutely_positioned(box.layout_node()) and not layout_node_is_floating(box.layout_node()) and not layout_node_is_inline(box.layout_node()):
            # 1. Let margins be sum of the used values of the margin-left and margin-right properties of element
            #    if element has a vertical writing mode, otherwise let margins be the sum of the used values of
            #    the margin-top and margin-bottom properties of element.
            let margins = box_state.margin_top.add(box_state.margin_bottom)

            # 2. Let size be the size of element's parent element's content box in the block flow direction minus margins.
            let containing_block_used_values = layout_state_used_values_containing_block_used_values(box_state) else trap("the body element without its containing block's used values")
            let size = layout_state_used_values_content_height(containing_block_used_values).sub(margins)

            # 3. Return the bigger value of size and the normal border box size the element would have
            #    according to the CSS specification.
            height = ak.max(size, height)

    layout_state_used_values_set_content_height(box_state, height)

# mark: Inline children =========================================================================

## The `should_treat_min_width_as_auto` lambda of layout_inline_children.
func layout_block_formatting_context_should_treat_min_width_as_auto(block_container: LayoutBlockContainer*, available_space: LayoutAvailableSpace) -> bool:
    let available_width = available_space.width
    let min_width = css_computed_values_min_width(layout_box_computed(block_container.layout_box()))
    if css_size_is_auto(&min_width):
        return true
    if css_size_is_fit_content(&min_width) and layout_available_size_is_intrinsic_sizing_constraint(&available_width):
        return true
    if css_size_is_max_content(&min_width) and layout_available_size_is_max_content(&available_width):
        return true
    if css_size_is_min_content(&min_width) and layout_available_size_is_min_content(&available_width):
        return true
    return false

## Ported from Libraries/LibWeb/Layout/BlockFormattingContext.cpp:598 Web::Layout::BlockFormattingContext::layout_inline_children.
pub func layout_block_formatting_context_layout_inline_children(this: LayoutBlockFormattingContext*, block_container: LayoutBlockContainer*, available_space: LayoutAvailableSpace):
    let base = this.layout_formatting_context()
    assert(layout_node_children_are_inline(block_container.layout_node()))

    let block_container_state = layout_state_get_mutable(base.m_state, block_container.layout_node_with_style())

    var context: LayoutInlineFormattingContext = ---
    layout_inline_formatting_context_construct(&context, base.m_state, base.m_layout_mode, block_container, block_container_state, this)
    layout_formatting_context_run(context.layout_formatting_context(), available_space)

    if not layout_state_used_values_has_definite_width(block_container_state):
        # NOTE: min-width or max-width for boxes with inline children can only be applied after inside layout
        #       is done and width of box content is known
        var used_width_px = layout_formatting_context_automatic_content_width(context.layout_formatting_context())
        # https://www.w3.org/TR/css-sizing-3/#sizing-values
        # Percentages are resolved against the width/height, as appropriate, of the box’s containing block.
        var containing_block_width = layout_zero_px()
        let containing_block = layout_node_containing_block(block_container.layout_node()) else trap("a block container without a containing block")
        if let containing_block_used_values = layout_state_try_get_layout_node_with_style(base.m_state, containing_block.layout_node_with_style()):
            containing_block_width = layout_state_used_values_content_width(containing_block_used_values)
        let available_width = layout_available_size_make_definite(containing_block_width)
        if not layout_formatting_context_should_treat_max_width_as_none(base, block_container.layout_box(), available_space.width):
            let max_width_px = layout_formatting_context_calculate_inner_width(base, block_container.layout_box(), available_width, css_computed_values_max_width(layout_box_computed(block_container.layout_box())))
            if used_width_px.gt(max_width_px):
                used_width_px = max_width_px

        let should_treat_min_width_as_auto = layout_block_formatting_context_should_treat_min_width_as_auto(block_container, available_space)
        if not should_treat_min_width_as_auto:
            let min_width_px = layout_formatting_context_calculate_inner_width(base, block_container.layout_box(), available_width, css_computed_values_min_width(layout_box_computed(block_container.layout_box())))
            if used_width_px.lt(min_width_px):
                used_width_px = min_width_px
        layout_state_used_values_set_content_width(block_container_state, used_width_px)
        layout_state_used_values_set_content_height(block_container_state, layout_formatting_context_automatic_content_height(context.layout_formatting_context()))