Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / layout / formatting_context_3.lucb

#==============================================================================================
#
#   formatting_context_3 - Layout::FormattingContext: the height of absolutely positioned
#                          non-replaced boxes and their containing block rectangle
#
#   DESCRIPTION:
#       Port of LibWeb/Layout/FormattingContext.cpp (lines 992-1384): the vertical constraint
#       equation of absolutely positioned non-replaced boxes (css-position-3 ยง5.3), whose C++
#       lambdas share their state through LayoutAbsposHeightSolver; the rectangle of an inline
#       element that establishes an abspos containing block (the bounding box of its fragments);
#       and resolve_abspos_containing_block_info.
#
#       Copyright (c) 2020-2022, Andreas Kling <andreas@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: Absolutely positioned heights ===========================================================

## The state compute_height_for_absolutely_positioned_non_replaced_element's lambdas (apply_min_max_height_constraints,
## try_compute_height and its solve_for_* helpers) capture by reference.
struct LayoutAbsposHeightSolver:
    var context: LayoutFormattingContext*
    var box: LayoutBox*
    var available_space: LayoutAvailableSpace
    var before_or_after_inside_layout: LayoutFormattingContextBeforeOrAfterInsideLayout
    var margin_top: CssLengthPercentageOrAuto
    var margin_bottom: CssLengthPercentageOrAuto
    var top: CssLengthPercentageOrAuto
    var bottom: CssLengthPercentageOrAuto
    var width_of_containing_block: gfx.CssPixels
    var height_of_containing_block: gfx.CssPixels
    var state: const LayoutStateUsedValues*
    # try_compute_height's `height`.
    var height: CssLengthOrAuto

## The `apply_min_max_height_constraints` lambda of compute_height_for_absolutely_positioned_non_replaced_element.
func layout_abspos_height_solver_apply_min_max_height_constraints(this: const LayoutAbsposHeightSolver*, unconstrained_height: CssLengthOrAuto) -> CssLengthOrAuto:
    let computed_values = layout_box_computed(this.box)
    let computed_min_height = css_computed_values_min_height(computed_values)
    let computed_max_height = css_computed_values_max_height(computed_values)
    var constrained_height = unconstrained_height
    if not css_size_is_none(&computed_max_height):
        let inner_max_height = layout_formatting_context_calculate_inner_height(this.context, this.box, this.available_space, computed_max_height)
        if inner_max_height.lt(css_length_or_auto_to_px_or_zero(&constrained_height, this.box.layout_node())):
            constrained_height = layout_length_or_auto_px(inner_max_height)
    if not css_size_is_auto(&computed_min_height):
        let inner_min_height = layout_formatting_context_calculate_inner_height(this.context, this.box, this.available_space, computed_min_height)
        if inner_min_height.gt(css_length_or_auto_to_px_or_zero(&constrained_height, this.box.layout_node())):
            constrained_height = layout_length_or_auto_px(inner_min_height)
    return constrained_height

## The `solve_for` lambda of try_compute_height: the value of `length_or_auto`'s term that satisfies the constraint
## equation, clamped to zero when asked.
func layout_abspos_height_solver_solve_for(this: const LayoutAbsposHeightSolver*, length_or_auto: CssLengthOrAuto, clamp_to_zero: bool = false) -> CssLength:
    let node = this.box.layout_node()
    let computed_values = layout_box_computed(this.box)
    let unclamped_value = (this.height_of_containing_block.sub(css_length_percentage_or_auto_to_px_or_zero(&this.top, node, this.height_of_containing_block)).sub(css_length_percentage_or_auto_to_px_or_zero(&this.margin_top, node, this.width_of_containing_block)).sub(css_computed_values_border_top(computed_values).width).sub(this.state.padding_top).sub(css_length_or_auto_to_px_or_zero(&this.height, node)).sub(this.state.padding_bottom).sub(css_computed_values_border_bottom(computed_values).width).sub(css_length_percentage_or_auto_to_px_or_zero(&this.margin_bottom, node, this.width_of_containing_block)).sub(css_length_percentage_or_auto_to_px_or_zero(&this.bottom, node, this.height_of_containing_block)).add(css_length_or_auto_to_px_or_zero(&length_or_auto, node)))
    if clamp_to_zero:
        return css_length_make_px_css_pixels(ak.max(layout_zero_px(), unclamped_value))
    return css_length_make_px_css_pixels(unclamped_value)

