Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / layout / formatting_context_2.lucb

#==============================================================================================
#
#   formatting_context_2 - Layout::FormattingContext: replaced element sizes and the widths of
#                          absolutely positioned boxes
#
#   DESCRIPTION:
#       Port of LibWeb/Layout/FormattingContext.cpp (lines 544-990): the tentative and used
#       width and height of replaced elements (CSS 2.2 §10.3.2, §10.6.2), and the horizontal
#       constraint equation of absolutely positioned non-replaced (§10.3.7) and replaced
#       (§10.3.8) boxes. The C++ lambdas that solve the constraint equation share their state
#       through LayoutAbsposWidthSolver.
#
#       Copyright (c) 2020-2022, Andreas Kling <andreas@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: Length helpers ==========================================================================

## `CSS::Length::make_px(px)` as a LengthOrAuto (the implicit conversion).
func layout_length_or_auto_px(px: gfx.CssPixels) -> CssLengthOrAuto:
    return css_length_or_auto_new(css_length_make_px_css_pixels(px))

## `CSS::Length::make_px(px)` as a LengthPercentageOrAuto (the implicit conversions).
func layout_length_percentage_or_auto_px(px: gfx.CssPixels) -> CssLengthPercentageOrAuto:
    return css_length_percentage_or_auto_new(css_length_percentage_new_css_length(css_length_make_px_css_pixels(px)))

## `box.computed_values()` of a layout box.
func layout_box_computed(box: LayoutBox*) -> const CssComputedValues*:
    return &layout_node_computed_values(box.layout_node()).computed_values

# mark: Replaced elements =======================================================================

## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:545 Web::Layout::FormattingContext::tentative_width_for_replaced_element.
# 10.3.2 Inline, replaced elements, https://www.w3.org/TR/CSS22/visudet.html#inline-replaced-width
pub func layout_formatting_context_tentative_width_for_replaced_element(this: LayoutFormattingContext*, box: LayoutBox*, computed_width: CssSize, available_space: LayoutAvailableSpace) -> gfx.CssPixels:
    # Treat percentages of indefinite containing block widths as 0 (the initial width).
    if css_size_is_percentage(&computed_width):
        let containing_block = layout_node_containing_block(box.layout_node()) else trap("a replaced box without a containing block")
        if not layout_state_used_values_has_definite_width(layout_state_get(this.m_state, containing_block.layout_node_with_style())):
            return layout_zero_px()

    let computed_height = css_size_make_auto() if layout_formatting_context_should_treat_height_as_auto(this, box, available_space) else css_computed_values_height(layout_box_computed(box))

    var used_width = layout_zero_px()
    if css_size_is_auto(&computed_width):
        used_width = css_size_to_px(&computed_width, box.layout_node(), layout_available_size_to_px_or_zero(&available_space.width))
    else:
        used_width = layout_formatting_context_calculate_inner_width(this, box, available_space.width, computed_width)

    # If 'height' and 'width' both have computed values of 'auto' and the element also has an intrinsic width,
    # then that intrinsic width is the used value of 'width'.
    let intrinsic = layout_box_auto_content_box_size(box)
    if css_size_is_auto(&computed_height) and css_size_is_auto(&computed_width) and css_size_with_aspect_ratio_has_width(&intrinsic):
        return intrinsic.width else trap("unreachable: the intrinsic width exists")

    # If 'height' and 'width' both have computed values of 'auto' and the element has no intrinsic width,
    # but does have an intrinsic height and intrinsic ratio;
    # or if 'width' has a computed value of 'auto',
    # 'height' has some other computed value, and the element does have an intrinsic ratio; then the used value of 'width' is:
    #
    #     (used height) * (intrinsic ratio)
    if (css_size_is_auto(&computed_height) and css_size_is_auto(&computed_width) and not css_size_with_aspect_ratio_has_width(&intrinsic) and css_size_with_aspect_ratio_has_height(&intrinsic) and layout_box_has_preferred_aspect_ratio(box)) or (css_size_is_auto(&computed_width) and not css_size_is_auto(&computed_height) and layout_box_has_preferred_aspect_ratio(box)):
        let aspect_ratio = layout_box_preferred_aspect_ratio(box) else trap("unreachable: the box has a preferred aspect ratio")
        return layout_formatting_context_compute_height_for_replaced_element(this, box, available_space).mul_fraction(aspect_ratio)

    # If 'height' and 'width' both have computed values of 'auto' and the element has an intrinsic ratio but no intrinsic height or width,
    # then the used value of 'width' is undefined in CSS 2.2. However, it is suggested that, if the containing block's width does not itself
    # depend on the replaced element's width, then the used value of 'width' is calculated from the constraint equation used for block-level,
    # non-replaced elements in normal flow.
    if css_size_is_auto(&computed_height) and css_size_is_auto(&computed_width) and not css_size_with_aspect_ratio_has_width(&intrinsic) and not css_size_with_aspect_ratio_has_height(&intrinsic) and layout_box_has_preferred_aspect_ratio(box):
        return layout_formatting_context_calculate_stretch_fit_width(this, box, available_space.width)

    # Otherwise, if 'width' has a computed value of 'auto', and the element has an intrinsic width, then that intrinsic width is the used value of 'width'.
    if css_size_is_auto(&computed_width) and css_size_with_aspect_ratio_has_width(&intrinsic):
        return intrinsic.width else trap("unreachable: the intrinsic width exists")

    # Otherwise, if 'width' has a computed value of 'auto', but none of the conditions above are met, then the used value of 'width' becomes 300px.
    # If 300px is too wide to fit the device, UAs should use the width of the largest rectangle that has a 2:1 ratio and fits the device instead.
    if css_size_is_auto(&computed_width):
        return gfx.CssPixels.from_int(300)

    return used_width

## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:597 Web::Layout::FormattingContext::compute_width_for_absolutely_positioned_element.
pub func layout_formatting_context_compute_width_for_absolutely_positioned_element(this: LayoutFormattingContext*, box: LayoutBox*, available_space: LayoutAvailableSpace):
    if layout_formatting_context_box_is_sized_as_replaced_element(this, box, available_space):
        layout_formatting_context_compute_width_for_absolutely_positioned_replaced_element(this, box, available_space)
    else:
        layout_formatting_context_compute_width_for_absolutely_positioned_non_replaced_element(this, box, available_space)

## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:605 Web::Layout::FormattingContext::compute_height_for_absolutely_positioned_element.
pub func layout_formatting_context_compute_height_for_absolutely_positioned_element(this: LayoutFormattingContext*, box: LayoutBox*, available_space: LayoutAvailableSpace, before_or_after_inside_layout: LayoutFormattingContextBeforeOrAfterInsideLayout):
    if layout_formatting_context_box_is_sized_as_replaced_element(this, box, available_space):
        layout_formatting_context_compute_height_for_absolutely_positioned_replaced_element(this, box, available_space, before_or_after_inside_layout)
    else:
        layout_formatting_context_compute_height_for_absolutely_positioned_non_replaced_element(this, box, available_space, before_or_after_inside_layout)

## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:613 Web::Layout::FormattingContext::compute_width_for_replaced_element.
pub func layout_formatting_context_compute_width_for_replaced_element(this: LayoutFormattingContext*, box: LayoutBox*, available_space: LayoutAvailableSpace) -> gfx.CssPixels:
    # 10.3.4 Block-level, replaced elements in normal flow...
    # 10.3.2 Inline, replaced elements

    let computed_width = css_size_make_auto() if layout_formatting_context_should_treat_width_as_auto(this, box, available_space) else css_computed_values_width(layout_box_computed(box))
    let computed_height = css_size_make_auto() if layout_formatting_context_should_treat_height_as_auto(this, box, available_space) else css_computed_values_height(layout_box_computed(box))

    # 1. The tentative used width is calculated (without 'min-width' and 'max-width')
    var used_width = layout_formatting_context_tentative_width_for_replaced_element(this, box, computed_width, available_space)

    if css_size_is_auto(&computed_width) and css_size_is_auto(&computed_height) and layout_box_has_preferred_aspect_ratio(box):
        let w = used_width
        let h = layout_formatting_context_tentative_height_for_replaced_element(this, box, computed_height, available_space)
        used_width = layout_formatting_context_solve_replaced_size_constraint(this, w, h, box, available_space).m_width

    # 2. If 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(this, box, available_space.width):
        let computed_max_width = css_computed_values_max_width(layout_box_computed(box))
        let max_width = layout_formatting_context_calculate_inner_width(this, box, available_space.width, computed_max_width)
        if used_width.gt(max_width):
            used_width = layout_formatting_context_tentative_width_for_replaced_element(this, box, computed_max_width, available_space)

    # 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(layout_box_computed(box))
    if not css_size_is_auto(&computed_min_width):
        let min_width = layout_formatting_context_calculate_inner_width(this, box, available_space.width, computed_min_width)
        if used_width.lt(min_width):
            used_width = layout_formatting_context_tentative_width_for_replaced_element(this, box, computed_min_width, available_space)

    return used_width

## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:653 Web::Layout::FormattingContext::tentative_height_for_replaced_element.
# 10.6.2 Inline replaced elements, block-level replaced elements in normal flow, 'inline-block' replaced elements in normal flow and floating replaced elements
# https://www.w3.org/TR/CSS22/visudet.html#inline-replaced-height
pub func layout_formatting_context_tentative_height_for_replaced_element(this: LayoutFormattingContext*, box: LayoutBox*, computed_height: CssSize, available_space: LayoutAvailableSpace) -> gfx.CssPixels:
    let intrinsic = layout_box_auto_content_box_size(box)
    # If 'height' and 'width' both have computed values of 'auto' and the element also has
    # an intrinsic height, then that intrinsic height is the used value of 'height'.
    if layout_formatting_context_should_treat_width_as_auto(this, box, available_space) and layout_formatting_context_should_treat_height_as_auto(this, box, available_space) and css_size_with_aspect_ratio_has_height(&intrinsic):
        return intrinsic.height else trap("unreachable: the intrinsic height exists")

    # Otherwise, if 'height' has a computed value of 'auto', and the element has an intrinsic ratio then the used value of 'height' is:
    #
    #     (used width) / (intrinsic ratio)
    if css_size_is_auto(&computed_height) and layout_box_has_preferred_aspect_ratio(box):
        let aspect_ratio = layout_box_preferred_aspect_ratio(box) else trap("unreachable: the box has a preferred aspect ratio")
        return layout_state_used_values_content_width(layout_state_get(this.m_state, box.layout_node_with_style())).div_fraction(aspect_ratio)

    # Otherwise, if 'height' has a computed value of 'auto', and the element has an intrinsic height, then that intrinsic height is the used value of 'height'.
    if css_size_is_auto(&computed_height) and css_size_with_aspect_ratio_has_height(&intrinsic):
        return intrinsic.height else trap("unreachable: the intrinsic height exists")

    # Otherwise, if 'height' has a computed value of 'auto', but none of the conditions above are met,
    # then the used value of 'height' must be set to the height of the largest rectangle that has a 2:1 ratio, has a height not greater than 150px,
    # and has a width not greater than the device width.
    if css_size_is_auto(&computed_height):
        return gfx.CssPixels.from_int(150)

    # FIXME: Handle cases when available_space is not definite.
    return layout_formatting_context_calculate_inner_height(this, box, available_space, computed_height)

## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:681 Web::Layout::FormattingContext::compute_height_for_replaced_element.
pub func layout_formatting_context_compute_height_for_replaced_element(this: LayoutFormattingContext*, box: LayoutBox*, available_space: LayoutAvailableSpace) -> gfx.CssPixels:
    # 10.6.2 Inline replaced elements
    # 10.6.4 Block-level replaced elements in normal flow
    # 10.6.6 Floating replaced elements
    # 10.6.10 'inline-block' replaced elements in normal flow

    let computed_width = css_size_make_auto() if layout_formatting_context_should_treat_width_as_auto(this, box, available_space) else css_computed_values_width(layout_box_computed(box))
    let computed_height = css_size_make_auto() if layout_formatting_context_should_treat_height_as_auto(this, box, available_space) else css_computed_values_height(layout_box_computed(box))

    # 1. The tentative used height is calculated (without 'min-height' and 'max-height')
    var used_height = layout_formatting_context_tentative_height_for_replaced_element(this, box, computed_height, available_space)

    # However, for replaced elements with both 'width' and 'height' computed as 'auto',
    # use the algorithm under 'Minimum and maximum widths'
    # https://www.w3.org/TR/CSS22/visudet.html#min-max-widths
    # to find the used width and height.
    if css_size_is_auto(&computed_width) and css_size_is_auto(&computed_height) and layout_box_has_preferred_aspect_ratio(box):
        # NOTE: This is a special case where calling tentative_width_for_replaced_element() would call us right back,
        #       and we'd end up in an infinite loop. So we need to handle this case separately.
        let intrinsic = layout_box_auto_content_box_size(box)
        if css_size_with_aspect_ratio_has_width(&intrinsic) or not css_size_with_aspect_ratio_has_height(&intrinsic):
            let w = layout_formatting_context_tentative_width_for_replaced_element(this, box, computed_width, available_space)
            let h = used_height
            used_height = layout_formatting_context_solve_replaced_size_constraint(this, w, h, box, available_space).m_height
    # 2. If this tentative height is greater than 'max-height', the rules above are applied again,
    #    but this time using the value of 'max-height' as the computed value for 'height'.
    if not layout_formatting_context_should_treat_max_height_as_none(this, box, available_space.height):
        let computed_max_height = css_computed_values_max_height(layout_box_computed(box))
        let max_height = layout_formatting_context_calculate_inner_height(this, box, available_space, computed_max_height)
        if used_height.gt(max_height):
            used_height = layout_formatting_context_tentative_height_for_replaced_element(this, box, computed_max_height, available_space)

    # 3. 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(layout_box_computed(box))
    if 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 used_height.lt(min_height):
            used_height = layout_formatting_context_tentative_height_for_replaced_element(this, box, computed_min_height, available_space)

    return used_height

# mark: Absolutely positioned widths ============================================================

## The state compute_width_for_absolutely_positioned_non_replaced_element's lambdas (try_compute_width and its
## solve_for_left / solve_for_width / solve_for_right) capture by reference.
struct LayoutAbsposWidthSolver:
    var context: LayoutFormattingContext*
    var box: LayoutBox*
    var width_of_containing_block: gfx.CssPixels
    var margin_left: CssLengthOrAuto
    var margin_right: CssLengthOrAuto
    var border_left: gfx.CssPixels
    var border_right: gfx.CssPixels
    var padding_left: gfx.CssPixels
    var padding_right: gfx.CssPixels
    var computed_left: CssLengthPercentageOrAuto
    var computed_right: CssLengthPercentageOrAuto
    var left: gfx.CssPixels
    var right: gfx.CssPixels
    # try_compute_width's `width`.
    var width: CssLengthOrAuto

## The `solve_for_left` lambda of try_compute_width.
func layout_abspos_width_solver_solve_for_left(this: const LayoutAbsposWidthSolver*) -> gfx.CssPixels:
    let node = this.box.layout_node()
    return (this.width_of_containing_block.sub(css_length_or_auto_to_px_or_zero(&this.margin_left, node)).sub(this.border_left).sub(this.padding_left).sub(css_length_or_auto_to_px_or_zero(&this.width, node)).sub(this.padding_right).sub(this.border_right).sub(css_length_or_auto_to_px_or_zero(&this.margin_right, node)).sub(this.right))

## The `solve_for_width` lambda of try_compute_width.
func layout_abspos_width_solver_solve_for_width(this: const LayoutAbsposWidthSolver*) -> CssLength:
    let node = this.box.layout_node()
    return css_length_make_px_css_pixels(ak.max(layout_zero_px(), this.width_of_containing_block.sub(this.left).sub(css_length_or_auto_to_px_or_zero(&this.margin_left, node)).sub(this.border_left).sub(this.padding_left).sub(this.padding_right).sub(this.border_right).sub(css_length_or_auto_to_px_or_zero(&this.margin_right, node)).sub(this.right)))

## The `solve_for_right` lambda of try_compute_width.
func layout_abspos_width_solver_solve_for_right(this: const LayoutAbsposWidthSolver*) -> gfx.CssPixels:
    let node = this.box.layout_node()
    return (this.width_of_containing_block.sub(this.left).sub(css_length_or_auto_to_px_or_zero(&this.margin_left, node)).sub(this.border_left).sub(this.padding_left).sub(css_length_or_auto_to_px_or_zero(&this.width, node)).sub(this.padding_right).sub(this.border_right).sub(css_length_or_auto_to_px_or_zero(&this.margin_right, node)))

