Skip to content

Truly independent web browser

LibWeb: Keep a flex item’s automatic minimum size with a small basis

sideshowbarker72b5a63611a9
diff --git a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
index d2491d9cdb8..3a9ff151976 100644
--- a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
@@ -4233,19 +4233,26 @@ impl<'pass> FlexFormattingContext<'pass> {
self.resolve_own_auto_block_size_from_max_content_main_size();
// 3. Determine the flex base size and hypothetical main size of each item
- let mut all_items_have_definite_main_sizes = true;
+ // NB: An item's automatic minimum size is never more than its specified size suggestion, so it can't raise a
+ // flex base size that's at least that suggestion. A flex-basis smaller than the item's width or height,
+ // e.g., is raised to the smaller of that size and the item's content size.
+ let mut automatic_minimum_sizes_cannot_raise_base_sizes = true;
let mut sum_of_flex_base_sizes = CssPixels::default();
for index in 0..self.flex_items.len() {
self.determine_flex_base_size(index);
- all_items_have_definite_main_sizes &= self.has_definite_main_size(index);
- sum_of_flex_base_sizes += self.flex_items[index].flex_base_size;
+ let flex_base_size = self.flex_items[index].flex_base_size;
+ automatic_minimum_sizes_cannot_raise_base_sizes &= self
+ .specified_size_suggestion(index)
+ .is_some_and(|suggestion| flex_base_size >= suggestion);
+ sum_of_flex_base_sizes += flex_base_size;
}
// OPTIMIZATION: We try to avoid calculating the "automatic minimum size" if possible,
// since it may require expensive intrinsic sizing layout.
// If this is a single-line flex container and the sum of flex bases sizes
// won't exceed the available space in the main axis, we know the layout
- // algorithm won't have to shrink anything, thus not needing the minimum size.
+ // algorithm won't have to shrink anything. And where no automatic minimum size can raise an
+ // item's flex base size either, the minimum size isn't needed at all.
let should_skip_automatic_minimum_size_clamp = !self
.available_space
.unwrap()
@@ -4253,7 +4260,7 @@ impl<'pass> FlexFormattingContext<'pass> {
.is_intrinsic_sizing_constraint()
&& self.is_single_line()
&& self.has_definite_main_size_used(&self.container_cells)
- && all_items_have_definite_main_sizes
+ && automatic_minimum_sizes_cannot_raise_base_sizes
&& sum_of_flex_base_sizes <= self.available_space_for_items.unwrap().main.to_px_or_zero();
for index in 0..self.flex_items.len() {
diff --git a/Tests/LibWeb/Ref/expected/flex-item-automatic-minimum-size-with-definite-flex-basis-ref.html b/Tests/LibWeb/Ref/expected/flex-item-automatic-minimum-size-with-definite-flex-basis-ref.html
new file mode 100644
index 00000000000..a668bdaf472
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/flex-item-automatic-minimum-size-with-definite-flex-basis-ref.html
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<style>
+ body {
+ margin: 0;
+ }
+ div {
+ background: green;
+ height: 20px;
+ margin-bottom: 4px;
+ }
+</style>
+<div style="width: 100px"></div>
+<div style="width: 100px"></div>
+<div style="width: 300px"></div>
+<div style="width: 300px"></div>
+<div style="width: 20px; height: 100px; margin-bottom: 200px"></div>
diff --git a/Tests/LibWeb/Ref/input/flex-item-automatic-minimum-size-with-definite-flex-basis.html b/Tests/LibWeb/Ref/input/flex-item-automatic-minimum-size-with-definite-flex-basis.html
new file mode 100644
index 00000000000..57251c905f0
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/flex-item-automatic-minimum-size-with-definite-flex-basis.html
@@ -0,0 +1,29 @@
+<!DOCTYPE html>
+<link rel="match" href="../expected/flex-item-automatic-minimum-size-with-definite-flex-basis-ref.html" />
+<style>
+ body {
+ margin: 0;
+ }
+ .outer {
+ display: flex;
+ width: 600px;
+ margin-bottom: 4px;
+ }
+ .item {
+ background: green;
+ height: 20px;
+ }
+ .content {
+ width: 300px;
+ height: 20px;
+ }
+</style>
+<!-- A flex-basis smaller than the item's width doesn't make its automatic minimum size smaller than that width, or
+ than its content where that's smaller. -->
+<div class="outer"><div class="item" style="flex-basis: 0px; width: 100px"><div class="content"></div></div></div>
+<div class="outer"><div class="item" style="flex-basis: 50px; width: 100px"><div class="content"></div></div></div>
+<div class="outer"><div class="item" style="flex-basis: 50px; width: 400px"><div class="content"></div></div></div>
+<div class="outer"><div class="item" style="flex-basis: 0%; width: 400px"><div class="content"></div></div></div>
+<div class="outer" style="flex-direction: column; width: auto; height: 300px">
+ <div class="item" style="flex-basis: 0%; height: 100px; width: 20px"><div class="content" style="width: 20px; height: 200px"></div></div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flex-minimum-height-flex-items-005.xht b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flex-minimum-height-flex-items-005.xht
new file mode 100644
index 00000000000..ce43e3860ba
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flex-minimum-height-flex-items-005.xht
@@ -0,0 +1,38 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <title>CSS Flexible Box Test: Minimum height of flex items</title>
+ <link rel="author" title="Manuel Rego Casasnovas" href="mailto:[email protected]" />
+ <link rel="help" href="http://www.w3.org/TR/css-flexbox-1/#min-size-auto" title="4.5. Implied Minimum Size of Flex Items" />
+ <link rel="match" href="../../../../expected/wpt-import/css/css-flexbox/../reference/ref-filled-green-100px-square.xht" />
+ <meta name="assert" content="Checks that automatic minimum height for flex items is the specified size suggestion, when that is the smallest of the available suggestions." />
+ <style type="text/css"><![CDATA[
+ #reference-overlapped-red {
+ position: absolute;
+ background-color: red;
+ width: 100px;
+ height: 100px;
+ z-index: -1;
+ }
+
+ #constrained-flex {
+ display: flex;
+ flex-direction: column;
+ width: 100px;
+ height: 10px;
+ }
+
+ #test-flex-item-overlapping-green {
+ height: 100px;
+ }
+ ]]></style>
+ </head>
+ <body>
+ <p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
+
+ <div id="reference-overlapped-red"></div>
+ <div id="constrained-flex">
+ <img id="test-flex-item-overlapping-green" src="support/200x200-green.png" />
+ </div>
+ </body>
+</html>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flex-minimum-width-flex-items-005.xht b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flex-minimum-width-flex-items-005.xht
new file mode 100644
index 00000000000..1c47b305399
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flex-minimum-width-flex-items-005.xht
@@ -0,0 +1,36 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <title>CSS Flexible Box Test: Minimum width of flex items</title>
+ <link rel="author" title="Manuel Rego Casasnovas" href="mailto:[email protected]" />
+ <link rel="help" href="http://www.w3.org/TR/css-flexbox-1/#min-size-auto" title="4.5. Implied Minimum Size of Flex Items" />
+ <link rel="match" href="../../../../expected/wpt-import/css/css-flexbox/../reference/ref-filled-green-100px-square.xht" />
+ <meta name="assert" content="Checks that automatic minimum width for flex items is the specified size suggestion, when that is the smallest of the available suggestions." />
+ <style type="text/css"><![CDATA[
+ #reference-overlapped-red {
+ position: absolute;
+ background-color: red;
+ width: 100px;
+ height: 100px;
+ z-index: -1;
+ }
+
+ #constrained-flex {
+ display: flex;
+ width: 10px;
+ }
+
+ #test-flex-item-overlapping-green {
+ width: 100px;
+ }
+ ]]></style>
+ </head>
+ <body>
+ <p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
+
+ <div id="reference-overlapped-red"></div>
+ <div id="constrained-flex">
+ <img id="test-flex-item-overlapping-green" src="support/200x200-green.png" />
+ </div>
+ </body>
+</html>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flex-minimum-width-flex-items-013.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flex-minimum-width-flex-items-013.html
new file mode 100644
index 00000000000..c562bcb99ef
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flex-minimum-width-flex-items-013.html
@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+<title>Flex transferred minimum width</title>
+<link rel="author" title="[email protected]" href="mailto:[email protected]" />
+<link rel="help" href="https://drafts.csswg.org/css-flexbox/#min-size-auto" />
+<link rel="help" href="https://github.com/w3c/csswg-drafts/issues/5663" />
+<link rel="match" href="../../../../expected/wpt-import/css/css-flexbox/../reference/ref-filled-green-100px-square.xht" />
+<meta name="assert" content="content size suggestion of a replaced aspect-ratio item accounts for definition stretched cross size">
+
+<style>
+#reference-overlapped-red {
+ position: absolute;
+ background-color: red;
+ width: 100px;
+ height: 100px;
+ z-index: -1;
+}
+</style>
+
+<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
+
+<div id="reference-overlapped-red"></div>
+<div style="width:100px; height: 50px; background: green;"></div> <!-- This just completes the green square. -->
+<div style="display: flex; width: 0px; height: 50px;">
+ <!--
+ content size suggestion is 100px because the image has a stretched height of 50px. So min-content size of the image is 300x150/50 = 100.
+ specified size suggestion is 150px
+ transferred size suggestion is 100px
+
+ transferred size suggestion is ignored because there is a specified size.
+
+ So here the content-based minimum size is min(100px, 150px) = 100px
+ -->
+ <img src="support/300x150-green.png" style="width: 999px;">
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/support/200x200-green.png b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/support/200x200-green.png
new file mode 100644
index 00000000000..1dcc392a6ef
Binary files /dev/null and b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/support/200x200-green.png differ
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/support/300x150-green.png b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/support/300x150-green.png
new file mode 100644
index 00000000000..57ece824a40
Binary files /dev/null and b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/support/300x150-green.png differ