Skip to content

Truly independent web browser

LibWeb: Let a grid image’s min-height give its minimum contribution

sideshowbarker7915186b236c
diff --git a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
index 7ac51ad6004..61fc2598aa4 100644
--- a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
@@ -3263,7 +3263,16 @@ impl<'pass> GridFormattingContext<'pass> {
}
let opposite = axis.opposite();
let available = self.item_available_space(item);
- let constraints = self.track_sizing_constraints();
+ // NB: The columns are sized before the rows, so while the rows are sized, a percentage of the item's width
+ // resolves against its grid area, and its percentage minimum and maximum widths are definite.
+ let opposite_axis_is_sized = !axis.is_column();
+ let constraints = if opposite_axis_is_sized {
+ self.grid_area_constraints(item)
+ } else {
+ self.track_sizing_constraints()
+ };
+ let limit_is_definite =
+ |size: &ComputedSize| size.is_length() || (opposite_axis_is_sized && size.is_length_percentage());
let inner_size = |property: SizingProperty| {
self.sizing().calculate_inner_size_for_property(
sizing_context::SizedBox::new(item.box_, &self.cells_of(item)),
@@ -3274,12 +3283,12 @@ impl<'pass> GridFormattingContext<'pass> {
)
};
let mut opposite_axis_size = inner_size(opposite.select(SizingProperty::Width, SizingProperty::Height));
- if self.maximum_size(item, opposite).is_length() {
+ if limit_is_definite(self.maximum_size(item, opposite)) {
opposite_axis_size = opposite_axis_size.min(inner_size(
opposite.select(SizingProperty::MaxWidth, SizingProperty::MaxHeight),
));
}
- if self.minimum_size(item, opposite).is_length() {
+ if limit_is_definite(self.minimum_size(item, opposite)) {
opposite_axis_size = opposite_axis_size.max(inner_size(
opposite.select(SizingProperty::MinWidth, SizingProperty::MinHeight),
));
@@ -3416,11 +3425,11 @@ impl<'pass> GridFormattingContext<'pass> {
// result from assuming the item’s used minimum size as its preferred size; else the item’s minimum
// contribution is its min-content contribution. Because the minimum contribution often depends on
// the size of the item’s content, it is considered a type of intrinsic size contribution.
- // NB: The height of a non-replaced item with a preferred aspect ratio is reported as not auto because it
+ // NB: The height of an item with a preferred aspect ratio, replaced or not, is reported as not auto because it
// transfers from the item's width, but it still behaves as auto here.
let facts = self.item_facts(item);
let automatic_size_with_aspect_ratio =
- self.preferred_size(item, axis).is_auto() && facts.has_preferred_aspect_ratio() && !facts.is_replaced_box();
+ self.preferred_size(item, axis).is_auto() && facts.has_preferred_aspect_ratio();
if !preferred_behaves_as_auto && !automatic_size_with_aspect_ratio {
return min_content_contribution();
}
diff --git a/Tests/LibWeb/Text/expected/Layout/grid-replaced-item-auto-height-minimum-contribution.txt b/Tests/LibWeb/Text/expected/Layout/grid-replaced-item-auto-height-minimum-contribution.txt
new file mode 100644
index 00000000000..0058f4c2a8f
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Layout/grid-replaced-item-auto-height-minimum-contribution.txt
@@ -0,0 +1,6 @@
+aspect-ratio-grid: 276x207, rows 207px
+fixed-height-grid: 276x207, rows 207px
+fixed-height-grid-start-aligned: 69x207, rows 207px
+fixed-height-grid-half-max-height: 276x103.5, rows 207px
+fixed-height-grid-fixed-max-height: 276x150, rows 207px
+fixed-height-grid-two-rows: 276x187, rows 187px 20px
diff --git a/Tests/LibWeb/Text/input/Layout/grid-replaced-item-auto-height-minimum-contribution.html b/Tests/LibWeb/Text/input/Layout/grid-replaced-item-auto-height-minimum-contribution.html
new file mode 100644
index 00000000000..073676bda81
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Layout/grid-replaced-item-auto-height-minimum-contribution.html
@@ -0,0 +1,30 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body { margin: 0; }
+ .grid { display: grid; width: 276px; margin-bottom: 4px; overflow: hidden; }
+ .grid > img { grid-area: 1 / -1; place-self: stretch; min-width: 0; min-height: 0; max-width: 100%; max-height: 100%; object-fit: cover; }
+</style>
+<!-- An image with an automatic height takes its height from its width through its aspect ratio, but that height still
+ behaves as auto for its minimum contribution, which its min-height then gives. So with min-height: 0, the row grows
+ only into the grid container's height, and max-height: 100% then limits the image to that row. -->
+<div class="grid" style="aspect-ratio: 4 / 3"><img id="aspect-ratio-grid" style="" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100' height='300'/%3E"></div>
+<div class="grid" style="height: 207px"><img id="fixed-height-grid" style="" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100' height='300'/%3E"></div>
+<div class="grid" style="height: 207px"><img id="fixed-height-grid-start-aligned" style="place-self: start" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100' height='300'/%3E"></div>
+<div class="grid" style="height: 207px"><img id="fixed-height-grid-half-max-height" style="max-height: 50%" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100' height='300'/%3E"></div>
+<div class="grid" style="height: 207px"><img id="fixed-height-grid-fixed-max-height" style="max-height: 150px" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100' height='300'/%3E"></div>
+<div class="grid" style="height: 207px; grid-template-rows: auto auto"><img id="fixed-height-grid-two-rows" style="grid-area: auto" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100' height='300'/%3E"><div style="height: 20px"></div></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}, rows ${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>