Skip to content

Truly independent web browser

LibWeb: Transfer a grid item's definite height through its aspect ratio

Andreas Kling56f9461b3d6d
diff --git a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
index 6c96348e02f..2f717674b37 100644
--- a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
@@ -2677,7 +2677,12 @@ impl<'pass> GridFormattingContext<'pass> {
item: GridItem,
mut constraints: ContainingBlockConstraints,
) -> ContainingBlockConstraints {
- if constraints.percentage_basis_block_size.is_none() {
+ let facts = self.item_facts(item);
+ if constraints.percentage_basis_block_size.is_none()
+ || (facts.has_preferred_aspect_ratio()
+ && !facts.is_replaced_box()
+ && !self.is_grid_lanes_stacking_axis(Axis::Row))
+ {
constraints.percentage_basis_block_size = if self.use_row_sizes_for_column_percentages {
Some(self.containing_block_size(item, Axis::Row))
} else {
@@ -2703,7 +2708,8 @@ impl<'pass> GridFormattingContext<'pass> {
|| ((style.height().contains_percentage()
|| style.min_height().contains_percentage()
|| style.max_height().contains_percentage())
- && self.fixed_track_span_size(item, Axis::Row).is_none())
+ && (self.fixed_track_span_size(item, Axis::Row).is_none()
+ || self.item_spans_percentage_row_of_indefinite_grid(item)))
})
.collect()
}
@@ -4109,8 +4115,21 @@ impl<'pass> GridFormattingContext<'pass> {
// element), the grid item is sized as for 'align-self: stretch'.
// Otherwise, the grid item is sized consistent with the size calculation rules for block-level elements for
// the corresponding axis.
- // NB: The automatic inline size of a block-level element is its stretch-fit size, with an aspect ratio too.
- axis.is_column() || !self.item_facts(item).has_preferred_aspect_ratio()
+ // NB: The automatic inline size of a block-level element is its stretch-fit size, unless it has an aspect ratio
+ // and a definite block size to transfer through it. A percentage block size becomes definite with its row.
+ if !self.item_facts(item).has_preferred_aspect_ratio() {
+ return true;
+ }
+ if !axis.is_column() {
+ return false;
+ }
+ let height = self.item_style(item).height();
+ let block_size_is_definite = !height.is_auto()
+ && !height.is_intrinsic_sizing_constraint()
+ && (!height.contains_percentage()
+ || self.use_row_sizes_for_column_percentages
+ || self.fixed_track_span_size(item, Axis::Row).is_some());
+ !block_size_is_definite
}
fn item_alignment(&self, item: GridItem, axis: Axis) -> Alignment {
diff --git a/Tests/LibWeb/Layout/expected/grid/aspect-ratio-item-normal-alignment.txt b/Tests/LibWeb/Layout/expected/grid/aspect-ratio-item-normal-alignment.txt
index 046b95f503f..7d4499e6d1c 100644
--- a/Tests/LibWeb/Layout/expected/grid/aspect-ratio-item-normal-alignment.txt
+++ b/Tests/LibWeb/Layout/expected/grid/aspect-ratio-item-normal-alignment.txt
@@ -1,6 +1,6 @@
-Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline
- BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 326 0+0+0] [BFC] children: not-inline
- BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 310 0+0+8] children: not-inline
+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 800x626]
+ BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 626 0+0+0] [BFC] children: not-inline
+ BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 610 0+0+8] children: not-inline
Box <div.grid> at [8,8] [0+0+0 300 0+0+484] [0+0+0 50 0+0+0] [GFC] children: not-inline
BlockContainer <div.item> at [8,8] [0+0+0 300 0+0+0] [0+0+0 150 0+0+0] [BFC] children: not-inline
Box <div.grid> at [8,58] [0+0+0 300 0+0+484] [0+0+0 60 0+0+0] [GFC] children: not-inline
@@ -17,6 +17,13 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
Box <div.grid> at [8,258] [0+0+0 300 0+0+484] [0+0+0 60 0+0+0] [GFC] children: not-inline
BlockContainer <div.item> at [8,258] [0+0+0 120 0+0+0] [0+0+0 60 0+0+0] [BFC] children: not-inline
BlockContainer <div.content> at [8,258] [0+0+0 60 0+0+60] [0+0+0 60 0+0+0] children: not-inline
+ Box <div.grid> at [8,318] [0+0+0 300 0+0+484] [0+0+0 50 0+0+0] [GFC] children: not-inline
+ BlockContainer <div.item> at [8,318] [0+0+0 300 0+0+0] [0+0+0 200 0+0+0] [BFC] children: not-inline
+ BlockContainer <div.content> at [8,318] [0+0+0 60 0+0+240] [0+0+0 200 0+0+0] children: not-inline
+ Box <div.grid> at [8,368] [0+0+0 300 0+0+484] [0+0+0 100 0+0+0] [GFC] children: not-inline
+ BlockContainer <div.item> at [8,368] [0+0+0 40 0+0+0] [0+0+0 20 0+0+0] [BFC] children: not-inline
+ Box <div.grid> at [8,468] [0+0+0 300 0+0+484] [0+0+0 150 0+0+0] [GFC] children: not-inline
+ BlockContainer <div.item> at [8,468] [0+0+0 300 0+0+0] [0+0+0 150 0+0+0] [BFC] children: not-inline
SC for Viewport<#document> [0,0 800x600] (z-index: auto)
- SC for BlockContainer<HTML> [0,0 800x326] (z-index: auto)
+ SC for BlockContainer<HTML> [0,0 800x626] (z-index: auto)
diff --git a/Tests/LibWeb/Layout/expected/grid/aspect-ratio-item-percentage-height.txt b/Tests/LibWeb/Layout/expected/grid/aspect-ratio-item-percentage-height.txt
new file mode 100644
index 00000000000..e862dd41820
--- /dev/null
+++ b/Tests/LibWeb/Layout/expected/grid/aspect-ratio-item-percentage-height.txt
@@ -0,0 +1,15 @@
+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 800x1066]
+ BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 1066 0+0+0] [BFC] children: not-inline
+ BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 1050 0+0+8] children: not-inline
+ Box <div.grid.definite> at [8,8] [0+0+0 300 0+0+484] [0+0+0 300 0+0+0] [GFC] children: not-inline
+ BlockContainer <div.item> at [8,8] [0+0+0 100 0+0+0] [0+0+0 50 0+0+0] [BFC] children: not-inline
+ Box <div.grid.definite> at [8,308] [0+0+0 300 0+0+484] [0+0+0 300 0+0+0] [GFC] children: not-inline
+ BlockContainer <div.item> at [8,308] [0+0+0 60 0+0+0] [0+0+0 30 0+0+0] [BFC] children: not-inline overflow: [8,308 60x60]
+ BlockContainer <div.content> at [8,308] [0+0+0 60 0+0+0] [0+0+0 60 0+0+0] children: not-inline
+ Box <div.grid.definite> at [8,608] [0+0+0 300 0+0+484] [0+0+0 300 0+0+0] [GFC] children: not-inline
+ BlockContainer <div.item> at [8,608] [0+0+0 600 0+0+0] [0+0+0 300 0+0+0] [BFC] children: not-inline
+ Box <div.grid> at [8,908] [0+0+0 300 0+0+484] [0+0+0 150 0+0+0] [GFC] children: not-inline
+ BlockContainer <div.item> at [8,908] [0+0+0 60 0+0+0] [0+0+0 30 0+0+0] [BFC] children: not-inline
+
+SC for Viewport<#document> [0,0 800x600] (z-index: auto)
+ SC for BlockContainer<HTML> [0,0 800x1066] (z-index: auto)
diff --git a/Tests/LibWeb/Layout/expected/grid/content-based-minimum-size-suggestions.txt b/Tests/LibWeb/Layout/expected/grid/content-based-minimum-size-suggestions.txt
index 4d1a8fb3f50..ec6294c915e 100644
--- a/Tests/LibWeb/Layout/expected/grid/content-based-minimum-size-suggestions.txt
+++ b/Tests/LibWeb/Layout/expected/grid/content-based-minimum-size-suggestions.txt
@@ -2,7 +2,7 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 26 0+0+0] [BFC] children: not-inline
BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 10 0+0+8] children: not-inline
Box <div.grid> at [8,8] [0+0+0 10 0+0+774] [0+0+0 10 0+0+0] [GFC] children: not-inline
- BlockContainer <div.item> at [8,8] [0+0+0 80 0+0+0] [0+0+0 40 0+0+0] [BFC] children: inline
+ BlockContainer <div.item> at [8,8] [0+0+0 115.625 0+0+0] [0+0+0 40 0+0+0] [BFC] children: inline
frag 0 from TextNode start: 0, length: 10, rect: [8,8 115.625x16] baseline: 13
"XXXXXXXXXX"
TextNode <#text> (not painted)
diff --git a/Tests/LibWeb/Layout/input/grid/aspect-ratio-item-normal-alignment.html b/Tests/LibWeb/Layout/input/grid/aspect-ratio-item-normal-alignment.html
index f7b2a5c1553..4224ba1e27b 100644
--- a/Tests/LibWeb/Layout/input/grid/aspect-ratio-item-normal-alignment.html
+++ b/Tests/LibWeb/Layout/input/grid/aspect-ratio-item-normal-alignment.html
@@ -25,3 +25,9 @@
<div class="grid" style="grid-template-columns: min-content; grid-template-rows: 0fr; height: 40px"><div class="item" style="overflow: hidden"><div class="content"></div></div></div>
<!-- A percentage of an indefinite size does not limit the item's content-based minimum. -->
<div class="grid" style="grid-template-columns: min-content; grid-template-rows: 20%"><div class="item"><div class="content"></div></div></div>
+<!-- A fixed maximum of the row limits the content-based minimum. -->
+<div class="grid" style="grid-template-rows: minmax(auto, 50px)"><div class="item"><div class="content" style="height: 200px"></div></div></div>
+<!-- The transferred maximum limits the column sized from the row. -->
+<div class="grid" style="grid-template-columns: min-content; grid-template-rows: 100px"><div class="item" style="max-height: 20px; overflow: hidden"></div></div>
+<!-- An intrinsic height is not definite, so the item still fills its column. -->
+<div class="grid"><div class="item" style="height: max-content"></div></div>
diff --git a/Tests/LibWeb/Layout/input/grid/aspect-ratio-item-percentage-height.html b/Tests/LibWeb/Layout/input/grid/aspect-ratio-item-percentage-height.html
new file mode 100644
index 00000000000..babaddb3198
--- /dev/null
+++ b/Tests/LibWeb/Layout/input/grid/aspect-ratio-item-percentage-height.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<style>
+.grid {
+ display: grid;
+ width: 300px;
+}
+.definite {
+ height: 300px;
+}
+.item {
+ aspect-ratio: 2;
+ height: 100%;
+ overflow: hidden;
+ background: green;
+}
+.content {
+ width: 60px;
+ height: 60px;
+}
+</style>
+<!-- The width transfers from the percentage height resolved against a fixed row. -->
+<div class="grid definite" style="grid-template-columns: 1fr; grid-template-rows: 50px"><div class="item"></div></div>
+<!-- A percentage does not resolve against the grid container while the columns are sized. -->
+<div class="grid definite" style="grid-template-columns: min-content; grid-template-rows: min-content"><div class="item"><div class="content"></div></div></div>
+<!-- The columns are sized again once the row is known. -->
+<div class="grid definite" style="grid-template-columns: 1fr; grid-template-rows: 1fr"><div class="item"></div></div>
+<!-- A percentage row of an indefinite grid resolves only after the rows are sized. -->
+<div class="grid" style="grid-template-columns: 1fr; grid-template-rows: 20%"><div class="item"></div></div>