Skip to content

Truly independent web browser

LibWeb: Lay out auto-height abspos flex and grid boxes once

Aliaksandr Kalenik18c37d648e06
diff --git a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
index 9bacdbcacdb..23ac0feaf8f 100644
--- a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
+++ b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
@@ -2352,7 +2352,9 @@ pub(crate) struct InsideLayoutBlockSizes {
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum BlockSizePass {
- BeforeInsideLayout,
+ BeforeInsideLayout {
+ inside_layout_resolves_automatic_block_size: bool,
+ },
AfterInsideLayout {
inside_layout: Option<InsideLayoutBlockSizes>,
},
@@ -2434,9 +2436,15 @@ impl AbsposEngine<'_> {
{
return Some(inside_layout.automatic_content_block_size);
}
+ if let BlockSizePass::BeforeInsideLayout {
+ inside_layout_resolves_automatic_block_size: true,
+ } = pass
+ {
+ return None;
+ }
if self.facts(node).creates_block_formatting_context() {
return match pass {
- BlockSizePass::BeforeInsideLayout => None,
+ BlockSizePass::BeforeInsideLayout { .. } => None,
BlockSizePass::AfterInsideLayout { .. } => Some(CssPixels::default()),
};
}
@@ -2718,7 +2726,7 @@ impl AbsposEngine<'_> {
let used = sized.cells;
used.set_content_block_size(auto_px_value(solution.block_size));
- if style.height().is_auto() && pass == BlockSizePass::BeforeInsideLayout {
+ if style.height().is_auto() && matches!(pass, BlockSizePass::BeforeInsideLayout { .. }) {
// NB: A box that establishes an orthogonal flow is sized in this axis before its contents are laid out,
// which they are at that size.
if solution.block_size.is_some() && self.sizing().establishes_orthogonal_flow(node) {
@@ -2808,7 +2816,7 @@ impl AbsposEngine<'_> {
let used = sized.cells;
used.set_content_block_size(block_size);
- if style.height().is_auto() && pass == BlockSizePass::BeforeInsideLayout {
+ if style.height().is_auto() && matches!(pass, BlockSizePass::BeforeInsideLayout { .. }) {
// https://drafts.csswg.org/css-sizing-4/#aspect-ratio-automatic
// A block size transferred through the preferred aspect ratio from a definite inline size is definite, so
// the content resolves percentages against it.
@@ -2872,11 +2880,14 @@ impl<'pass> AbsposEngine<'pass> {
// Run-prelude sizing for an absolutely positioned root: box-model
// metrics, the inset-aware inline solve, the pre-inside-layout block
// pass, and the definiteness overrides insets and aspect ratios provide.
+ // Answers the space a flex root resolves its own automatic block size in,
+ // if it does.
pub(crate) fn dimension_out_of_flow_root(
&self,
sized: OutOfFlowBox<'_>,
inputs: abspos_inputs::AbsposLayoutInputs,
- ) {
+ inside_layout_follows: bool,
+ ) -> Option<AvailableSpace> {
let node = sized.node;
let (available_space, constraints) = out_of_flow_root_space(inputs);
let containing_block_inline_size = available_space.inline_size.to_px_or_zero();
@@ -2909,12 +2920,28 @@ impl<'pass> AbsposEngine<'pass> {
resolved,
self.auto_margin_overflow(inputs.containing_block, SizingAxis::Inline),
);
+ let fc_type = formatting_context::formatting_context_type_created_by_box(self.facts(node));
+ let flex_self_resolution_space = AvailableSpace {
+ inline_size: AvailableSize::definite(sized.cells.content_inline_size()),
+ block_size: AvailableSize::Indefinite,
+ };
+ let inside_layout_resolves_automatic_block_size = inside_layout_follows
+ && self.inside_layout_resolves_automatic_block_size(
+ sized,
+ style,
+ fc_type,
+ available_space,
+ flex_self_resolution_space,
+ constraints,
+ );
self.compute_block_size(
sized,
available_space,
constraints,
static_position_rect_for_sizing(&inputs),
- BlockSizePass::BeforeInsideLayout,
+ BlockSizePass::BeforeInsideLayout {
+ inside_layout_resolves_automatic_block_size,
+ },
resolved,
self.auto_margin_overflow(inputs.containing_block, SizingAxis::Block),
);
@@ -2946,6 +2973,66 @@ impl<'pass> AbsposEngine<'pass> {
self.sizing()
.make_form_control_content_box_definite(sized.sized_cells(), available_space, constraints, None);
+ (inside_layout_resolves_automatic_block_size
+ && fc_type == Some(formatting_context::FormattingContextType::Flex))
+ .then_some(flex_self_resolution_space)
+ }
+
+ fn inside_layout_resolves_automatic_block_size(
+ &self,
+ sized: OutOfFlowBox<'_>,
+ style: StyleValues<'_>,
+ fc_type: Option<formatting_context::FormattingContextType>,
+ available_space: AvailableSpace,
+ flex_self_resolution_space: AvailableSpace,
+ constraints: ContainingBlockConstraints,
+ ) -> bool {
+ let node = sized.node;
+ let sizing = self.sizing();
+ let facts = self.facts(node);
+ if !crate::layout::sizing_context::is_plain_auto(style.height())
+ || (!style.inset_top().is_auto() && !style.inset_bottom().is_auto())
+ || sizing.establishes_orthogonal_flow(node)
+ || facts.has_preferred_aspect_ratio()
+ || facts.node_has_size_containment()
+ || sizing.box_is_sized_as_replaced_element(sized.sized_box(), available_space, constraints)
+ {
+ return false;
+ }
+ let style_seen_by_inside_layout = self.style(node);
+ let inside_layout_reads_the_same_min_and_max_height = style_seen_by_inside_layout.min_height()
+ == style.min_height()
+ && style_seen_by_inside_layout.max_height() == style.max_height();
+ if !inside_layout_reads_the_same_min_and_max_height {
+ return false;
+ }
+ match fc_type {
+ Some(formatting_context::FormattingContextType::Grid) => {
+ crate::layout::sizing_context::is_plain_auto(style_seen_by_inside_layout.min_height())
+ || style_seen_by_inside_layout.max_height().is_none()
+ }
+ Some(formatting_context::FormattingContextType::Flex) => {
+ let main_axis_is_block_axis = matches!(
+ style_seen_by_inside_layout.effective_flex_direction(),
+ flex_direction::COLUMN | flex_direction::COLUMN_REVERSE
+ );
+ let lays_out_its_lines_against_its_block_size =
+ main_axis_is_block_axis && style_seen_by_inside_layout.flex_wrap() != flex_wrap::NOWRAP;
+ !lays_out_its_lines_against_its_block_size
+ && matches!(
+ style_seen_by_inside_layout.max_height().kind,
+ ComputedSizeKind::None | ComputedSizeKind::Length | ComputedSizeKind::Percentage
+ )
+ && formatting_context::flex_self_block_size_resolution_space(
+ &sizing,
+ sized.sized_box(),
+ flex_self_resolution_space,
+ constraints,
+ )
+ .is_some()
+ }
+ _ => false,
+ }
}
pub(crate) fn finalize_out_of_flow_root_after_inside_layout(
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index 037e66f4b60..ff1e4664a9d 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -1629,15 +1629,18 @@ fn apply_root_sizing_directives(
body_input
}
ParticipationInParentFormattingContext::AbsolutelyPositioned(abspos_inputs) => {
- abspos_engine::AbsposEngine::for_run(run).dimension_out_of_flow_root(
+ let flex_self_resolution_space = abspos_engine::AbsposEngine::for_run(run).dimension_out_of_flow_root(
abspos_engine::OutOfFlowBox::new(
sized_root,
root_used_insets,
run.state.anchor_resolved_values_of(run.box_),
),
abspos_inputs,
+ true,
);
- body_input_with_inner_available_space(sized_root.cells, input)
+ let mut body_input = body_input_with_inner_available_space(sized_root.cells, input);
+ body_input.sizing.flex_self_block_size_resolution_space = flex_self_resolution_space;
+ body_input
}
ParticipationInParentFormattingContext::Item => {
if cfg!(debug_assertions) && run.fragments.is_some() {
@@ -1905,7 +1908,7 @@ fn size_skipped_independent_root(
&used_insets,
anchor_resolved_values.as_ref(),
);
- engine.dimension_out_of_flow_root(sized_child, abspos_inputs);
+ engine.dimension_out_of_flow_root(sized_child, abspos_inputs, false);
engine.finalize_out_of_flow_root_after_inside_layout(sized_child, abspos_inputs, None);
Some(used_insets.get())
}
diff --git a/Tests/LibWeb/Layout/expected/abspos-auto-height-flex-and-grid-roots.txt b/Tests/LibWeb/Layout/expected/abspos-auto-height-flex-and-grid-roots.txt
new file mode 100644
index 00000000000..1f50936f02f
--- /dev/null
+++ b/Tests/LibWeb/Layout/expected/abspos-auto-height-flex-and-grid-roots.txt
@@ -0,0 +1,294 @@
+Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline overflow: [0,0 800x5400]
+ BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 5400 0+0+0] [BFC] children: not-inline
+ BlockContainer <body> at [0,0] [0+0+0 800 0+0+0] [0+0+0 5400 0+0+0] children: not-inline
+ BlockContainer <div.cb> at [0,0] positioned [0+0+0 400 0+0+400] [0+0+0 300 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ Box <div#columnGrow.root.column> at [5,9] positioned flex-container(column) [0+2+3 190 3+2+0] [0+2+7 40 5+2+0] [FFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.item> at [5,9] flex-item [0+0+0 20 0+0+0] [0+0+0 16 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [5,29] flex-item [0+0+0 190 0+0+0] [0+0+0 0 0+0+0] [BFC] children: not-inline
+ BlockContainer <div.half> at [5,29] [0+0+0 10 0+0+180] [0+0+0 0 0+0+0] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.item> at [5,33] flex-item [0+0+0 20 0+0+0] [0+0+0 16 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [0,300] [0+0+0 800 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.cb> at [0,300] positioned [0+0+0 400 0+0+400] [0+0+0 300 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ Box <div#columnMaxPx.root.column> at [5,305] positioned flex-container(column) [0+1+4 190 4+1+0] [0+1+4 20 4+1+0] [FFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.item> at [5,305] flex-item [0+0+0 20 0+0+0] [0+0+0 10 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.item> at [5,315] flex-item [0+0+0 20 0+0+0] [0+0+0 10 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [0,600] [0+0+0 800 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.cb> at [0,600] positioned [0+0+0 400 0+0+400] [0+0+0 300 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ Box <div#columnMaxPercent.root.column> at [6,606] positioned flex-container(column) [0+0+6 188 6+0+0] [0+0+6 33 6+0+0] [FFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.item> at [6,606] flex-item [0+0+0 20 0+0+0] [0+0+0 11 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.item> at [6,617] flex-item [0+0+0 20 0+0+0] [0+0+0 11 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.item> at [6,628] flex-item [0+0+0 20 0+0+0] [0+0+0 11 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [0,900] [0+0+0 800 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.cb> at [0,900] positioned [0+0+0 400 0+0+400] [0+0+0 300 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ Box <div#rowWrap.root.row> at [0,900] positioned flex-container(row) [0+0+0 50 0+0+0] [0+0+0 55 0+0+0] [FFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.item> at [0,900] flex-item [0+0+0 20 0+0+0] [0+0+0 20 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.item> at [20,900] flex-item [0+0+0 20 0+0+0] [0+0+0 35 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.item> at [0,940.5] flex-item [0+0+0 20 0+0+0] [0+0+0 20 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [20,940.5] flex-item [0+0+0 20 0+0+0] [0+0+0 20 0+0+0] [BFC] children: not-inline
+ BlockContainer <div.half> at [20,940.5] [0+0+0 10 0+0+10] [0+0+0 10 0+0+0] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [0,1200] [0+0+0 800 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.cb> at [0,1200] positioned [0+0+0 400 0+0+400] [0+0+0 300 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ Box <div#rowMaxPercent.root.row> at [3,1203] positioned flex-container(row) [0+0+3 200 3+0+0] [0+0+3 30 3+0+0] [FFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.item> at [3,1203] flex-item [0+0+0 20 0+0+0] [0+0+0 50 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [23,1203] flex-item [0+0+0 20 0+0+0] [0+0+0 50 0+0+0] [BFC] children: not-inline
+ BlockContainer <div.half> at [23,1203] [0+0+0 10 0+0+10] [0+0+0 25 0+0+0] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [0,1500] [0+0+0 800 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.cb> at [0,1500] positioned [0+0+0 400 0+0+400] [0+0+0 300 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ Box <div#gridRows.root.grid> at [0,1500] positioned [0+0+0 200 0+0+0] [0+0+0 80 0+0+0] [GFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.item> at [0,1500] [0+0+0 20 0+0+0] [0+0+0 20 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.item> at [0,1524] [0+0+0 20 0+0+0] [0+0+0 20 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [0,1544] [0+0+0 200 0+0+0] [0+0+0 0 0+0+0] [BFC] children: not-inline
+ BlockContainer <div.half> at [0,1544] [0+0+0 10 0+0+190] [0+0+0 0 0+0+0] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.item> at [0,1548] [0+0+0 20 0+0+0] [0+0+0 30 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [0,1800] [0+0+0 800 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.cb> at [0,1800] positioned [0+0+0 400 0+0+400] [0+0+0 300 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ Box <div#gridMinHeight.root.grid> at [0,1800] positioned [0+0+0 200 0+0+0] [0+0+0 100 0+0+0] [GFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.item> at [0,1830] [0+0+0 20 0+0+0] [0+0+0 20 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.item> at [0,1850] [0+0+0 20 0+0+0] [0+0+0 20 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [0,2100] [0+0+0 800 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.cb> at [0,2100] positioned [0+0+0 400 0+0+400] [0+0+0 300 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ Box <div#gridMinMaxHeight.root.grid> at [0,2100] positioned [0+0+0 200 0+0+0] [0+0+0 60 0+0+0] [GFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.item> at [0,2100] [0+0+0 20 0+0+0] [0+0+0 50 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.item> at [0,2150] [0+0+0 20 0+0+0] [0+0+0 30 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [0,2400] [0+0+0 800 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.cb> at [0,2400] positioned [0+0+0 400 0+0+400] [0+0+0 300 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ Box <div#columnBothInsetsStretch.root.column> at [0,2400] positioned flex-container(column) [0+0+0 200 0+0+0] [0+0+0 300 0+0+0] [FFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.item> at [0,2400] flex-item [0+0+0 20 0+0+0] [0+0+0 20 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [0,2420] flex-item [0+0+0 200 0+0+0] [0+0+0 280 0+0+0] [BFC] children: not-inline
+ BlockContainer <div.half> at [0,2420] [0+0+0 10 0+0+190] [0+0+0 140 0+0+0] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [0,2700] [0+0+0 800 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.cb> at [0,2700] positioned [0+0+0 400 0+0+400] [0+0+0 300 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ Box <div#columnBothInsetsEnd.root.column> at [0,2980] positioned flex-container(column) [0+0+0 200 0+0+0] [0+0+0 20 0+0+0] [FFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.item> at [0,2980] flex-item [0+0+0 20 0+0+0] [0+0+0 20 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [0,3000] flex-item [0+0+0 200 0+0+0] [0+0+0 0 0+0+0] [BFC] children: not-inline
+ BlockContainer <div.half> at [0,3000] [0+0+0 10 0+0+190] [0+0+0 0 0+0+0] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [0,3000] [0+0+0 800 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.cb> at [0,3000] positioned [0+0+0 400 0+0+400] [0+0+0 300 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ Box <div#gridBothInsetsStretch.root.grid> at [0,3000] positioned [0+0+0 200 0+0+0] [0+0+0 300 0+0+0] [GFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.item> at [0,3000] [0+0+0 20 0+0+0] [0+0+0 20 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [0,3300] [0+0+0 200 0+0+0] [0+0+0 0 0+0+0] [BFC] children: not-inline
+ BlockContainer <div.half> at [0,3300] [0+0+0 10 0+0+190] [0+0+0 0 0+0+0] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [0,3300] [0+0+0 800 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.cb> at [0,3300] positioned [0+0+0 400 0+0+400] [0+0+0 300 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ Box <div#gridBothInsetsEnd.root.grid> at [0,3580] positioned [0+0+0 200 0+0+0] [0+0+0 20 0+0+0] [GFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.item> at [0,3580] [0+0+0 20 0+0+0] [0+0+0 20 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [0,3600] [0+0+0 200 0+0+0] [0+0+0 0 0+0+0] [BFC] children: not-inline
+ BlockContainer <div.half> at [0,3600] [0+0+0 10 0+0+190] [0+0+0 0 0+0+0] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [0,3600] [0+0+0 800 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.cb> at [0,3600] positioned [0+0+0 400 0+0+400] [0+0+0 300 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ Box <div#rowBottomInset.root.row> at [0,3850] positioned flex-container(row) [0+0+0 200 0+0+0] [0+0+0 40 0+0+0] [FFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.item> at [0,3850] flex-item [0+0+0 20 0+0+0] [0+0+0 20 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.item> at [20,3850] flex-item [0+0+0 20 0+0+0] [0+0+0 40 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [0,3900] [0+0+0 800 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.cb> at [0,3900] positioned [0+0+0 400 0+0+400] [0+0+0 300 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ Box <div#columnStaticPosition.root.column> at [0,3920] positioned flex-container(column) [0+0+0 200 0+0+0] [0+0+20 40 0+0+0] [FFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.item> at [0,3920] flex-item [0+0+0 20 0+0+0] [0+0+0 20 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.item> at [0,3940] flex-item [0+0+0 20 0+0+0] [0+0+0 20 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [0,4200] [0+0+0 800 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.cb> at [0,4200] positioned [0+0+0 400 0+0+400] [0+0+0 300 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> at [400,4200] [0+0+0 0 0+0+0] [0+0+0 300 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.anchor> at [350,4200] [0+0+0 50 0+0+0] [0+0+0 50 0+0+250] children: not-inline
+ BlockContainer <(anonymous)> at [350,4200] [0+0+0 0 0+0+0] [0+0+0 300 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ Box <div#columnEndWithAnchorSizedMaxHeightInOtherWritingMode.anchor-sized-root.column> at [0,4200] positioned flex-container(column) [0+0+0 50 0+0+0] [0+0+0 100 0+0+0] [FFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.rigid> at [-150,4200] flex-item [0+0+0 200 0+0+0] [0+0+0 20 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [0,4500] [0+0+0 800 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.cb> at [0,4500] positioned [0+0+0 400 0+0+400] [0+0+0 300 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> at [400,4500] [0+0+0 0 0+0+0] [0+0+0 300 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.anchor> at [350,4500] [0+0+0 50 0+0+0] [0+0+0 50 0+0+250] children: not-inline
+ BlockContainer <(anonymous)> at [350,4500] [0+0+0 0 0+0+0] [0+0+0 300 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ Box <div#columnReverseWithAnchorSizedMaxHeightInOtherWritingMode.anchor-sized-root.column> at [0,4500] positioned flex-container(column-reverse) [0+0+0 50 0+0+0] [0+0+0 100 0+0+0] [FFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.rigid> at [-150,4500] flex-item [0+0+0 200 0+0+0] [0+0+0 20 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [0,4800] [0+0+0 800 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.cb> at [0,4800] positioned [0+0+0 400 0+0+400] [0+0+0 300 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> at [400,4800] [0+0+0 0 0+0+0] [0+0+0 300 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.anchor> at [350,4800] [0+0+0 50 0+0+0] [0+0+0 50 0+0+250] children: not-inline
+ BlockContainer <(anonymous)> at [350,4800] [0+0+0 0 0+0+0] [0+0+0 300 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ Box <div#columnShrinkWithAnchorSizedMaxHeightInOtherWritingMode.anchor-sized-root.column> at [0,4800] positioned flex-container(column) [0+0+0 50 0+0+0] [0+0+0 100 0+0+0] [FFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.shrinkable> at [0,4800] flex-item [0+0+0 25 0+0+0] [0+0+0 20 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.shrinkable> at [25,4800] flex-item [0+0+0 25 0+0+0] [0+0+0 20 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [0,5100] [0+0+0 800 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.cb> at [0,5100] positioned [0+0+0 400 0+0+400] [0+0+0 300 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> at [400,5100] [0+0+0 0 0+0+0] [0+0+0 300 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.anchor> at [350,5100] [0+0+0 50 0+0+0] [0+0+0 50 0+0+250] children: not-inline
+ BlockContainer <(anonymous)> at [350,5100] [0+0+0 0 0+0+0] [0+0+0 300 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ Box <div#rowWrapEndWithAnchorSizedMaxHeightInOtherWritingMode.anchor-sized-root.row> at [0,5100] positioned flex-container(row) [0+0+0 50 0+0+0] [0+0+0 100 0+0+0] [FFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.line> at [-150,5100] flex-item [0+0+0 100 0+0+0] [0+0+0 60 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div.line> at [-50,5100] flex-item [0+0+0 100 0+0+0] [0+0+0 60 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [0,5400] [0+0+0 800 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+
+SC for Viewport<#document> [0,0 800x600] (z-index: auto)
+ SC for BlockContainer<HTML> [0,0 800x5400] (z-index: auto)
diff --git a/Tests/LibWeb/Layout/input/abspos-auto-height-flex-and-grid-roots.html b/Tests/LibWeb/Layout/input/abspos-auto-height-flex-and-grid-roots.html
new file mode 100644
index 00000000000..a370afd9460
--- /dev/null
+++ b/Tests/LibWeb/Layout/input/abspos-auto-height-flex-and-grid-roots.html
@@ -0,0 +1,132 @@
+<!DOCTYPE html>
+<style>
+ body { margin: 0; }
+ .cb { position: relative; width: 400px; height: 300px; }
+ .root { position: absolute; left: 0; top: 0; width: 200px; }
+ .column { display: flex; flex-direction: column; }
+ .row { display: flex; }
+ .grid { display: grid; }
+ .item { width: 20px; height: 20px; }
+ .half { width: 10px; height: 50%; }
+ .anchor { anchor-name: --a; width: 50px; height: 50px; }
+ .anchor-sized-root { position: absolute; left: 0; top: 0; writing-mode: vertical-lr; inline-size: 100px; position-anchor: --a; max-block-size: anchor-size(block); }
+ .rigid { inline-size: 20px; block-size: 200px; flex-shrink: 0; }
+ .shrinkable { inline-size: 20px; block-size: 100px; min-block-size: 0; }
+ .line { inline-size: 60px; block-size: 100px; }
+</style>
+<div class="cb">
+ <div class="root column" id="columnGrow" style="box-sizing: border-box; padding: 7px 3px 5px; border: 2px solid; gap: 10%">
+ <div class="item"></div>
+ <div style="flex-grow: 1"><div class="half"></div></div>
+ <div class="item"></div>
+ </div>
+</div>
+<div class="cb">
+ <div class="root column" id="columnMaxPx" style="box-sizing: border-box; padding: 4px; border: 1px solid; max-height: 30px">
+ <div class="item" style="min-height: 0"></div>
+ <div class="item" style="min-height: 0"></div>
+ </div>
+</div>
+<div class="cb">
+ <div class="root column" id="columnMaxPercent" style="box-sizing: border-box; padding: 6px; max-height: 15%">
+ <div class="item" style="min-height: 0"></div>
+ <div class="item" style="min-height: 0"></div>
+ <div class="item" style="min-height: 0"></div>
+ </div>
+</div>
+<div class="cb">
+ <div class="root row" id="rowWrap" style="flex-wrap: wrap; align-content: stretch; row-gap: 10%; width: 50px">
+ <div class="item"></div>
+ <div class="item" style="height: 35px"></div>
+ <div class="item"></div>
+ <div style="width: 20px"><div class="half"></div></div>
+ </div>
+</div>
+<div class="cb">
+ <div class="root row" id="rowMaxPercent" style="max-height: 10%; padding: 3px">
+ <div class="item" style="height: 50px"></div>
+ <div style="width: 20px"><div class="half"></div></div>
+ </div>
+</div>
+<div class="cb">
+ <div class="root grid" id="gridRows" style="grid-template-rows: 1fr 20% auto 2fr; row-gap: 5%">
+ <div class="item"></div>
+ <div class="item"></div>
+ <div><div class="half"></div></div>
+ <div class="item" style="height: 30px"></div>
+ </div>
+</div>
+<div class="cb">
+ <div class="root grid" id="gridMinHeight" style="min-height: 100px; align-content: center; grid-template-rows: auto auto">
+ <div class="item"></div>
+ <div class="item"></div>
+ </div>
+</div>
+<div class="cb">
+ <div class="root grid" id="gridMinMaxHeight" style="min-height: 40px; max-height: 60px; grid-template-rows: 1fr auto">
+ <div class="item" style="height: 50px"></div>
+ <div class="item" style="height: 30px"></div>
+ </div>
+</div>
+<div class="cb">
+ <div class="root column" id="columnBothInsetsStretch" style="bottom: 0">
+ <div class="item"></div>
+ <div style="flex-grow: 1"><div class="half"></div></div>
+ </div>
+</div>
+<div class="cb">
+ <div class="root column" id="columnBothInsetsEnd" style="bottom: 0; align-self: end">
+ <div class="item"></div>
+ <div style="flex-grow: 1"><div class="half"></div></div>
+ </div>
+</div>
+<div class="cb">
+ <div class="root grid" id="gridBothInsetsStretch" style="bottom: 0; grid-template-rows: 1fr auto">
+ <div class="item"></div>
+ <div><div class="half"></div></div>
+ </div>
+</div>
+<div class="cb">
+ <div class="root grid" id="gridBothInsetsEnd" style="bottom: 0; align-self: end; grid-template-rows: 1fr auto">
+ <div class="item"></div>
+ <div><div class="half"></div></div>
+ </div>
+</div>
+<div class="cb">
+ <div class="root row" id="rowBottomInset" style="top: auto; bottom: 10px">
+ <div class="item"></div>
+ <div class="item" style="height: 40px"></div>
+ </div>
+</div>
+<div class="cb">
+ <div class="root column" id="columnStaticPosition" style="top: auto; padding-top: 5%">
+ <div class="item"></div>
+ <div class="item"></div>
+ </div>
+</div>
+<div class="cb" style="writing-mode: vertical-rl">
+ <div class="anchor"></div>
+ <div class="anchor-sized-root column" id="columnEndWithAnchorSizedMaxHeightInOtherWritingMode" style="justify-content: flex-end">
+ <div class="rigid"></div>
+ </div>
+</div>
+<div class="cb" style="writing-mode: vertical-rl">
+ <div class="anchor"></div>
+ <div class="anchor-sized-root column" id="columnReverseWithAnchorSizedMaxHeightInOtherWritingMode" style="flex-direction: column-reverse">
+ <div class="rigid"></div>
+ </div>
+</div>
+<div class="cb" style="writing-mode: vertical-rl">
+ <div class="anchor"></div>
+ <div class="anchor-sized-root column" id="columnShrinkWithAnchorSizedMaxHeightInOtherWritingMode">
+ <div class="shrinkable"></div>
+ <div class="shrinkable"></div>
+ </div>
+</div>
+<div class="cb" style="writing-mode: vertical-rl">
+ <div class="anchor"></div>
+ <div class="anchor-sized-root row" id="rowWrapEndWithAnchorSizedMaxHeightInOtherWritingMode" style="flex-wrap: wrap; align-content: flex-end">
+ <div class="line"></div>
+ <div class="line"></div>
+ </div>
+</div>
diff --git a/Tests/LibWeb/Text/expected/abspos-flex-and-grid-roots-lay-out-once.txt b/Tests/LibWeb/Text/expected/abspos-flex-and-grid-roots-lay-out-once.txt
new file mode 100644
index 00000000000..1ac9d616e4a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/abspos-flex-and-grid-roots-lay-out-once.txt
@@ -0,0 +1,8 @@
+gridWithFrRow: measurements=0 runs=1 height=60 items=20, 40
+rowFlex: measurements=0 runs=1 height=40 items=20, 40
+columnFlex: measurements=0 runs=1 height=85 items=20, 40
+columnFlexWithMaxHeight: measurements=0 runs=1 height=30 items=10, 20
+rowFlexWithPercentageMaxHeight: measurements=0 runs=1 height=40 items=20, 40
+flexWithMinHeight: measurements=1 runs=2 height=100 items=40
+gridAlignedWithinBothInsets: measurements=1 runs=2 height=40 items=40
+wrappingColumnFlex: measurements=1 runs=2 height=40 items=40
diff --git a/Tests/LibWeb/Text/input/abspos-flex-and-grid-roots-lay-out-once.html b/Tests/LibWeb/Text/input/abspos-flex-and-grid-roots-lay-out-once.html
new file mode 100644
index 00000000000..9dc8cee64dd
--- /dev/null
+++ b/Tests/LibWeb/Text/input/abspos-flex-and-grid-roots-lay-out-once.html
@@ -0,0 +1,80 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<script src="layout-test.js"></script>
+<style>
+ body { margin: 0; }
+ .cb { position: relative; width: 400px; height: 300px; }
+ .cb > div { position: absolute; left: 0; top: 0; width: 200px; }
+ .leaf { width: 20px; height: 20px; }
+</style>
+<div class="cb">
+ <div id="gridWithFrRow" style="display: grid; grid-template-rows: 1fr auto">
+ <div class="leaf"></div>
+ <div class="leaf" id="gridWithFrRowLeaf"></div>
+ </div>
+</div>
+<div class="cb">
+ <div id="rowFlex" style="display: flex">
+ <div class="leaf"></div>
+ <div class="leaf" id="rowFlexLeaf"></div>
+ </div>
+</div>
+<div class="cb">
+ <div id="columnFlex" style="display: flex; flex-direction: column; box-sizing: border-box; padding: 10px; gap: 5px">
+ <div class="leaf"></div>
+ <div style="flex-grow: 1"><div class="leaf" id="columnFlexLeaf"></div></div>
+ </div>
+</div>
+<div class="cb">
+ <div id="columnFlexWithMaxHeight" style="display: flex; flex-direction: column; max-height: 30px">
+ <div class="leaf" style="min-height: 0"></div>
+ <div class="leaf" id="columnFlexWithMaxHeightLeaf" style="min-height: 0"></div>
+ </div>
+</div>
+<div class="cb">
+ <div id="rowFlexWithPercentageMaxHeight" style="display: flex; max-height: 50%">
+ <div class="leaf"></div>
+ <div class="leaf" id="rowFlexWithPercentageMaxHeightLeaf"></div>
+ </div>
+</div>
+<div class="cb">
+ <div id="flexWithMinHeight" style="display: flex; min-height: 100px">
+ <div class="leaf" id="flexWithMinHeightLeaf"></div>
+ </div>
+</div>
+<div class="cb">
+ <div id="gridAlignedWithinBothInsets" style="display: grid; bottom: 0; align-self: start">
+ <div class="leaf" id="gridAlignedWithinBothInsetsLeaf"></div>
+ </div>
+</div>
+<div class="cb">
+ <div id="wrappingColumnFlex" style="display: flex; flex-flow: column wrap">
+ <div class="leaf" id="wrappingColumnFlexLeaf"></div>
+ </div>
+</div>
+<script>
+ test(() => {
+ const output = [];
+ const roots = [
+ gridWithFrRow,
+ rowFlex,
+ columnFlex,
+ columnFlexWithMaxHeight,
+ rowFlexWithPercentageMaxHeight,
+ flexWithMinHeight,
+ gridAlignedWithinBothInsets,
+ wrappingColumnFlex,
+ ];
+ for (const root of roots) {
+ const leaf = document.getElementById(`${root.id}Leaf`);
+ const work = layoutWork(() => (leaf.style.height = "40px"));
+ const isRoot = event => event.box.endsWith(`#${root.id}`);
+ const measurements = work.count(event => event.event === "measure" && isRoot(event) && event.size === "max-content block");
+ const runs = work.count(event => event.event === "run" && isRoot(event) && event.action === "RUN");
+ const heights = [...root.children].map(child => child.getBoundingClientRect().height).join(", ");
+ output.push(`${root.id}: measurements=${measurements} runs=${runs} height=${root.getBoundingClientRect().height} items=${heights}`);
+ }
+ for (const line of output)
+ println(line);
+ });
+</script>