Skip to content

Truly independent web browser

LibWeb: Give a replaced box a min-content height from aspect-ratio

sideshowbarker129344ee1804
diff --git a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
index 67c233c0673..12369048341 100644
--- a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
@@ -3741,10 +3741,13 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
IntrinsicSizeCacheKind::MinContentInline,
);
}
- if facts.is_replaced_box() {
+ // https://drafts.csswg.org/css-sizing-3/#intrinsic-sizes
+ // NB: A replaced box's min-content block size is its max-content block size, which follows its preferred
+ // aspect ratio where it has one, rather than its natural ratio.
+ if facts.is_replaced_box() && !facts.has_preferred_aspect_ratio() {
let auto_size = self.auto_content_size(node);
if let Some(height) = auto_size.height {
- return auto_size.aspect_ratio.map_or(height, |ratio| ratio.divide(inline_size));
+ return height;
}
}
// For block containers, tables, and inline boxes, this is equivalent to the max-content block size.
diff --git a/Tests/LibWeb/Text/expected/Layout/flex-replaced-item-automatic-minimum-height-follows-aspect-ratio.txt b/Tests/LibWeb/Text/expected/Layout/flex-replaced-item-automatic-minimum-height-follows-aspect-ratio.txt
new file mode 100644
index 00000000000..9e498d9ea26
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Layout/flex-replaced-item-automatic-minimum-height-follows-aspect-ratio.txt
@@ -0,0 +1,9 @@
+aspect-ratio: 300x225
+aspect-ratio-min-height-zero: 300x128.578125
+natural-ratio: 300x75
+auto-and-ratio: 300x75
+aspect-ratio-without-natural-size: 300x225
+aspect-ratio-fixed-width: 200x150
+aspect-ratio-max-height: 300x200
+square-aspect-ratio: 300x300
+aspect-ratio-fixed-height: 300x225
diff --git a/Tests/LibWeb/Text/input/Layout/flex-replaced-item-automatic-minimum-height-follows-aspect-ratio.html b/Tests/LibWeb/Text/input/Layout/flex-replaced-item-automatic-minimum-height-follows-aspect-ratio.html
new file mode 100644
index 00000000000..d031217e48d
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Layout/flex-replaced-item-automatic-minimum-height-follows-aspect-ratio.html
@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body { margin: 0; }
+ .card { display: inline-flex; flex-direction: column; width: 300px; height: 300px; vertical-align: top; }
+ .card > img { display: block; }
+ .rest { height: 100%; }
+</style>
+<!-- The automatic minimum height of an image in a column flex container is its min-content height, which follows its
+ aspect-ratio rather than its natural ratio. So it doesn't shrink below the height its width and aspect-ratio give. -->
+<div class="card"><img id="aspect-ratio" style="aspect-ratio: 4 / 3" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='100'/%3E"><div class="rest"></div></div>
+<div class="card"><img id="aspect-ratio-min-height-zero" style="aspect-ratio: 4 / 3; min-height: 0" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='100'/%3E"><div class="rest"></div></div>
+<div class="card"><img id="natural-ratio" style="" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='100'/%3E"><div class="rest"></div></div>
+<div class="card"><img id="auto-and-ratio" style="aspect-ratio: auto 4 / 3" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='100'/%3E"><div class="rest"></div></div>
+<div class="card"><img id="aspect-ratio-without-natural-size" style="aspect-ratio: 4 / 3" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 400 100'/%3E"><div class="rest"></div></div>
+<div class="card"><img id="aspect-ratio-fixed-width" style="aspect-ratio: 4 / 3; width: 200px" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='100'/%3E"><div class="rest"></div></div>
+<div class="card"><img id="aspect-ratio-max-height" style="aspect-ratio: 4 / 3; max-height: 200px" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='100'/%3E"><div class="rest"></div></div>
+<div class="card"><img id="square-aspect-ratio" style="aspect-ratio: 1" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='100'/%3E"><div class="rest"></div></div>
+<div class="card"><img id="aspect-ratio-fixed-height" style="aspect-ratio: 4 / 3; height: 250px" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='100'/%3E"><div class="rest"></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}`);
+ }
+ }
+</script>
+<script>
+ if (window.asyncTest)
+ asyncTest(done => addEventListener("load", () => { run(println); done(); }));
+ else
+ addEventListener("load", () => { run(line => console.log(line)); window.engineCompareDone = true; });
+</script>