## A solved CSS::Length assigned to a LengthPercentageOrAuto.
func layout_length_percentage_or_auto_from_length(length: CssLength) -> CssLengthPercentageOrAuto:
    return css_length_percentage_or_auto_new(css_length_percentage_new_css_length(length))

## The `solve_for_top` lambda of try_compute_height.
func layout_abspos_height_solver_solve_for_top(this: LayoutAbsposHeightSolver*):
    let resolved = css_length_percentage_or_auto_resolved_or_auto(&this.top, this.box.layout_node(), this.height_of_containing_block)
    this.top = layout_length_percentage_or_auto_from_length(layout_abspos_height_solver_solve_for(this, resolved))

## The `solve_for_bottom` lambda of try_compute_height.
func layout_abspos_height_solver_solve_for_bottom(this: LayoutAbsposHeightSolver*):
    let resolved = css_length_percentage_or_auto_resolved_or_auto(&this.bottom, this.box.layout_node(), this.height_of_containing_block)
    this.bottom = layout_length_percentage_or_auto_from_length(layout_abspos_height_solver_solve_for(this, resolved))

## The `solve_for_height` lambda of try_compute_height.
func layout_abspos_height_solver_solve_for_height(this: LayoutAbsposHeightSolver*):
    this.height = css_length_or_auto_new(layout_abspos_height_solver_solve_for(this, this.height, true))

## The `solve_for_margin_top` lambda of try_compute_height.
func layout_abspos_height_solver_solve_for_margin_top(this: LayoutAbsposHeightSolver*):
    let resolved = css_length_percentage_or_auto_resolved_or_auto(&this.margin_top, this.box.layout_node(), this.width_of_containing_block)
    this.margin_top = layout_length_percentage_or_auto_from_length(layout_abspos_height_solver_solve_for(this, resolved))

## The `solve_for_margin_bottom` lambda of try_compute_height.
func layout_abspos_height_solver_solve_for_margin_bottom(this: LayoutAbsposHeightSolver*):
    let resolved = css_length_percentage_or_auto_resolved_or_auto(&this.margin_bottom, this.box.layout_node(), this.width_of_containing_block)
    this.margin_bottom = layout_length_percentage_or_auto_from_length(layout_abspos_height_solver_solve_for(this, resolved))

## The `solve_for_margin_top_and_margin_bottom` lambda of try_compute_height.
func layout_abspos_height_solver_solve_for_margin_top_and_margin_bottom(this: LayoutAbsposHeightSolver*):
    let node = this.box.layout_node()
    let margins = css_length_percentage_or_auto_to_px_or_zero(&this.margin_top, node, this.width_of_containing_block).add(css_length_percentage_or_auto_to_px_or_zero(&this.margin_bottom, node, this.width_of_containing_block))
    let solved = layout_abspos_height_solver_solve_for(this, layout_length_or_auto_px(margins))
    let remainder = css_length_to_px_layout_node(&solved, node)
    this.margin_top = layout_length_percentage_or_auto_px(layout_div_int(remainder, 2))
    this.margin_bottom = layout_length_percentage_or_auto_px(layout_div_int(remainder, 2))

