Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / layout / formatting_context_5.lucb

#==============================================================================================
#
#   formatting_context_5 - Layout::FormattingContext: intrinsic sizes, inner sizes and the
#                          auto-ness of sizes
#
#   DESCRIPTION:
#       Port of LibWeb/Layout/FormattingContext.cpp (lines 1872-2330): fit-content sizes,
#       min-/max-content widths and heights (laid out in a throwaway LayoutState and cached on
#       the box), inner widths and heights (box-sizing, the percentage height quirk), the
#       containing block width a node resolves against, stretch-fit sizes, when a width or
#       height is treated as auto, and whitespace-only anonymous boxes.
#
#       Copyright (c) 2020-2022, Andreas Kling <andreas@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: Fit-content sizes =======================================================================

## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:1873 Web::Layout::FormattingContext::calculate_fit_content_width.
# https://drafts.csswg.org/css-sizing-3/#fit-content-size
pub func layout_formatting_context_calculate_fit_content_width(this: LayoutFormattingContext*, box: LayoutBox*, available_space: LayoutAvailableSpace) -> gfx.CssPixels:
    # If the available space in a given axis is definite, equal to clamp(min-content size, stretch-fit size,
    # max-content size) (i.e. max(min-content size, min(max-content size, stretch-fit size))).
    if layout_available_size_is_definite(&available_space.width):
        let min_content_width = layout_formatting_context_calculate_min_content_width(this, box)
        let stretch_fit_width = layout_formatting_context_calculate_stretch_fit_width(this, box, available_space.width)
        let max_content_width = layout_formatting_context_calculate_max_content_width(this, box)
        return ak.max(min_content_width, ak.min(stretch_fit_width, max_content_width))

    # When sizing under a min-content constraint, equal to the min-content size.
    if layout_available_size_is_min_content(&available_space.width):
        return layout_formatting_context_calculate_min_content_width(this, box)

    # Otherwise, equal to the max-content size in that axis.
    return layout_formatting_context_calculate_max_content_width(this, box)

## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:1892 Web::Layout::FormattingContext::calculate_fit_content_height.
# https://drafts.csswg.org/css-sizing-3/#fit-content-size
pub func layout_formatting_context_calculate_fit_content_height(this: LayoutFormattingContext*, box: LayoutBox*, available_space: LayoutAvailableSpace) -> gfx.CssPixels:
    # If the available space in a given axis is definite,
    # equal to clamp(min-content size, stretch-fit size, max-content size)
    # (i.e. max(min-content size, min(max-content size, stretch-fit size))).
    if layout_available_size_is_definite(&available_space.height):
        let min_content_height = layout_formatting_context_calculate_min_content_height(this, box, layout_available_size_to_px_or_zero(&available_space.width))
        let stretch_fit_height = layout_formatting_context_calculate_stretch_fit_height(this, box, available_space.height)
        let max_content_height = layout_formatting_context_calculate_max_content_height(this, box, layout_available_size_to_px_or_zero(&available_space.width))
        return ak.max(min_content_height, ak.min(stretch_fit_height, max_content_height))

    # When sizing under a min-content constraint, equal to the min-content size.
    if layout_available_size_is_min_content(&available_space.height):
        return layout_formatting_context_calculate_min_content_height(this, box, layout_available_size_to_px_or_zero(&available_space.width))

    # Otherwise, equal to the max-content size in that axis.
    return layout_formatting_context_calculate_max_content_height(this, box, layout_available_size_to_px_or_zero(&available_space.width))

# mark: Intrinsic sizes =========================================================================

## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:1911 Web::Layout::FormattingContext::calculate_min_content_width.
pub func layout_formatting_context_calculate_min_content_width(this: LayoutFormattingContext*, box: LayoutBox*) -> gfx.CssPixels:
    if layout_node_is_replaced_box(box.layout_node()):
        # https://www.w3.org/TR/css-sizing-3/#replaced-percentage-min-contribution
        # NOTE: If the box is replaced, a cyclic percentage in the value of any max size property or
        #       preferred size property (width/max-width/height/max-height), is resolved against zero
        #       when calculating the min-content contribution in the corresponding axis.
        # FIXME: If the box also has a preferred aspect ratio, then this min-content contribution is
        #        floored by any <length-percentage> minimum size from the opposite axis—resolving any
        #        such percentage against zero—transferred through the preferred aspect ratio.
        let width = css_computed_values_width(layout_box_computed(box))
        if css_size_is_percentage(&width):
            return css_size_to_px(&width, box.layout_node(), layout_zero_px())
        let max_width = css_computed_values_max_width(layout_box_computed(box))
        if css_size_is_percentage(&max_width):
            return css_size_to_px(&max_width, box.layout_node(), layout_zero_px())
    let auto_size = layout_box_auto_content_box_size(box)
    if css_size_with_aspect_ratio_has_width(&auto_size):
        return auto_size.width else trap("unreachable: the auto width exists")

    # Boxes with no children have zero intrinsic width.
    if not tree_node_has_children(box.layout_node()):
        return layout_zero_px()

    let intrinsic_sizes = layout_box_cached_intrinsic_sizes(box)
    let cache = &intrinsic_sizes.min_content_width
    if let cached = *cache:
        return cached

    var throwaway_state: LayoutState
    layout_state_construct(&throwaway_state, box.layout_node_with_style())
    let containing_block = layout_node_containing_block(box.layout_node()) else trap("an intrinsically sized box without a containing block")
    _ = layout_state_populate_node_from(&throwaway_state, this.m_state, containing_block.layout_node_with_style())

    let box_state = layout_state_get_mutable(&throwaway_state, box.layout_node_with_style())
    box_state.width_constraint = LayoutSizeConstraint.min_content
    layout_state_used_values_set_indefinite_content_width(box_state)

    let context = layout_formatting_context_create_independent_formatting_context(this, &throwaway_state, LayoutMode.intrinsic_sizing, box)
    defer layout_formatting_context_destruct(context)

    let available_width = layout_available_size_make_min_content()
    let available_height = layout_available_size_make_definite(layout_state_used_values_content_height(box_state)) if layout_state_used_values_has_definite_height(box_state) else layout_available_size_make_indefinite()

    layout_formatting_context_run(context, LayoutAvailableSpace(width = available_width, height = available_height))

    let min_content_width = layout_clamp_to_max_dimension_value(layout_formatting_context_automatic_content_width(context))
    *cache = min_content_width
    return min_content_width

## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:1958 Web::Layout::FormattingContext::calculate_max_content_width.
pub func layout_formatting_context_calculate_max_content_width(this: LayoutFormattingContext*, box: LayoutBox*) -> gfx.CssPixels:
    let auto_size = layout_box_auto_content_box_size(box)
    if css_size_with_aspect_ratio_has_width(&auto_size):
        return auto_size.width else trap("unreachable: the auto width exists")

    # Boxes with no children have zero intrinsic width.
    if not tree_node_has_children(box.layout_node()):
        return layout_zero_px()

    let intrinsic_sizes = layout_box_cached_intrinsic_sizes(box)
    let cache = &intrinsic_sizes.max_content_width
    if let cached = *cache:
        return cached

    var throwaway_state: LayoutState
    layout_state_construct(&throwaway_state, box.layout_node_with_style())
    let containing_block = layout_node_containing_block(box.layout_node()) else trap("an intrinsically sized box without a containing block")
    _ = layout_state_populate_node_from(&throwaway_state, this.m_state, containing_block.layout_node_with_style())

    let actual_box_state = layout_state_get(this.m_state, box.layout_node_with_style())

    let box_state = layout_state_get_mutable(&throwaway_state, box.layout_node_with_style())
    box_state.width_constraint = LayoutSizeConstraint.max_content
    layout_state_used_values_set_indefinite_content_width(box_state)

    box_state.border_left = actual_box_state.border_left
    box_state.padding_left = actual_box_state.padding_left

    box_state.border_right = actual_box_state.border_right
    box_state.padding_right = actual_box_state.padding_right

    let context = layout_formatting_context_create_independent_formatting_context(this, &throwaway_state, LayoutMode.intrinsic_sizing, box)
    defer layout_formatting_context_destruct(context)

    let available_width = layout_available_size_make_max_content()
    let available_height = layout_available_size_make_definite(layout_state_used_values_content_height(box_state)) if layout_state_used_values_has_definite_height(box_state) else layout_available_size_make_indefinite()

    layout_formatting_context_run(context, LayoutAvailableSpace(width = available_width, height = available_height))

    let max_content_width = layout_clamp_to_max_dimension_value(layout_formatting_context_automatic_content_width(context))
    *cache = max_content_width
    return max_content_width

## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:2002 Web::Layout::FormattingContext::calculate_min_content_height.
# https://www.w3.org/TR/css-sizing-3/#min-content-block-size
pub func layout_formatting_context_calculate_min_content_height(this: LayoutFormattingContext*, box: LayoutBox*, width: gfx.CssPixels) -> gfx.CssPixels:
    # For block containers, tables, and inline boxes, this is equivalent to the max-content block size.
    let display = layout_node_display(box.layout_node())
    if layout_node_is_block_container(box.layout_node()) or css_display_is_table_inside(&display):
        return layout_formatting_context_calculate_max_content_height(this, box, width)

    let auto_size = layout_box_auto_content_box_size(box)
    if css_size_with_aspect_ratio_has_height(&auto_size):
        if let aspect_ratio = auto_size.aspect_ratio:
            return width.div_fraction(aspect_ratio)
        return auto_size.height else trap("unreachable: the auto height exists")

    # Boxes with no children have zero intrinsic height.
    if not tree_node_has_children(box.layout_node()):
        return layout_zero_px()

    let intrinsic_sizes = layout_box_cached_intrinsic_sizes(box)
    let cache = ak.hash_map_ensure(&intrinsic_sizes.min_content_height, width)
    if let cached = *cache:
        return cached

    var throwaway_state: LayoutState
    layout_state_construct(&throwaway_state, box.layout_node_with_style())
    let containing_block = layout_node_containing_block(box.layout_node()) else trap("an intrinsically sized box without a containing block")
    _ = layout_state_populate_node_from(&throwaway_state, this.m_state, containing_block.layout_node_with_style())

    let box_state = layout_state_get_mutable(&throwaway_state, box.layout_node_with_style())
    box_state.height_constraint = LayoutSizeConstraint.min_content
    layout_state_used_values_set_indefinite_content_height(box_state)
    layout_state_used_values_set_content_width(box_state, width)

    let context = layout_formatting_context_create_independent_formatting_context(this, &throwaway_state, LayoutMode.intrinsic_sizing, box)
    defer layout_formatting_context_destruct(context)

    layout_formatting_context_run(context, LayoutAvailableSpace(width = layout_available_size_make_definite(width), height = layout_available_size_make_min_content()))

    let min_content_height = layout_clamp_to_max_dimension_value(layout_formatting_context_automatic_content_height(context))
    *cache = min_content_height
    return min_content_height

## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:2039 Web::Layout::FormattingContext::calculate_max_content_height.
pub func layout_formatting_context_calculate_max_content_height(this: LayoutFormattingContext*, box: LayoutBox*, width: gfx.CssPixels) -> gfx.CssPixels:
    if let preferred_aspect_ratio = layout_box_preferred_aspect_ratio(box):
        return width.div_fraction(preferred_aspect_ratio)

    let auto_size = layout_box_auto_content_box_size(box)
    if css_size_with_aspect_ratio_has_height(&auto_size):
        return auto_size.height else trap("unreachable: the auto height exists")

    # Boxes with no children have zero intrinsic height.
    if not tree_node_has_children(box.layout_node()):
        return layout_zero_px()

    let intrinsic_sizes = layout_box_cached_intrinsic_sizes(box)
    let cache_slot = ak.hash_map_ensure(&intrinsic_sizes.max_content_height, width)
    if let cached = *cache_slot:
        return cached

    var throwaway_state: LayoutState
    layout_state_construct(&throwaway_state, box.layout_node_with_style())
    let containing_block = layout_node_containing_block(box.layout_node()) else trap("an intrinsically sized box without a containing block")
    _ = layout_state_populate_node_from(&throwaway_state, this.m_state, containing_block.layout_node_with_style())

    let box_state = layout_state_get_mutable(&throwaway_state, box.layout_node_with_style())
    box_state.height_constraint = LayoutSizeConstraint.max_content
    layout_state_used_values_set_indefinite_content_height(box_state)
    layout_state_used_values_set_content_width(box_state, width)

    let context = layout_formatting_context_create_independent_formatting_context(this, &throwaway_state, LayoutMode.intrinsic_sizing, box)
    defer layout_formatting_context_destruct(context)

    layout_formatting_context_run(context, LayoutAvailableSpace(width = layout_available_size_make_definite(width), height = layout_available_size_make_max_content()))

    let max_content_height = layout_clamp_to_max_dimension_value(layout_formatting_context_automatic_content_height(context))
    *cache_slot = max_content_height
    return max_content_height

# mark: Inner sizes =============================================================================

## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:2072 Web::Layout::FormattingContext::calculate_inner_width.
pub func layout_formatting_context_calculate_inner_width(this: LayoutFormattingContext*, box: LayoutBox*, available_width: LayoutAvailableSize, width: CssSize) -> gfx.CssPixels:
    assert(not css_size_is_auto(&width))

    let width_of_containing_block = layout_available_size_to_px_or_zero(&available_width)
    if css_size_is_fit_content(&width):
        return layout_formatting_context_calculate_fit_content_width(this, box, LayoutAvailableSpace(width = available_width, height = layout_available_size_make_indefinite()))
    if css_size_is_max_content(&width):
        return layout_formatting_context_calculate_max_content_width(this, box)
    if css_size_is_min_content(&width):
        return layout_formatting_context_calculate_min_content_width(this, box)

    let computed_values = layout_box_computed(box)
    if css_computed_values_box_sizing(computed_values) == css_data.BoxSizing.border_box:
        let state = layout_state_get(this.m_state, box.layout_node_with_style())
        let inner_width = (css_size_to_px(&width, box.layout_node(), width_of_containing_block).sub(css_computed_values_border_left(computed_values).width).sub(state.padding_left).sub(css_computed_values_border_right(computed_values).width).sub(state.padding_right))
        return ak.max(inner_width, layout_zero_px())

    return css_size_to_px(&width, box.layout_node(), width_of_containing_block)

## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:2101 Web::Layout::FormattingContext::calculate_inner_height.
pub func layout_formatting_context_calculate_inner_height(this: LayoutFormattingContext*, box: LayoutBox*, available_space: LayoutAvailableSpace, height: CssSize) -> gfx.CssPixels:
    if css_size_is_auto(&height):
        if let preferred_aspect_ratio = layout_box_preferred_aspect_ratio(box):
            # (`*box.preferred_aspect_ratio() == 0` compares through operator CSSPixels.)
            if gfx.eq_css_pixels_int(gfx.css_pixel_fraction_to_css_pixels(&preferred_aspect_ratio), 0):
                return gfx.CssPixels.from_int(0)
            return layout_state_used_values_content_width(layout_state_get(this.m_state, box.layout_node_with_style())).div_fraction(preferred_aspect_ratio)

    assert(not css_size_is_auto(&height))

    if css_size_is_fit_content(&height):
        return layout_formatting_context_calculate_fit_content_height(this, box, available_space)
    if css_size_is_max_content(&height):
        return layout_formatting_context_calculate_max_content_height(this, box, layout_available_size_to_px_or_zero(&available_space.width))
    if css_size_is_min_content(&height):
        return layout_formatting_context_calculate_min_content_height(this, box, layout_available_size_to_px_or_zero(&available_space.width))

    var height_of_containing_block = layout_available_size_to_px_or_zero(&available_space.height)
    # NOTE: Percentage heights are resolved against the containing block's used height,
    #       not the available space height. The containing block's height must be definite
    #       for percentage resolution to work (otherwise should_treat_height_as_auto
    #       should have returned true and we wouldn't be here).
    # NOTE: We only do this when available space height is indefinite. If it's definite,
    #       we trust that the caller has set it up correctly (e.g., grid/flex items get
    #       their cell/area size as available space).
    if css_size_contains_percentage(&height) and layout_available_size_is_indefinite(&available_space.height):
        var containing_block = layout_node_containing_block(box.layout_node())
        while let block = containing_block:
            let display = layout_node_display(block.layout_node())
            if not (layout_node_is_anonymous(block.layout_node()) and not css_display_is_table_cell(&display)):
                break
            containing_block = layout_node_containing_block(block.layout_node())

        # https://quirks.spec.whatwg.org/#the-percentage-height-calculation-quirk
        # In quirks mode, walk up to find an ancestor with explicit height or the viewport.
        # NOTE: Flex/grid items resolve percentage heights against their container, not via quirk.
        var is_flex_or_grid_item = false
        if let parent = layout_node_parent(box.layout_node()):
            let parent_display = layout_node_display(parent.layout_node())
            is_flex_or_grid_item = css_display_is_flex_inside(&parent_display) or css_display_is_grid_inside(&parent_display)
        var is_in_ua_shadow_tree = false
        if let dom_node = layout_node_dom_node(box.layout_node()):
            if let shadow_root = dom_node_containing_shadow_root(dom_node):
                is_in_ua_shadow_tree = dom_shadow_root_is_user_agent_internal(shadow_root)
        if dom_document_in_quirks_mode(layout_node_document(box.layout_node())) and not layout_node_is_anonymous(box.layout_node()) and not is_flex_or_grid_item and not is_in_ua_shadow_tree:
            while let block = containing_block:
                if layout_node_is_viewport(block.layout_node()):
                    break
                let block_height = css_computed_values_height(layout_box_computed(block))
                if not css_size_is_auto(&block_height):
                    break
                containing_block = layout_node_containing_block(block.layout_node())

        if let block = containing_block:
            if let containing_block_used_values = layout_state_try_get_layout_node_with_style(this.m_state, block.layout_node_with_style()):
                if layout_state_used_values_has_definite_height(containing_block_used_values):
                    height_of_containing_block = layout_state_used_values_content_height(containing_block_used_values)
    let computed_values = layout_box_computed(box)

    if css_computed_values_box_sizing(computed_values) == css_data.BoxSizing.border_box:
        let state = layout_state_get(this.m_state, box.layout_node_with_style())
        let inner_height = (css_size_to_px(&height, box.layout_node(), height_of_containing_block).sub(css_computed_values_border_top(computed_values).width).sub(state.padding_top).sub(css_computed_values_border_bottom(computed_values).width).sub(state.padding_bottom))
        return ak.max(inner_height, layout_zero_px())

    return css_size_to_px(&height, box.layout_node(), height_of_containing_block)

## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:2166 Web::Layout::FormattingContext::containing_block_width_for.
pub func layout_formatting_context_containing_block_width_for(this: LayoutFormattingContext*, node: LayoutNodeWithStyleAndBoxModelMetrics*) -> gfx.CssPixels:
    let used_values = layout_state_get(this.m_state, node.layout_node_with_style())
    match used_values.width_constraint:
        .min_content:
            return layout_zero_px()
        .max_content:
            return gfx.css_pixels_max()
        .none_:
            let containing_block_used_values = layout_state_used_values_containing_block_used_values(used_values) else trap("a node without the containing block's used values")
            return layout_state_used_values_content_width(containing_block_used_values)
        _:
            trap("VERIFY_NOT_REACHED")

## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:2181 Web::Layout::FormattingContext::calculate_stretch_fit_width.
# https://drafts.csswg.org/css-sizing-3/#stretch-fit-size
pub func layout_formatting_context_calculate_stretch_fit_width(this: LayoutFormattingContext*, box: LayoutBox*, available_width: LayoutAvailableSize) -> gfx.CssPixels:
    # The size a box would take if its outer size filled the available space in the given axis;
    # in other words, the stretch fit into the available space, if that is definite.

    # Undefined if the available space is indefinite.
    if not layout_available_size_is_definite(&available_width):
        return layout_zero_px()

    let box_state = layout_state_get(this.m_state, box.layout_node_with_style())
    return (layout_available_size_to_px_or_zero(&available_width).sub(box_state.margin_left).sub(box_state.margin_right).sub(box_state.padding_left).sub(box_state.padding_right).sub(box_state.border_left).sub(box_state.border_right))

## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:2201 Web::Layout::FormattingContext::calculate_stretch_fit_height.
# https://drafts.csswg.org/css-sizing-3/#stretch-fit-size
pub func layout_formatting_context_calculate_stretch_fit_height(this: LayoutFormattingContext*, box: LayoutBox*, available_height: LayoutAvailableSize) -> gfx.CssPixels:
    # The size a box would take if its outer size filled the available space in the given axis;
    # in other words, the stretch fit into the available space, if that is definite.
    # Undefined if the available space is indefinite.
    let box_state = layout_state_get(this.m_state, box.layout_node_with_style())
    return (layout_available_size_to_px_or_zero(&available_height).sub(box_state.margin_top).sub(box_state.margin_bottom).sub(box_state.padding_top).sub(box_state.padding_bottom).sub(box_state.border_top).sub(box_state.border_bottom))

# mark: Treating sizes as auto or none ==========================================================

## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:2216 Web::Layout::FormattingContext::should_treat_width_as_auto.
pub func layout_formatting_context_should_treat_width_as_auto(this: LayoutFormattingContext*, box: LayoutBox*, available_space: LayoutAvailableSpace) -> bool:
    let computed_width = css_computed_values_width(layout_box_computed(box))
    if css_size_is_auto(&computed_width):
        return true

    # https://drafts.csswg.org/css-sizing-3/#cyclic-percentage-contribution
    if css_size_contains_percentage(&computed_width):
        if not layout_node_is_replaced_box(box.layout_node()) and layout_available_size_is_min_content(&available_space.width):
            return true
        if layout_available_size_is_max_content(&available_space.width):
            return true
        if layout_available_size_is_indefinite(&available_space.width):
            return true
    # AD-HOC: If the box has a preferred aspect ratio and an intrinsic keyword for width...
    if layout_box_has_preferred_aspect_ratio(box) and css_size_is_intrinsic_sizing_constraint(&computed_width):
        # If the box has no natural height to resolve the aspect ratio, we treat the width as auto.
        let auto_size = layout_box_auto_content_box_size(box)
        if not css_size_with_aspect_ratio_has_height(&auto_size):
            return true
        # If the box has definite height, we can resolve the width through the aspect ratio.
        if layout_state_used_values_has_definite_height(layout_state_get(this.m_state, box.layout_node_with_style())):
            return true
    return false

## The `percentage_height_quirk_applies` lambda of should_treat_height_as_auto.
func layout_formatting_context_percentage_height_quirk_applies(box: LayoutBox*) -> bool:
    if not dom_document_in_quirks_mode(layout_node_document(box.layout_node())) or layout_node_is_anonymous(box.layout_node()):
        return false
    let display = layout_node_display(box.layout_node())
    if css_display_is_table_inside(&display):
        return false
    # Flex/grid items resolve percentage heights against their container, not via quirk.
    if let parent = layout_node_parent(box.layout_node()):
        let parent_display = layout_node_display(parent.layout_node())
        if css_display_is_flex_inside(&parent_display):
            return false
    if let parent = layout_node_parent(box.layout_node()):
        let parent_display = layout_node_display(parent.layout_node())
        if css_display_is_grid_inside(&parent_display):
            return false
    # The quirk should not apply inside user agent shadow trees.
    if let dom_node = layout_node_dom_node(box.layout_node()):
        if let shadow_root = dom_node_containing_shadow_root(dom_node):
            if dom_shadow_root_is_user_agent_internal(shadow_root):
                return false
    return true

## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:2243 Web::Layout::FormattingContext::should_treat_height_as_auto.
pub func layout_formatting_context_should_treat_height_as_auto(this: LayoutFormattingContext*, box: LayoutBox*, available_space: LayoutAvailableSpace) -> bool:
    let computed_height = css_computed_values_height(layout_box_computed(box))
    if css_size_is_auto(&computed_height):
        let box_state = layout_state_get(this.m_state, box.layout_node_with_style())
        if layout_state_used_values_has_definite_width(box_state) and layout_box_has_preferred_aspect_ratio(box):
            return false
        return true

    # https://drafts.csswg.org/css-sizing-3/#cyclic-percentage-contribution
    if css_size_contains_percentage(&computed_height):
        if not layout_node_is_replaced_box(box.layout_node()) and layout_available_size_is_min_content(&available_space.height):
            return true
        if layout_available_size_is_max_content(&available_space.height):
            return true
        # https://www.w3.org/TR/CSS22/visudet.html#the-height-property
        # If the height of the containing block is not specified explicitly (i.e., it depends on
        # content height), and this element is not absolutely positioned, the percentage value
        # is treated as 'auto'.
        # https://quirks.spec.whatwg.org/#the-percentage-height-calculation-quirk
        # In quirks mode, percentage heights can resolve even without explicit containing block
        # height. The quirk applies to DOM elements only (not anonymous boxes), and excludes
        # table-related display types.
        if not layout_node_is_absolutely_positioned(box.layout_node()):
            let percentage_height_quirk_applies = layout_formatting_context_percentage_height_quirk_applies(box)
            if not percentage_height_quirk_applies:
                # NOTE: Anonymous blocks inherit height definiteness from their containing block.
                #       However, anonymous table cells are proper containing blocks with their own
                #       height semantics, so we must not walk past them.
                var containing_block = layout_node_containing_block(box.layout_node())
                while let block = containing_block:
                    let display = layout_node_display(block.layout_node())
                    if not (layout_node_is_anonymous(block.layout_node()) and not css_display_is_table_cell(&display)):
                        break
                    containing_block = layout_node_containing_block(block.layout_node())
                let block = containing_block else return true
                let containing_block_used_values = layout_state_try_get_layout_node_with_style(this.m_state, block.layout_node_with_style()) else return true
                if not layout_state_used_values_has_definite_height(containing_block_used_values):
                    return true

    # AD-HOC: If the box has a preferred aspect ratio and an intrinsic keyword for height...
    if layout_box_has_preferred_aspect_ratio(box) and css_size_is_intrinsic_sizing_constraint(&computed_height):
        # If the box has no natural width to resolve the aspect ratio, we treat the height as auto.
        let auto_size = layout_box_auto_content_box_size(box)
        if not css_size_with_aspect_ratio_has_width(&auto_size):
            return true
        # If the box has definite width, we can resolve the height through the aspect ratio.
        if layout_state_used_values_has_definite_width(layout_state_get(this.m_state, box.layout_node_with_style())):
            return true
    return false

## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:2315 Web::Layout::FormattingContext::can_skip_is_anonymous_text_run.
pub func layout_formatting_context_can_skip_is_anonymous_text_run(this: LayoutFormattingContext*, box: LayoutBox*) -> bool:
    _ = this
    if layout_node_is_anonymous(box.layout_node()) and not layout_node_is_generated_for_pseudo_element(box.layout_node()) and tree_node_first_child_of_type(box.layout_node(), &layout_block_container_class) == none:
        var contains_only_white_space = true
        for node in layout_node_subtree(box.layout_node()):
            if let text_node = as_if_layout_text_node(node):
                let data = dom_character_data_data(layout_text_node_dom_node(text_node).dom_character_data())
                if ak.utf16_string_base_is_ascii_whitespace(&data.utf16_string_base):
                    continue
            contains_only_white_space = false
            break
        if contains_only_white_space:
            return true
    return false