Skip to content

Truly independent web browser

LibWeb: Size a wrapping column’s images in its widest item’s width

sideshowbarker2a888e0ee1e3
diff --git a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
index 71f282e84a2..16031691cbc 100644
--- a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
@@ -4094,6 +4094,21 @@ impl<'pass> FlexFormattingContext<'pass> {
return self.calculate_intrinsic_main_size_of_flex_container();
}
if !self.is_single_line() && layout_input.available_space.inline_size == AvailableSize::MaxContent {
+ // https://drafts.csswg.org/css-flexbox-1/#intrinsic-cross-sizes
+ // NB: An item with an aspect ratio has a main size that depends on its width. So size the items in the width of
+ // the widest item's max-content contribution, rather than under a max-content constraint, which would leave
+ // such an item without a width.
+ if (0..self.flex_items.len())
+ .any(|index| self.flex_item_details[index].inputs.preferred_aspect_ratio.is_some())
+ {
+ let widest = (0..self.flex_items.len())
+ .map(|index| self.calculate_cross_content_contribution(index, true, true))
+ .max()
+ .unwrap_or_default();
+ let mut space = layout_input.available_space;
+ space.inline_size = AvailableSize::Definite(widest);
+ self.determine_available_space_for_items(space);
+ }
self.determine_flex_base_sizes_and_hypothetical_main_sizes();
self.collect_flex_items_into_flex_lines();
self.resolve_flexible_lengths();
diff --git a/Tests/LibWeb/Text/expected/flex/column-wrap-flex-container-max-width-image.txt b/Tests/LibWeb/Text/expected/flex/column-wrap-flex-container-max-width-image.txt
new file mode 100644
index 00000000000..c97fec05230
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/flex/column-wrap-flex-container-max-width-image.txt
@@ -0,0 +1,2 @@
+image in a column wrap flex container: 730x90
+image in a column nowrap flex container: 730x90
diff --git a/Tests/LibWeb/Text/input/flex/column-wrap-flex-container-max-width-image.html b/Tests/LibWeb/Text/input/flex/column-wrap-flex-container-max-width-image.html
new file mode 100644
index 00000000000..acd7bef1c40
--- /dev/null
+++ b/Tests/LibWeb/Text/input/flex/column-wrap-flex-container-max-width-image.html
@@ -0,0 +1,35 @@
+<!DOCTYPE html>
+<style>
+ .row {
+ display: flex;
+ }
+ .wrap {
+ display: flex;
+ flex-flow: column wrap;
+ align-items: center;
+ }
+ .nowrap {
+ display: flex;
+ flex-flow: column nowrap;
+ align-items: center;
+ }
+ img {
+ max-width: 100%;
+ }
+</style>
+<div class="row"><a class="wrap"><img id="wrap" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='730' height='90'%3E%3Crect width='730' height='90' fill='crimson'/%3E%3C/svg%3E"></a></div>
+<div class="row"><a class="nowrap"><img id="nowrap" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='730' height='90'%3E%3Crect width='730' height='90' fill='crimson'/%3E%3C/svg%3E"></a></div>
+<script src="../include.js"></script>
+<script>
+ // A shrink-to-fit column flex container gives an image with max-width: 100% its natural size, whether it wraps
+ // or not.
+ asyncTest(done => {
+ window.addEventListener("load", () => {
+ for (const id of ["wrap", "nowrap"]) {
+ const rect = document.getElementById(id).getBoundingClientRect();
+ println(`image in a column ${id} flex container: ${rect.width}x${rect.height}`);
+ }
+ done();
+ });
+ });
+</script>