## The `try_compute_height` lambda of compute_height_for_absolutely_positioned_non_replaced_element.
func layout_abspos_height_solver_try_compute_height(this: LayoutAbsposHeightSolver*, initial_height: CssLengthOrAuto) -> CssLengthOrAuto:
    this.height = initial_height

    # Reset values that may have been modified by a previous call (when re-solving for min/max-height).
    let computed_values = layout_box_computed(this.box)
    let margin = css_computed_values_margin(computed_values)
    let inset = css_computed_values_inset(computed_values)
    this.margin_top = css_length_box_top_const(&margin)
    this.margin_bottom = css_length_box_bottom_const(&margin)
    this.top = css_length_box_top_const(&inset)
    this.bottom = css_length_box_bottom_const(&inset)

    # If all three of top, height, and bottom are auto:
    if css_length_percentage_or_auto_is_auto(&this.top) and css_length_or_auto_is_auto(&this.height) and css_length_percentage_or_auto_is_auto(&this.bottom):
        # First set any auto values for margin-top and margin-bottom to 0,
        if css_length_percentage_or_auto_is_auto(&this.margin_top):
            this.margin_top = layout_length_percentage_or_auto_px(layout_zero_px())
        if css_length_percentage_or_auto_is_auto(&this.margin_bottom):
            this.margin_bottom = layout_length_percentage_or_auto_px(layout_zero_px())

        # then set top to the static position,
        # and finally apply rule number three below.

        # NOTE: We actually perform these two steps in the opposite order,
        #       because the static position may depend on the height of the box (due to alignment properties).

        let maybe_height = layout_formatting_context_compute_auto_height_for_absolutely_positioned_element(this.context, this.box, this.available_space, this.before_or_after_inside_layout)
        let found_height = maybe_height else return this.height
        this.height = layout_length_or_auto_px(found_height)

        let constrained_height = layout_abspos_height_solver_apply_min_max_height_constraints(this, this.height)
        layout_state_used_values_set_content_height(layout_state_get_mutable(this.context.m_state, this.box.layout_node_with_style()), css_length_or_auto_to_px_or_zero(&constrained_height, this.box.layout_node()))

        let static_position = layout_state_used_values_static_position(layout_state_get(this.context.m_state, this.box.layout_node_with_style()))
        this.top = layout_length_percentage_or_auto_px(static_position.m_y)

        layout_abspos_height_solver_solve_for_bottom(this)

    # If none of the three are auto:
    elif not css_length_percentage_or_auto_is_auto(&this.top) and not css_length_or_auto_is_auto(&this.height) and not css_length_percentage_or_auto_is_auto(&this.bottom):
        # If both margin-top and margin-bottom are auto,
        if css_length_percentage_or_auto_is_auto(&this.margin_top) and css_length_percentage_or_auto_is_auto(&this.margin_bottom):
            # solve the equation under the extra constraint that the two margins get equal values.
            layout_abspos_height_solver_solve_for_margin_top_and_margin_bottom(this)

        # If one of margin-top or margin-bottom is auto,
        elif css_length_percentage_or_auto_is_auto(&this.margin_top) or css_length_percentage_or_auto_is_auto(&this.margin_bottom):
            # solve the equation for that value.
            if css_length_percentage_or_auto_is_auto(&this.margin_top):
                layout_abspos_height_solver_solve_for_margin_top(this)
            else:
                layout_abspos_height_solver_solve_for_margin_bottom(this)

        # If the values are over-constrained,
        else:
            # ignore the value for bottom and solve for that value.
            layout_abspos_height_solver_solve_for_bottom(this)

    # Otherwise,
    else:
        # set auto values for margin-top and margin-bottom to 0,
        if css_length_percentage_or_auto_is_auto(&this.margin_top):
            this.margin_top = layout_length_percentage_or_auto_px(layout_zero_px())
        if css_length_percentage_or_auto_is_auto(&this.margin_bottom):
            this.margin_bottom = layout_length_percentage_or_auto_px(layout_zero_px())

        # and pick one of the following six rules that apply.

        # 1. If top and height are auto and bottom is not auto,
        if css_length_percentage_or_auto_is_auto(&this.top) and css_length_or_auto_is_auto(&this.height) and not css_length_percentage_or_auto_is_auto(&this.bottom):
            # then the height is based on the Auto heights for block formatting context roots,
            let maybe_height = layout_formatting_context_compute_auto_height_for_absolutely_positioned_element(this.context, this.box, this.available_space, this.before_or_after_inside_layout)
            let found_height = maybe_height else return this.height
            this.height = layout_length_or_auto_px(found_height)

            # and solve for top.
            layout_abspos_height_solver_solve_for_top(this)

        # 2. If top and bottom are auto and height is not auto,
        elif css_length_percentage_or_auto_is_auto(&this.top) and css_length_percentage_or_auto_is_auto(&this.bottom) and not css_length_or_auto_is_auto(&this.height):
            # then set top to the static position,
            let static_position = layout_state_used_values_static_position(layout_state_get(this.context.m_state, this.box.layout_node_with_style()))
            this.top = layout_length_percentage_or_auto_px(static_position.m_y)

            # then solve for bottom.
            layout_abspos_height_solver_solve_for_bottom(this)

        # 3. If height and bottom are auto and top is not auto,
        elif css_length_or_auto_is_auto(&this.height) and css_length_percentage_or_auto_is_auto(&this.bottom) and not css_length_percentage_or_auto_is_auto(&this.top):
            # then the height is based on the Auto heights for block formatting context roots,
            let maybe_height = layout_formatting_context_compute_auto_height_for_absolutely_positioned_element(this.context, this.box, this.available_space, this.before_or_after_inside_layout)
            let found_height = maybe_height else return this.height
            this.height = layout_length_or_auto_px(found_height)

            # and solve for bottom.
            layout_abspos_height_solver_solve_for_bottom(this)

        # 4. If top is auto, height and bottom are not auto,
        elif css_length_percentage_or_auto_is_auto(&this.top) and not css_length_or_auto_is_auto(&this.height) and not css_length_percentage_or_auto_is_auto(&this.bottom):
            # then solve for top.
            layout_abspos_height_solver_solve_for_top(this)

        # 5. If height is auto, top and bottom are not auto,
        elif css_length_or_auto_is_auto(&this.height) and not css_length_percentage_or_auto_is_auto(&this.top) and not css_length_percentage_or_auto_is_auto(&this.bottom):
            # then solve for height.
            layout_abspos_height_solver_solve_for_height(this)

        # 6. If bottom is auto, top and height are not auto,
        elif css_length_percentage_or_auto_is_auto(&this.bottom) and not css_length_percentage_or_auto_is_auto(&this.top) and not css_length_or_auto_is_auto(&this.height):
            # then solve for bottom.
            layout_abspos_height_solver_solve_for_bottom(this)

    return this.height

## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:993 Web::Layout::FormattingContext::compute_height_for_absolutely_positioned_non_replaced_element.
# https://drafts.csswg.org/css-position-3/#abs-non-replaced-height
pub func layout_formatting_context_compute_height_for_absolutely_positioned_non_replaced_element(this: LayoutFormattingContext*, box: LayoutBox*, available_space: LayoutAvailableSpace, before_or_after_inside_layout: LayoutFormattingContextBeforeOrAfterInsideLayout):
    # 5.3. The Height Of Absolutely Positioned, Non-Replaced Elements

    # For absolutely positioned elements, the used values of the vertical dimensions must satisfy this constraint:
    # top + margin-top + border-top-width + padding-top + height + padding-bottom + border-bottom-width + margin-bottom + bottom = height of containing block

    # NOTE: This function is called twice: both before and after inside layout.
    #       In the before pass, if it turns out we need the automatic height of the box, we abort these steps.
    #       This allows the box to retain an indefinite height from the perspective of inside layout.

    let computed_values = layout_box_computed(box)
    let margin = css_computed_values_margin(computed_values)
    let inset = css_computed_values_inset(computed_values)
    var solver = LayoutAbsposHeightSolver(
        context = this,
        box = box,
        available_space = available_space,
        before_or_after_inside_layout = before_or_after_inside_layout,
        margin_top = css_length_box_top_const(&margin),
        margin_bottom = css_length_box_bottom_const(&margin),
        top = css_length_box_top_const(&inset),
        bottom = css_length_box_bottom_const(&inset),
        width_of_containing_block = layout_available_size_to_px_or_zero(&available_space.width),
        height_of_containing_block = layout_available_size_to_px_or_zero(&available_space.height),
        state = layout_state_get(this.m_state, box.layout_node_with_style()),
        height = css_length_or_auto_make_auto(),
    )

    # Compute the height based on box type and CSS properties:
    # https://www.w3.org/TR/css-sizing-3/#box-sizing
    var input_height = css_length_or_auto_make_auto()
    if is_layout_table_wrapper(box):
        input_height = layout_length_or_auto_px(layout_formatting_context_compute_table_box_height_inside_table_wrapper(this, box, available_space))
    elif layout_formatting_context_should_treat_height_as_auto(this, box, available_space):
        input_height = css_length_or_auto_make_auto()
    else:
        input_height = layout_length_or_auto_px(layout_formatting_context_calculate_inner_height(this, box, available_space, css_computed_values_height(computed_values)))
    var used_height = layout_abspos_height_solver_try_compute_height(&solver, input_height)

    # If the tentative used height is greater than 'max-height', the rules above are applied again,
    # but this time using the computed value of 'max-height' as the computed value for 'height'.
    let computed_max_height = css_computed_values_max_height(computed_values)
    if not css_length_or_auto_is_auto(&used_height) and not css_size_is_none(&computed_max_height):
        let max_height = layout_formatting_context_calculate_inner_height(this, box, available_space, computed_max_height)
        if css_length_or_auto_to_px_or_zero(&used_height, box.layout_node()).gt(max_height):
            used_height = layout_abspos_height_solver_try_compute_height(&solver, layout_length_or_auto_px(max_height))

    # If the resulting height is smaller than 'min-height', the rules above are applied again,
    # but this time using the value of 'min-height' as the computed value for 'height'.
    let computed_min_height = css_computed_values_min_height(computed_values)
    if not css_length_or_auto_is_auto(&used_height) and not css_size_is_auto(&computed_min_height):
        let min_height = layout_formatting_context_calculate_inner_height(this, box, available_space, computed_min_height)
        if css_length_or_auto_to_px_or_zero(&used_height, box.layout_node()).lt(min_height):
            used_height = layout_abspos_height_solver_try_compute_height(&solver, layout_length_or_auto_px(min_height))

    # For the before-inside-layout pass where height is still auto, apply min-max as a simple clamp.
    if css_length_or_auto_is_auto(&used_height):
        used_height = layout_abspos_height_solver_apply_min_max_height_constraints(&solver, used_height)

    # NOTE: The following is not directly part of any spec, but this is where we resolve
    #       the final used values for vertical margin/border/padding.

    let box_state = layout_state_get_mutable(this.m_state, box.layout_node_with_style())
    layout_state_used_values_set_content_height(box_state, css_length_or_auto_to_px_or_zero(&used_height, box.layout_node()))

    # do not set calculated insets or margins on the first pass, there will be a second pass
    let computed_height = css_computed_values_height(computed_values)
    if css_size_is_auto(&computed_height) and before_or_after_inside_layout == LayoutFormattingContextBeforeOrAfterInsideLayout.before:
        return
    layout_state_used_values_set_has_definite_height(box_state, true)
    box_state.inset_top = css_length_percentage_or_auto_to_px_or_zero(&solver.top, box.layout_node(), solver.height_of_containing_block)
    box_state.inset_bottom = css_length_percentage_or_auto_to_px_or_zero(&solver.bottom, box.layout_node(), solver.height_of_containing_block)
    box_state.margin_top = css_length_percentage_or_auto_to_px_or_zero(&solver.margin_top, box.layout_node(), solver.width_of_containing_block)
    box_state.margin_bottom = css_length_percentage_or_auto_to_px_or_zero(&solver.margin_bottom, box.layout_node(), solver.width_of_containing_block)

