Skip to content

Truly independent web browser

LibWeb: Limit a replaced box’s intrinsic width by its max-height

sideshowbarker2f29a0bb2cad
diff --git a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
index 9192448b03b..39f0289a7fb 100644
--- a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
@@ -1689,11 +1689,24 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
sized: SizedBox<'_>,
available_space: AvailableSpace,
constraints: ContainingBlockConstraints,
+ ) -> Option<TransferredInlineSizeLimits> {
+ // NB: Replaced boxes transfer their limits as part of replaced element sizing.
+ if self.facts(sized.node).is_replaced_box() {
+ return None;
+ }
+ self.transferred_inline_size_limits_of_any_box(sized, available_space, constraints)
+ }
+
+ /// The limits that [`Self::transferred_inline_size_limits`] gives, for a replaced box too.
+ fn transferred_inline_size_limits_of_any_box(
+ &self,
+ sized: SizedBox<'_>,
+ available_space: AvailableSpace,
+ constraints: ContainingBlockConstraints,
) -> Option<TransferredInlineSizeLimits> {
let node = sized.node;
let facts = self.facts(node);
- // NB: Replaced boxes transfer their limits as part of replaced element sizing.
- if facts.is_replaced_box() || !self.should_treat_inline_size_as_auto(sized, available_space) {
+ if !self.should_treat_inline_size_as_auto(sized, available_space) {
return None;
}
let ratio = facts.preferred_aspect_ratio()?;
@@ -2999,8 +3012,15 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
inline_size: available,
block_size: AvailableSize::Indefinite,
};
- self.transferred_inline_size_limits(sized, available_space, constraints)
- .map_or(inline_size, |limits| limits.clamp(inline_size))
+ let Some(limits) = self.transferred_inline_size_limits_of_any_box(sized, available_space, constraints) else {
+ return inline_size;
+ };
+ // Replaced element sizing already raises a replaced box to a transferred minimum, but never lowers its natural
+ // size to a transferred maximum.
+ if self.facts(node).is_replaced_box() {
+ return limits.maximum.map_or(inline_size, |maximum| inline_size.min(maximum));
+ }
+ limits.clamp(inline_size)
}
fn min_content_inline_size_answered_without_measuring_content(
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flex-aspect-ratio-img-row-004.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flex-aspect-ratio-img-row-004.html
new file mode 100644
index 00000000000..426c2d4cea0
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flex-aspect-ratio-img-row-004.html
@@ -0,0 +1,45 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<link rel="author" title="Google LLC" href="http://www.google.com" />
+<link rel="help" href="https://drafts.csswg.org/css-flexbox/#line-sizing" />
+<title>css-flexbox: Tests that we use the aspect ratio, clamped by cross min/max, to compute the main size.</title>
+<link rel="match" href="../../../../expected/wpt-import/css/css-flexbox/../reference/ref-filled-green-100px-square.xht" />
+
+<style>
+#reference-overlapped-red {
+ position: absolute;
+ background-color: red;
+ width: 100px;
+ height: 100px;
+ z-index: -1;
+}
+
+#flex {
+ display: flex;
+ justify-content: flex-start;
+ align-items: flex-start;
+}
+
+img {
+ min-width: 0; /* Disable min-size: auto so we don't have to think about it. */
+ min-height: 100px;
+}
+</style>
+<script>
+// Force a relayout after the image loads to work around http://crbug.com/1042329
+window.onload = (event) => {
+ flex.style.width = "50px";
+ flex.offsetTop;
+ flex.style.removeProperty("width");
+ flex.offsetTop;
+ document.documentElement.classList.remove("reftest-wait")
+}
+</script>
+
+<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:60px; height: 100px; background: green; float: left;"></div>
+<div id="flex">
+ <img src="support/20x50-green.png">
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flex-aspect-ratio-img-row-010.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flex-aspect-ratio-img-row-010.html
new file mode 100644
index 00000000000..2d5b211a1ff
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flex-aspect-ratio-img-row-010.html
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<title>SVG img as flex item</title>
+<link rel="author" title="David Grogan" href="mailto:[email protected]">
+<link rel="help" href="https://drafts.csswg.org/css-flexbox/#algo-main-item" title="Part E">
+<link rel="help" href="https://www.w3.org/TR/CSS22/visudet.html#min-max-widths" title="h > max-height line of the table">
+<link rel="match" href="../../../../expected/wpt-import/css/css-flexbox/../reference/ref-filled-green-100px-square.xht">
+<meta name="assert" content="Flex base size of image item with aspect ratio + intrinsic width + no intrinsic height honors transferred max-height." />
+
+<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
+
+<div style="display: flex;">
+ <img src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100' width='200'%3E%3Crect width='100%25' height='100%25' fill='green'/%3E%3C/svg%3E" style="max-height: 100px; flex: 0 0 auto; background: red;">
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flex-aspect-ratio-img-row-011.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flex-aspect-ratio-img-row-011.html
new file mode 100644
index 00000000000..6e1464ef211
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flex-aspect-ratio-img-row-011.html
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<title>SVG img as flex item</title>
+<link rel="author" title="David Grogan" href="mailto:[email protected]">
+<link rel="help" href="https://drafts.csswg.org/css-flexbox/#algo-main-item" title="Part E">
+<link rel="help" href="https://www.w3.org/TR/CSS22/visudet.html#min-max-widths" title="h > max-height line of the table">
+<link rel="match" href="../../../../expected/wpt-import/css/css-flexbox/../reference/ref-filled-green-100px-square.xht">
+<meta name="assert" content="Flex base size of image item with aspect ratio + intrinsic height + no intrinsic width honors transferred max-height." />
+
+<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
+
+<div style="display: flex;">
+ <img src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100' height='200'%3E%3Crect width='100%25' height='100%25' fill='green'/%3E%3C/svg%3E" style="max-height: 100px; flex: 0 0 auto; background: red;">
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flex-aspect-ratio-img-row-015.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flex-aspect-ratio-img-row-015.html
new file mode 100644
index 00000000000..423f1fee2a0
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flex-aspect-ratio-img-row-015.html
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<title>SVG as flex item</title>
+<link rel="author" title="David Grogan" href="mailto:[email protected]">
+<link rel="help" href="https://drafts.csswg.org/css-flexbox/#algo-main-item" title="Part E">
+<link rel="help" href="https://www.w3.org/TR/CSS22/visudet.html#min-max-widths">
+<link rel="match" href="../../../../expected/wpt-import/css/css-flexbox/../reference/ref-filled-green-100px-square-only.html">
+<meta name="assert" content="Flex base size of svg item with aspect ratio + no intrinsic width or height honors transferred max-height." />
+
+<p>Test passes if there is a filled green square.</p>
+
+<div style="display: flex; align-items: flex-start; height: 200px;">
+ <svg viewBox="0 0 1 1" style="max-height: 100px; background: green;"></svg>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flex-aspect-ratio-img-row-016.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flex-aspect-ratio-img-row-016.html
new file mode 100644
index 00000000000..b20df63447a
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flex-aspect-ratio-img-row-016.html
@@ -0,0 +1,35 @@
+<!DOCTYPE html>
+<title>aspect-ratio flex item</title>
+<link rel="help" href="https://drafts.csswg.org/css-flexbox/#algo-main-item" title="Part E">
+<link rel="match" href="../../../../expected/wpt-import/css/css-flexbox/../reference/ref-filled-green-100px-square.xht" />
+<meta name="assert" content="% max-height is updated when height of parent changes when %max-height affects max-content size of item">
+
+<style>
+#target {
+ display: flex;
+ align-items: start;
+ width: 50px;
+ height: 50px;
+}
+
+img {
+ /* Remove min-width:auto because it complicates things. */
+ min-width: 0px;
+ max-height: 100%;
+ background: green;
+}
+</style>
+
+<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
+<div id="target">
+ <!-- This image's natural size is 1000x1000 -->
+ <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA+gAAAPoAQMAAAB3bUanAAAAA1BMVEUAAACnej3aAAAAAXRSTlMAQObYZgAAAKpJREFUeNrswYEAAAAAgKD9qRepAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABg9uBAAAAAAADI/7URVFVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVWlPTgkAAAAABD0/7UrbAAAAADMAuw/AAGdJWCbAAAAAElFTkSuQmCC
+">
+</div>
+
+<script>
+document.body.offsetTop;
+target.style.width = '100px';
+document.body.offsetTop;
+target.style.height = '100px';
+</script>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/support/20x50-green.png b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/support/20x50-green.png
new file mode 100644
index 00000000000..f7f6e67feee
Binary files /dev/null and b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/support/20x50-green.png differ
diff --git a/Tests/LibWeb/Text/expected/css/flex-item-image-max-height-limits-container-width.txt b/Tests/LibWeb/Text/expected/css/flex-item-image-max-height-limits-container-width.txt
new file mode 100644
index 00000000000..85a6d8eb804
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/flex-item-image-max-height-limits-container-width.txt
@@ -0,0 +1,2 @@
+max-height: image 114x65, flex container 114 wide
+min-height: image 456x260, flex container 456 wide
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-grid/grid-items/grid-automatic-minimum-intrinsic-aspect-ratio-001.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-grid/grid-items/grid-automatic-minimum-intrinsic-aspect-ratio-001.txt
index f46146753c2..d1bc072d249 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-grid/grid-items/grid-automatic-minimum-intrinsic-aspect-ratio-001.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-grid/grid-items/grid-automatic-minimum-intrinsic-aspect-ratio-001.txt
@@ -2,11 +2,11 @@ Harness status: OK
Found 14 tests
-12 Pass
-2 Fail
+13 Pass
+1 Fail
Pass .container 1
Fail .container 2
-Fail .container 3
+Pass .container 3
Pass .container 4
Pass .container 5
Pass .container 6
diff --git a/Tests/LibWeb/Text/input/css/flex-item-image-max-height-limits-container-width.html b/Tests/LibWeb/Text/input/css/flex-item-image-max-height-limits-container-width.html
new file mode 100644
index 00000000000..8bf2b9a2980
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/flex-item-image-max-height-limits-container-width.html
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<div style="display: inline-flex"><img id="max-height" style="max-height: 65px"></div>
+<div style="display: inline-flex"><img id="min-height" style="min-height: 260px"></div>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ // A 228x130 image.
+ const canvas = document.createElement("canvas");
+ canvas.width = 228;
+ canvas.height = 130;
+ canvas.getContext("2d").fillRect(0, 0, 228, 130);
+ const url = canvas.toDataURL();
+ for (const id of ["max-height", "min-height"]) {
+ const image = document.getElementById(id);
+ await new Promise(resolve => {
+ image.onload = resolve;
+ image.src = url;
+ });
+ const imageRect = image.getBoundingClientRect();
+ const containerRect = image.parentElement.getBoundingClientRect();
+ println(`${id}: image ${Math.round(imageRect.width)}x${Math.round(imageRect.height)}, flex container ${Math.round(containerRect.width)} wide`);
+ }
+ });
+</script>