## The shrink-to-fit width `min(max(preferred minimum width, available width), preferred width)` of try_compute_width.
func layout_abspos_width_solver_shrink_to_fit(this: const LayoutAbsposWidthSolver*) -> gfx.CssPixels:
    let result = layout_formatting_context_calculate_shrink_to_fit_widths(this.context, this.box)
    let available_width = layout_abspos_width_solver_solve_for_width(this)
    return ak.min(ak.max(result.preferred_minimum_width, css_length_to_px_layout_node(&available_width, this.box.layout_node())), result.preferred_width)

## The `try_compute_width` lambda of compute_width_for_absolutely_positioned_non_replaced_element.
func layout_abspos_width_solver_try_compute_width(this: LayoutAbsposWidthSolver*, a_width: CssLengthOrAuto) -> CssLengthOrAuto:
    let node = this.box.layout_node()
    let margin = css_computed_values_margin(layout_box_computed(this.box))
    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, this.width_of_containing_block)
    this.margin_right = css_length_percentage_or_auto_resolved_or_auto(&computed_margin_right, node, this.width_of_containing_block)

    this.width = a_width

    let computed_left_is_auto = css_length_percentage_or_auto_is_auto(&this.computed_left)
    let computed_right_is_auto = css_length_percentage_or_auto_is_auto(&this.computed_right)

    # If all three of 'left', 'width', and 'right' are 'auto':
    if computed_left_is_auto and css_length_or_auto_is_auto(&this.width) and computed_right_is_auto:
        # First set any 'auto' values for 'margin-left' and 'margin-right' to 0.
        if css_length_or_auto_is_auto(&this.margin_left):
            this.margin_left = layout_length_or_auto_px(layout_zero_px())
        if css_length_or_auto_is_auto(&this.margin_right):
            this.margin_right = layout_length_or_auto_px(layout_zero_px())
        # Then, if the 'direction' property of the element establishing the static-position containing block
        # is 'ltr' set 'left' to the static position and apply rule number three below;
        # otherwise, set 'right' to the static position and apply rule number one below.

        # NOTE: As with compute_height_for_absolutely_positioned_non_replaced_element, we actually apply these
        #       steps in the opposite order since the static position may depend on the width of the box.

        let content_width = layout_abspos_width_solver_shrink_to_fit(this)
        this.width = layout_length_or_auto_px(content_width)
        layout_state_used_values_set_content_width(layout_state_get_mutable(this.context.m_state, this.box.layout_node_with_style()), content_width)

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

        this.left = static_position.m_x
        this.right = layout_abspos_width_solver_solve_for_right(this)

    # If none of the three is auto:
    if not computed_left_is_auto and not css_length_or_auto_is_auto(&this.width) and not computed_right_is_auto:
        # If both margin-left and margin-right are auto,
        # solve the equation under the extra constraint that the two margins get equal values
        # FIXME: unless this would make them negative, in which case when direction of the containing block is ltr (rtl), set margin-left (margin-right) to 0 and solve for margin-right (margin-left).
        let size_available_for_margins = (this.width_of_containing_block.sub(this.border_left).sub(this.padding_left).sub(css_length_or_auto_to_px_or_zero(&this.width, node)).sub(this.padding_right).sub(this.border_right).sub(this.left).sub(this.right))
        if css_length_or_auto_is_auto(&this.margin_left) and css_length_or_auto_is_auto(&this.margin_right):
            this.margin_left = layout_length_or_auto_px(layout_div_int(size_available_for_margins, 2))
            this.margin_right = layout_length_or_auto_px(layout_div_int(size_available_for_margins, 2))
            return this.width

        # If one of margin-left or margin-right is auto, solve the equation for that value.
        if css_length_or_auto_is_auto(&this.margin_left):
            this.margin_left = layout_length_or_auto_px(size_available_for_margins)
            return this.width
        if css_length_or_auto_is_auto(&this.margin_right):
            this.margin_right = layout_length_or_auto_px(size_available_for_margins)
            return this.width
        # If the values are over-constrained, ignore the value for left
        # (in case the direction property of the containing block is rtl)
        # or right (in case direction is ltr) and solve for that value.

        # NOTE: At this point we *are* over-constrained since none of margin-left, left, width, right, or margin-right are auto.
        # FIXME: Check direction.
        this.right = layout_abspos_width_solver_solve_for_right(this)
        return this.width

    if css_length_or_auto_is_auto(&this.margin_left):
        this.margin_left = layout_length_or_auto_px(layout_zero_px())
    if css_length_or_auto_is_auto(&this.margin_right):
        this.margin_right = layout_length_or_auto_px(layout_zero_px())

    # 1. 'left' and 'width' are 'auto' and 'right' is not 'auto',
    #    then the width is shrink-to-fit. Then solve for 'left'
    if computed_left_is_auto and css_length_or_auto_is_auto(&this.width) and not computed_right_is_auto:
        this.width = layout_length_or_auto_px(layout_abspos_width_solver_shrink_to_fit(this))
        this.left = layout_abspos_width_solver_solve_for_left(this)

    # 2. 'left' and 'right' are 'auto' and 'width' is not 'auto',
    #    then if the 'direction' property of the element establishing
    #    the static-position containing block is 'ltr' set 'left'
    #    to the static position, otherwise set 'right' to the static position.
    #    Then solve for 'left' (if 'direction is 'rtl') or 'right' (if 'direction' is 'ltr').
    elif computed_left_is_auto and computed_right_is_auto and not css_length_or_auto_is_auto(&this.width):
        # FIXME: Check direction
        let static_position = layout_state_used_values_static_position(layout_state_get(this.context.m_state, this.box.layout_node_with_style()))
        this.left = static_position.m_x
        this.right = layout_abspos_width_solver_solve_for_right(this)

    # 3. 'width' and 'right' are 'auto' and 'left' is not 'auto',
    #    then the width is shrink-to-fit. Then solve for 'right'
    elif css_length_or_auto_is_auto(&this.width) and computed_right_is_auto and not computed_left_is_auto:
        this.width = layout_length_or_auto_px(layout_abspos_width_solver_shrink_to_fit(this))
        this.right = layout_abspos_width_solver_solve_for_right(this)

    # 4. 'left' is 'auto', 'width' and 'right' are not 'auto', then solve for 'left'
    elif computed_left_is_auto and not css_length_or_auto_is_auto(&this.width) and not computed_right_is_auto:
        this.left = layout_abspos_width_solver_solve_for_left(this)

    # 5. 'width' is 'auto', 'left' and 'right' are not 'auto', then solve for 'width'
    elif css_length_or_auto_is_auto(&this.width) and not computed_left_is_auto and not computed_right_is_auto:
        this.width = css_length_or_auto_new(layout_abspos_width_solver_solve_for_width(this))

    # 6. 'right' is 'auto', 'left' and 'width' are not 'auto', then solve for 'right'
    elif computed_right_is_auto and not computed_left_is_auto and not css_length_or_auto_is_auto(&this.width):
        this.right = layout_abspos_width_solver_solve_for_right(this)

    return this.width

## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:728 Web::Layout::FormattingContext::compute_width_for_absolutely_positioned_non_replaced_element.
pub func layout_formatting_context_compute_width_for_absolutely_positioned_non_replaced_element(this: LayoutFormattingContext*, box: LayoutBox*, available_space: LayoutAvailableSpace):
    let width_of_containing_block = layout_available_size_to_px_or_zero(&available_space.width)
    let computed_values = layout_box_computed(box)
    let box_state = layout_state_get_mutable(this.m_state, box.layout_node_with_style())

    let inset = css_computed_values_inset(computed_values)
    let computed_left = css_length_box_left_const(&inset)
    let computed_right = css_length_box_right_const(&inset)
    var solver = LayoutAbsposWidthSolver(
        context = this,
        box = box,
        width_of_containing_block = width_of_containing_block,
        margin_left = css_length_or_auto_make_auto(),
        margin_right = css_length_or_auto_make_auto(),
        border_left = css_computed_values_border_left(computed_values).width,
        border_right = css_computed_values_border_right(computed_values).width,
        padding_left = box_state.padding_left,
        padding_right = box_state.padding_right,
        computed_left = computed_left,
        computed_right = computed_right,
        left = css_length_percentage_or_auto_to_px_or_zero(&computed_left, box.layout_node(), width_of_containing_block),
        right = css_length_percentage_or_auto_to_px_or_zero(&computed_right, box.layout_node(), width_of_containing_block),
        width = css_length_or_auto_make_auto(),
    )

    # 1. The tentative used width is calculated (without 'min-width' and 'max-width')
    var input_width = css_length_or_auto_make_auto()
    let computed_width = css_computed_values_width(computed_values)
    if is_layout_table_wrapper(box):
        input_width = layout_length_or_auto_px(layout_formatting_context_compute_table_box_width_inside_table_wrapper(this, box, available_space))
    elif css_size_is_auto(&computed_width):
        input_width = css_length_or_auto_make_auto()
    else:
        input_width = layout_length_or_auto_px(layout_formatting_context_calculate_inner_width(this, box, available_space.width, computed_width))
    var used_width = layout_abspos_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(this, box, available_space.width):
        let max_width = layout_formatting_context_calculate_inner_width(this, 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_abspos_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):
        let min_width = layout_formatting_context_calculate_inner_width(this, 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_abspos_width_solver_try_compute_width(&solver, layout_length_or_auto_px(min_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.inset_left = solver.left
    box_state.inset_right = solver.right
    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/FormattingContext.cpp:909 Web::Layout::FormattingContext::compute_width_for_absolutely_positioned_replaced_element.
# https://drafts.csswg.org/css2/#abs-replaced-width
pub func layout_formatting_context_compute_width_for_absolutely_positioned_replaced_element(this: LayoutFormattingContext*, box: LayoutBox*, available_space: LayoutAvailableSpace):
    # 10.3.8 Absolutely positioned, replaced elements
    # In this case, section 10.3.7 applies up through and including the constraint equation,
    # but the rest of section 10.3.7 is replaced by the following rules:

    # 1. The used value of 'width' is determined as for inline replaced elements.

    let width = layout_formatting_context_compute_width_for_replaced_element(this, box, available_space)
    let width_of_containing_block = layout_available_size_to_px_or_zero(&available_space.width)
    let computed_values = layout_box_computed(box)
    let box_state = layout_state_get_mutable(this.m_state, box.layout_node_with_style())
    let border_left = css_computed_values_border_left(computed_values).width
    let border_right = css_computed_values_border_right(computed_values).width
    let padding_left = box_state.padding_left
    let padding_right = box_state.padding_right
    let available = width_of_containing_block.sub(width).sub(border_left).sub(padding_left).sub(padding_right).sub(border_right)
    let inset = css_computed_values_inset(computed_values)
    let margin = css_computed_values_margin(computed_values)
    var left = css_length_box_left_const(&inset)
    var margin_left = css_length_box_left_const(&margin)
    var right = css_length_box_right_const(&inset)
    var margin_right = css_length_box_right_const(&margin)
    let static_position = layout_state_used_values_static_position(layout_state_get(this.m_state, box.layout_node_with_style()))

    # The `to_px` lambda.
    let node = box.layout_node()

    # If 'margin-left' or 'margin-right' is specified as 'auto' its used value is determined by the rules below.
    # 2. If both 'left' and 'right' have the value 'auto', then if the 'direction' property of the
    # element establishing the static-position containing block is 'ltr', set 'left' to the static
    # position; else if 'direction' is 'rtl', set 'right' to the static position.
    if css_length_percentage_or_auto_is_auto(&left) and css_length_percentage_or_auto_is_auto(&right):
        left = layout_length_percentage_or_auto_px(static_position.m_x)

    # 3. If 'left' or 'right' are 'auto', replace any 'auto' on 'margin-left' or 'margin-right' with '0'.
    if css_length_percentage_or_auto_is_auto(&left) or css_length_percentage_or_auto_is_auto(&right):
        if css_length_percentage_or_auto_is_auto(&margin_left):
            margin_left = layout_length_percentage_or_auto_px(layout_zero_px())
        if css_length_percentage_or_auto_is_auto(&margin_right):
            margin_right = layout_length_percentage_or_auto_px(layout_zero_px())

    # 4. If at this point both 'margin-left' and 'margin-right' are still 'auto', solve the equation
    # under the extra constraint that the two margins must get equal values, unless this would make
    # them negative, in which case when the direction of the containing block is 'ltr' ('rtl'),
    # set 'margin-left' ('margin-right') to zero and solve for 'margin-right' ('margin-left').
    if css_length_percentage_or_auto_is_auto(&margin_left) and css_length_percentage_or_auto_is_auto(&margin_right):
        let remainder = available.sub(css_length_percentage_or_auto_to_px_or_zero(&left, node, width_of_containing_block)).sub(css_length_percentage_or_auto_to_px_or_zero(&right, node, width_of_containing_block))
        if remainder.lt(layout_zero_px()):
            margin_left = layout_length_percentage_or_auto_px(layout_zero_px())
            margin_right = layout_length_percentage_or_auto_px(layout_zero_px())
        else:
            margin_left = layout_length_percentage_or_auto_px(layout_div_int(remainder, 2))
            margin_right = layout_length_percentage_or_auto_px(layout_div_int(remainder, 2))

    # 5. If at this point there is an 'auto' left, solve the equation for that value.
    if css_length_percentage_or_auto_is_auto(&left):
        left = layout_length_percentage_or_auto_px(available.sub(css_length_percentage_or_auto_to_px_or_zero(&right, node, width_of_containing_block)).sub(css_length_percentage_or_auto_to_px_or_zero(&margin_left, node, width_of_containing_block)).sub(css_length_percentage_or_auto_to_px_or_zero(&margin_right, node, width_of_containing_block)))
    elif css_length_percentage_or_auto_is_auto(&right):
        right = layout_length_percentage_or_auto_px(available.sub(css_length_percentage_or_auto_to_px_or_zero(&left, node, width_of_containing_block)).sub(css_length_percentage_or_auto_to_px_or_zero(&margin_left, node, width_of_containing_block)).sub(css_length_percentage_or_auto_to_px_or_zero(&margin_right, node, width_of_containing_block)))
    elif css_length_percentage_or_auto_is_auto(&margin_left):
        margin_left = layout_length_percentage_or_auto_px(available.sub(css_length_percentage_or_auto_to_px_or_zero(&left, node, width_of_containing_block)).sub(css_length_percentage_or_auto_to_px_or_zero(&right, node, width_of_containing_block)).sub(css_length_percentage_or_auto_to_px_or_zero(&margin_right, node, width_of_containing_block)))
    elif css_length_percentage_or_auto_is_auto(&margin_right):
        margin_right = layout_length_percentage_or_auto_px(available.sub(css_length_percentage_or_auto_to_px_or_zero(&left, node, width_of_containing_block)).sub(css_length_percentage_or_auto_to_px_or_zero(&margin_left, node, width_of_containing_block)).sub(css_length_percentage_or_auto_to_px_or_zero(&right, node, width_of_containing_block)))

    # 6. If at this point the values are over-constrained, ignore the value for either 'left'
    # (in case the 'direction' property of the containing block is 'rtl') or 'right'
    # (in case 'direction' is 'ltr') and solve for that value.
    let remaining = available.sub(css_length_percentage_or_auto_to_px_or_zero(&left, node, width_of_containing_block)).sub(css_length_percentage_or_auto_to_px_or_zero(&right, node, width_of_containing_block)).sub(css_length_percentage_or_auto_to_px_or_zero(&margin_left, node, width_of_containing_block)).sub(css_length_percentage_or_auto_to_px_or_zero(&margin_right, node, width_of_containing_block))
    if not gfx.eq_css_pixels_int(remaining, 0):
        right = layout_length_percentage_or_auto_px(available.sub(css_length_percentage_or_auto_to_px_or_zero(&left, node, width_of_containing_block)).sub(css_length_percentage_or_auto_to_px_or_zero(&margin_left, node, width_of_containing_block)).sub(css_length_percentage_or_auto_to_px_or_zero(&margin_right, node, width_of_containing_block)))

    box_state.inset_left = css_length_percentage_or_auto_to_px_or_zero(&left, node, width_of_containing_block)
    box_state.inset_right = css_length_percentage_or_auto_to_px_or_zero(&right, node, width_of_containing_block)
    box_state.margin_left = css_length_percentage_or_auto_to_px_or_zero(&margin_left, node, width_of_containing_block)
    box_state.margin_right = css_length_percentage_or_auto_to_px_or_zero(&margin_right, node, width_of_containing_block)
    layout_state_used_values_set_content_width(box_state, width)