# mark: Abspos containing blocks ================================================================

# FIXME: Containing block handling for absolutely positioned elements needs architectural improvements.
#
#        The CSS specification defines the containing block as a *rectangle*, not a box. For most cases,
#        this rectangle is derived from the padding box of the nearest positioned ancestor Box. However,
#        when the positioned ancestor is an *inline* element (e.g., a <span> with position: relative),
#        the containing block rectangle should be the bounding box of that inline's fragments.
#
#        Currently, Layout::Node::m_containing_block is typed as Layout::Box*, which cannot represent
#        inline elements. The proper fix would be to:
#        1. Separate the concept of "the node that establishes the containing block" from "the containing
#           block rectangle".
#        2. Store a reference to the establishing node (which could be InlineNode or Box).
#        3. Compute the containing block rectangle on demand based on the establishing node's type.
#
#        For now, we use a surgical workaround: when laying out an absolutely positioned element, we check
#        if there's an inline element with position:relative (or other containing-block-establishing
#        properties) between the abspos element and its current containing_block(). If found, we compute
#        the inline's fragment bounding box and use that for sizing and positioning, then adjust the final
#        offset to be relative to the containing_block() Box that the rest of the system expects.

## The state compute_inline_containing_block_rect's `union_rect` and `walk` lambdas capture.
struct LayoutInlineContainingBlockRectWalk:
    var inline_dom_node: DomNode*
    var state: const LayoutState*
    var bounding_rect: gfx.CssPixelRect?

