Skip to content

Truly independent web browser

LibWeb: Keep a sized image’s minimum width from its height

sideshowbarkerccc12310bec4
diff --git a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
index 12369048341..9ee9c530ad4 100644
--- a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
@@ -2856,11 +2856,28 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
return false;
}
let available_space = self.transferred_intrinsic_inline_sizing_space(sized, constraints);
- // NB: A cyclic percentage width behaves as auto for the box's intrinsic sizes, whether or not it's replaced.
- self.should_treat_inline_size_as_auto(sized, available_space)
+ // NB: A replaced box's intrinsic widths come from its natural sizes and its block axis constraints, whatever its
+ // own width. A cyclic percentage width of a non-replaced box behaves as auto for its intrinsic sizes.
+ (facts.is_replaced_box() || self.should_treat_inline_size_as_auto(sized, available_space))
&& !self.should_treat_block_size_as_auto(sized, available_space, constraints)
}
+ /// The inline size that a replaced box whose width doesn't behave as auto would have if it did: its block size
+ /// through its aspect ratio.
+ // https://drafts.csswg.org/css-sizing-3/#intrinsic-sizes
+ // When the box has a preferred aspect ratio, size constraints in the opposite dimension will transfer through and
+ // can affect the auto size in the considered one.
+ // NB: The box's own minimum and maximum widths limit its intrinsic size contributions, not its intrinsic sizes.
+ fn transferred_inline_size_ignoring_width(
+ &self,
+ sized: SizedBox<'_>,
+ available_space: AvailableSpace,
+ constraints: ContainingBlockConstraints,
+ ) -> CssPixels {
+ let block_size = self.compute_block_size_for_replaced_element(sized, available_space, constraints);
+ self.content_inline_size_from_aspect_ratio(sized, block_size)
+ }
+
fn calculate_transferred_inline_size_for_replaced_element(
&self,
sized: SizedBox<'_>,
@@ -2872,7 +2889,19 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
let available_space = self.transferred_intrinsic_inline_sizing_space(sized, constraints);
// https://drafts.csswg.org/css2/#inline-replaced-width
// "(used height) * (intrinsic ratio)"
- let inline_size = self.compute_inline_size_for_replaced_element(sized, available_space, constraints);
+ let inline_size = if self.should_treat_inline_size_as_auto(sized, available_space) {
+ self.compute_inline_size_for_replaced_element(sized, available_space, constraints)
+ } else {
+ // NB: A block size that behaves as auto here, such as a stretch block size against an auto height, has no
+ // size to transfer, so the box's natural sizes give its intrinsic widths. And a width that isn't a
+ // length or percentage, such as a calc-size() of an intrinsic size, resolves through those very widths.
+ let (_, computed_block) = self.computed_sizes_for_replaced_element(sized, available_space, constraints);
+ let width = self.style(sized.node).width();
+ if computed_block.is_auto() || !width.is_length_percentage() || width.calc_size().is_some() {
+ return None;
+ }
+ self.transferred_inline_size_ignoring_width(sized, available_space, constraints)
+ };
// https://drafts.csswg.org/css-sizing-4/#aspect-ratio-minimum
let block_size = self.compute_block_size_for_replaced_element(sized, available_space, constraints);
let minimum = self.automatic_minimum_size_from_aspect_ratio(
diff --git a/Tests/LibWeb/Text/expected/Layout/flex-replaced-item-automatic-minimum-width-follows-height.txt b/Tests/LibWeb/Text/expected/Layout/flex-replaced-item-automatic-minimum-width-follows-height.txt
new file mode 100644
index 00000000000..119ebd7173c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Layout/flex-replaced-item-automatic-minimum-width-follows-height.txt
@@ -0,0 +1,9 @@
+square: 60x60
+wide: 60x60
+tall: 40x60
+tall-max-width: 30x60
+tall-height-only: 40x60
+tall-aspect-ratio: 30x60
+tall-border-box: 43.328125x60
+larger-natural-size: 60x60
+column-wide: 60x40
diff --git a/Tests/LibWeb/Text/input/Layout/flex-replaced-item-automatic-minimum-width-follows-height.html b/Tests/LibWeb/Text/input/Layout/flex-replaced-item-automatic-minimum-width-follows-height.html
new file mode 100644
index 00000000000..087d43eb907
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Layout/flex-replaced-item-automatic-minimum-width-follows-height.html
@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body { margin: 0; }
+ .row { display: flex; width: 100px; margin-bottom: 4px; font: 16px monospace; }
+</style>
+<!-- The automatic minimum width of an image in a flex container is its min-content width, which is its definite height
+ through its aspect ratio, whatever its own width. So it doesn't shrink to a smaller natural width. -->
+<div class="row"><img id="square" style="width: 60px; height: 60px" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='1' height='1'/%3E"><div>xxxxxxxx</div></div>
+<div class="row"><img id="wide" style="width: 60px; height: 60px" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='30' height='20'/%3E"><div>xxxxxxxx</div></div>
+<div class="row"><img id="tall" style="width: 60px; height: 60px" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='30'/%3E"><div>xxxxxxxx</div></div>
+<div class="row"><img id="tall-max-width" style="width: 60px; height: 60px; max-width: 30px" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='30'/%3E"><div>xxxxxxxx</div></div>
+<div class="row"><img id="tall-height-only" style="height: 60px" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='30'/%3E"><div>xxxxxxxx</div></div>
+<div class="row"><img id="tall-aspect-ratio" style="width: 60px; height: 60px; aspect-ratio: 1 / 2" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='30'/%3E"><div>xxxxxxxx</div></div>
+<div class="row"><img id="tall-border-box" style="width: 60px; height: 60px; padding: 5px; box-sizing: border-box" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='30'/%3E"><div>xxxxxxxx</div></div>
+<div class="row"><img id="larger-natural-size" style="width: 60px; height: 60px" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='200'/%3E"><div>xxxxxxxx</div></div>
+<div class="row" style="flex-direction: column; height: 30px"><img id="column-wide" style="width: 60px; height: 60px" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='30' height='20'/%3E"><div>x</div></div>
+<script>
+ function run(report) {
+ for (const element of document.querySelectorAll("img")) {
+ 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>