Skip to content

Truly independent web browser

LibWeb: Grow an auto grid row to an image with a percentage height

sideshowbarker8916f8f34e24
diff --git a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
index 257f4facdc6..7ac51ad6004 100644
--- a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
@@ -2660,6 +2660,13 @@ impl<'pass> GridFormattingContext<'pass> {
/// INTEROP: Like other engines, we resolve them against the item's grid area in a subgrid without the extra margins
/// the subgrid gives it, which is the part of the area that lies between the subgrid's own lines.
fn percentage_basis_size(&self, item: GridItem, axis: Axis) -> CssPixels {
+ CssPixels::default()
+ .max(self.containing_block_size(item, axis) - self.subgrid_edge_extra_margins_of(item, axis))
+ }
+
+ /// The extra margins that stand in for the edges of a subgrid around `item`, which its grid area includes but its
+ /// percentage basis doesn't.
+ fn subgrid_edge_extra_margins_of(&self, item: GridItem, axis: Axis) -> CssPixels {
let mut extra_margins = axis.select(
item.extra_margin_left + item.extra_margin_right,
item.extra_margin_top + item.extra_margin_bottom,
@@ -2675,7 +2682,7 @@ impl<'pass> GridFormattingContext<'pass> {
extra_margins += gap_extra_margin;
}
}
- CssPixels::default().max(self.containing_block_size(item, axis) - extra_margins)
+ extra_margins
}
fn item_available_space(&self, item: GridItem) -> AvailableSpace {
@@ -2758,15 +2765,16 @@ impl<'pass> GridFormattingContext<'pass> {
mut constraints: ContainingBlockConstraints,
) -> ContainingBlockConstraints {
let facts = self.item_facts(item);
+ // NB: A percentage height of an item with a preferred aspect ratio, replaced or not, resolves against its grid
+ // area, not the grid container, so its row's size is its basis here.
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))
+ || (facts.has_preferred_aspect_ratio() && !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))
+ Some(self.percentage_basis_size(item, Axis::Row))
} else {
self.fixed_track_span_size(item, Axis::Row)
+ .map(|size| CssPixels::default().max(size - self.subgrid_edge_extra_margins_of(item, Axis::Row)))
};
}
constraints.quirks_mode_percentage_basis_block_size =
@@ -3343,8 +3351,15 @@ impl<'pass> GridFormattingContext<'pass> {
// the size suggestion is capped by those sizes; for this purpose, any indefinite percentages in these sizes are resolved
// against zero (and considered definite).
// FIXME: "compressible replaced element" includes more elements than is_replaced_box().
+ // AD-HOC: Only in the inline axis. A replaced box's percentage block size with nothing to resolve against
+ // behaves as auto instead, as it does for its min-content contribution, for web compatibility. So an
+ // image with height: 100% in an auto row keeps its content size as its minimum, and the row grows to
+ // fit it, as in other engines.
let preferred_size = self.preferred_size(item, axis);
- if self.item_facts(item).is_replaced_box() && (preferred_size.is_percentage() || maximum_size.is_percentage()) {
+ if self.item_facts(item).is_replaced_box()
+ && axis.is_column()
+ && (preferred_size.is_percentage() || maximum_size.is_percentage())
+ {
// NOTE: Implements "for this purpose, any indefinite percentages in these sizes are resolved
// against zero (and considered definite)." part.
result = CssPixels::default();
@@ -4706,6 +4721,17 @@ impl<'pass> GridFormattingContext<'pass> {
}
} else if use_replaced {
if axis.is_column() {
+ // NB: The rows aren't sized yet when the column pass resolves the item's width, so a percentage height
+ // resolves against the block size the column pass assumes for the item's rows, as its column
+ // contributions did, rather than against rows that are still empty.
+ let available = AvailableSpace {
+ block_size: constraints
+ .percentage_basis_block_size
+ .map_or(AvailableSize::Indefinite, |basis| {
+ AvailableSize::definite(clamp_to_max_dimension_value(basis))
+ }),
+ ..available
+ };
self.sizing().compute_inline_size_for_replaced_element(
sizing_context::SizedBox::new(item.box_, &self.cells_of(item)),
available,
diff --git a/Tests/LibWeb/Text/expected/Layout/grid-auto-row-grows-to-image-with-percentage-height.txt b/Tests/LibWeb/Text/expected/Layout/grid-auto-row-grows-to-image-with-percentage-height.txt
new file mode 100644
index 00000000000..06e6294f484
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Layout/grid-auto-row-grows-to-image-with-percentage-height.txt
@@ -0,0 +1,9 @@
+width-and-height: 100x150, row 150px
+height-only: 300x450, row 450px
+half-height: 150x225, row 450px
+fixed-row: 66.65625x100, row 100px
+without-natural-size: 100x150, row 150px
+centered: 100x150, row 150px
+min-content-row: 300x450, row 450px
+flexible-row: 300x450, row 450px
+auto-height-grid: 100x150, row 150px
diff --git a/Tests/LibWeb/Text/input/Layout/grid-auto-row-grows-to-image-with-percentage-height.html b/Tests/LibWeb/Text/input/Layout/grid-auto-row-grows-to-image-with-percentage-height.html
new file mode 100644
index 00000000000..3e1912f4bb6
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Layout/grid-auto-row-grows-to-image-with-percentage-height.html
@@ -0,0 +1,32 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body { margin: 0; }
+ .grid { display: grid; width: 100px; height: 100px; margin-bottom: 4px; }
+ img { display: block; }
+</style>
+<!-- An image's percentage height in a grid resolves against its grid area, so it behaves as auto while an auto row is
+ sized, and the row grows to fit the image. The image's width only follows that height once the row is sized. -->
+<div class="grid"><img id="width-and-height" style="width: 100%; height: 100%" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='450'/%3E"></div>
+<div class="grid"><img id="height-only" style="height: 100%" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='450'/%3E"></div>
+<div class="grid"><img id="half-height" style="height: 50%" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='450'/%3E"></div>
+<div class="grid" style="grid-template-rows: 100px"><img id="fixed-row" style="height: 100%" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='450'/%3E"></div>
+<div class="grid"><img id="without-natural-size" style="width: 100%; height: 100%" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 300 450'/%3E"></div>
+<div class="grid" style="place-items: center; overflow: hidden"><img id="centered" style="width: 100%; height: 100%; object-fit: cover" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='450'/%3E"></div>
+<div class="grid" style="grid-template-rows: min-content"><img id="min-content-row" style="height: 100%" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='450'/%3E"></div>
+<div class="grid" style="grid-template-rows: 1fr"><img id="flexible-row" style="height: 100%" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='450'/%3E"></div>
+<div class="grid" style="height: auto"><img id="auto-height-grid" style="width: 100%; height: 100%" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='450'/%3E"></div>
+<script>
+ function run(report) {
+ for (const element of document.querySelectorAll("[id]:not(#out)")) {
+ const rect = element.getBoundingClientRect();
+ report(`${element.id}: ${rect.width}x${rect.height}, row ${getComputedStyle(element.parentElement).gridTemplateRows}`);
+ }
+ }
+</script>
+<script>
+ if (window.asyncTest)
+ asyncTest(done => addEventListener("load", () => { run(println); done(); }));
+ else
+ addEventListener("load", () => { run(line => console.log(line)); window.engineCompareDone = true; });
+</script>