## The `union_rect` lambda of compute_inline_containing_block_rect.
func layout_inline_containing_block_rect_walk_union_rect(this: LayoutInlineContainingBlockRectWalk*, rect: gfx.CssPixelRect):
    if let bounding_rect = this.bounding_rect:
        this.bounding_rect = gfx.css_pixel_rect_united(&bounding_rect, rect)
    else:
        this.bounding_rect = rect

## The recursive `walk` lambda of compute_inline_containing_block_rect.
func layout_inline_containing_block_rect_walk(this: LayoutInlineContainingBlockRectWalk*, node: LayoutNode*, offset: gfx.CssPixelPoint):
    let used_values = layout_state_try_get_layout_node(this.state, node)
    if let values = used_values:
        if not ak.vector_is_empty(&values.line_boxes):
            for line_box in ak.vector_span_const(&values.line_boxes):
                for fragment in ak.vector_span_const(&line_box.m_fragments):
                    let dom = layout_node_dom_node(layout_line_box_fragment_layout_node(&fragment)) else continue
                    if tree_node_is_inclusive_ancestor_of(this.inline_dom_node, dom):
                        let fragment_offset = layout_line_box_fragment_offset(&fragment)
                        layout_inline_containing_block_rect_walk_union_rect(this, gfx.CssPixelRect(gfx.css_pixel_point_add(&fragment_offset, offset), layout_line_box_fragment_size(&fragment)))

    var child_or_none = tree_node_first_child(node)
    while let child = child_or_none:
        child_or_none = tree_node_next_sibling(child)
        if layout_node_is_absolutely_positioned(child) or layout_node_is_floating(child):
            continue
        let child_used_values = layout_state_try_get_layout_node(this.state, child)
        var child_offset = offset
        if let child_values = child_used_values:
            child_offset = gfx.css_pixel_point_add(&offset, child_values.offset)
        if let box_child = as_if_layout_box(child):
            if not layout_node_is_anonymous(box_child.layout_node()):
                let dom = layout_node_dom_node(box_child.layout_node()) else continue
                if not tree_node_is_inclusive_ancestor_of(this.inline_dom_node, dom):
                    continue
                # child_offset addresses the box's content area; the border-box origin sits
                # (border-left + padding-left, border-top + padding-top) before that.
                if let child_values = child_used_values:
                    let border_box_origin = gfx.css_pixel_point_sub(&child_offset, gfx.CssPixelPoint(
                        child_values.border_left.add(child_values.padding_left),
                        child_values.border_top.add(child_values.padding_top),
                    ))
                    layout_inline_containing_block_rect_walk_union_rect(this, gfx.CssPixelRect(border_box_origin, gfx.CssPixelSize(layout_state_used_values_border_box_width(child_values), layout_state_used_values_border_box_height(child_values))))
        layout_inline_containing_block_rect_walk(this, child, child_offset)

## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:1277 Web::Layout::compute_inline_containing_block_rect.
# Computes the bounding box rectangle of an inline node's fragments, in the coordinate
# space of the abspos containing block. Returns the padding-box rect because that's the
# edge containing blocks are formed by.
#
# When an inline element has block-level descendants, the layout tree splits the inline
# into "before"/"middle"/"after" anonymous wrappers; we walk all of them so the rect
# covers the inline's full extent (matching getClientRects() for split inlines).
func layout_compute_inline_containing_block_rect(inline_node: LayoutInlineNode*, abspos_containing_block: LayoutBox*, state: const LayoutState*) -> gfx.CssPixelRect?:
    let inline_dom_node = layout_node_dom_node(inline_node.layout_node()) else return none

    let outer_block = layout_node_non_anonymous_containing_block(inline_node.layout_node()) else return none

    # Walk outer_block's subtree in pre-order, threading the running offset from
    # abspos_containing_block down so we don't have to re-walk to the root for every
    # matching node. We prune subtrees rooted at non-anonymous boxes that don't belong
    # to the inline (sibling content can't contribute to its rect), and skip out-of-flow
    # descendants (they aren't part of the inline's fragments).
    var walk = LayoutInlineContainingBlockRectWalk(inline_dom_node = inline_dom_node, state = state, bounding_rect = none)

    var outer_offset = gfx.CssPixelPoint()
    var ancestor_or_none: LayoutNode*? = outer_block.layout_node()
    while let ancestor = ancestor_or_none:
        if ancestor == abspos_containing_block.layout_node():
            break
        if let used_values = layout_state_try_get_layout_node(state, ancestor):
            gfx.css_pixel_point_translate_by_css_pixel_point(&outer_offset, used_values.offset)
        ancestor_or_none = (LayoutNode*?)layout_node_parent(ancestor)
    layout_inline_containing_block_rect_walk(&walk, outer_block.layout_node(), outer_offset)

    var bounding_rect = walk.bounding_rect else return none

    # Expand the bounding rect by the inline's padding to get the padding box.
    if let inline_used_values = layout_state_try_get_layout_node_with_style(state, inline_node.layout_node_with_style()):
        gfx.css_pixel_rect_inflate_css_pixels_css_pixels_css_pixels(&bounding_rect,
                                                                    inline_used_values.padding_top,
                                                                    inline_used_values.padding_right,
                                                                    inline_used_values.padding_bottom,
                                                                    inline_used_values.padding_left)

    return bounding_rect

## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:1354 Web::Layout::FormattingContext::resolve_abspos_containing_block_info (virtual).
pub func layout_formatting_context_resolve_abspos_containing_block_info_impl(this: LayoutFormattingContext*, box: LayoutBox*) -> LayoutAbsposContainingBlockInfo:
    let computed_values = layout_box_computed(box)
    let inset = css_computed_values_inset(computed_values)
    let inset_left = css_length_box_left_const(&inset)
    let inset_right = css_length_box_right_const(&inset)
    let inset_top = css_length_box_top_const(&inset)
    let inset_bottom = css_length_box_bottom_const(&inset)

    # Per-axis mode: auto+auto insets -> static position, otherwise -> inset from rect
    let horizontal_axis_mode = LayoutAbsposAxisMode.static_position if (css_length_percentage_or_auto_is_auto(&inset_left) and css_length_percentage_or_auto_is_auto(&inset_right)) else LayoutAbsposAxisMode.inset_from_rect
    let vertical_axis_mode = LayoutAbsposAxisMode.static_position if (css_length_percentage_or_auto_is_auto(&inset_top) and css_length_percentage_or_auto_is_auto(&inset_bottom)) else LayoutAbsposAxisMode.inset_from_rect

    # Check if there's an inline element that should be the real containing block.
    if let inline_containing_block = layout_node_inline_containing_block_if_applicable(box.layout_node()):
        if let box_containing_block = layout_node_containing_block(box.layout_node()):
            if let rect = layout_compute_inline_containing_block_rect(inline_containing_block, box_containing_block, this.m_state):
                return LayoutAbsposContainingBlockInfo(rect = rect, horizontal_axis_mode = horizontal_axis_mode, vertical_axis_mode = vertical_axis_mode, horizontal_alignment = none, vertical_alignment = none)

    # Normal case: padding box of the actual containing block.
    let containing_block = layout_node_containing_block(box.layout_node()) else trap("VERIFY: an abspos box without a containing block")
    let containing_block_state = layout_state_get(this.m_state, containing_block.layout_node_with_style())
    let rect = gfx.CssPixelRect(
        gfx.CssPixelPoint(containing_block_state.padding_left.neg(), containing_block_state.padding_top.neg()),
        gfx.CssPixelSize(
            layout_state_used_values_content_width(containing_block_state).add(containing_block_state.padding_left).add(containing_block_state.padding_right),
            layout_state_used_values_content_height(containing_block_state).add(containing_block_state.padding_top).add(containing_block_state.padding_bottom),
        ),
    )
    return LayoutAbsposContainingBlockInfo(rect = rect, horizontal_axis_mode = horizontal_axis_mode, vertical_axis_mode = vertical_axis_mode, horizontal_alignment = none, vertical_alignment = none)