Skip to content

Truly independent web browser

LibWeb: Give replaced elements their natural height for size keywords

Andreas Kling104861099c70
diff --git a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
index 59338caf72c..29750a45536 100644
--- a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
@@ -3321,7 +3321,22 @@ impl<'pass> SizingContext<'pass> {
return self.content_block_size_from_aspect_ratio(node, self.used(node).content_inline_size.get());
}
assert!(!preferred_size.is_auto());
- let keyword_size = if preferred_size.is_fit_content() {
+ let facts = self.facts(node);
+ let keyword_size = if facts.is_replaced_box()
+ && facts.has_auto_content_box_size()
+ && preferred_size.is_intrinsic_sizing_constraint()
+ {
+ // NB: The intrinsic block sizes of a replaced element are its natural height, or the height that follows
+ // its used width through its aspect ratio.
+ let intrinsic = self.intrinsic_size_for_replaced_sizing(node);
+ match intrinsic.height {
+ Some(height) if self.style(node).width().is_auto() => Some(height),
+ _ if facts.has_preferred_aspect_ratio() => {
+ Some(self.content_block_size_from_aspect_ratio(node, self.used(node).content_inline_size.get()))
+ }
+ height => Some(height.unwrap_or_else(|| CssPixels::from_integer(150))),
+ }
+ } else if preferred_size.is_fit_content() {
Some(self.calculate_fit_content_size(node, SizingAxis::Block, available_space, constraints))
} else if preferred_size.is_max_content() {
Some(self.calculate_max_content_block_size(node, available_space.inline_size.to_px_or_zero(), constraints))
diff --git a/Tests/LibWeb/Text/expected/replaced/image-intrinsic-height-keyword.txt b/Tests/LibWeb/Text/expected/replaced/image-intrinsic-height-keyword.txt
new file mode 100644
index 00000000000..e2e8ea7aeef
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/replaced/image-intrinsic-height-keyword.txt
@@ -0,0 +1,3 @@
+max: 60x20
+min: 60x20
+fit: 60x20
diff --git a/Tests/LibWeb/Text/input/replaced/image-intrinsic-height-keyword.html b/Tests/LibWeb/Text/input/replaced/image-intrinsic-height-keyword.html
new file mode 100644
index 00000000000..71c919605e0
--- /dev/null
+++ b/Tests/LibWeb/Text/input/replaced/image-intrinsic-height-keyword.html
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<style>
+ img {
+ display: block;
+ }
+</style>
+<img id="max" style="height: max-content" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='60' height='20'/%3E">
+<img id="min" style="height: min-content" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='60' height='20'/%3E">
+<img id="fit" style="height: fit-content" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='60' height='20'/%3E">
+<script src="../include.js"></script>
+<script>
+ asyncTest(done => {
+ window.onload = () => {
+ for (const id of ["max", "min", "fit"]) {
+ const image = document.getElementById(id);
+ println(`${id}: ${image.offsetWidth}x${image.offsetHeight}`);
+ }
+ done();
+ };
+ });
+</script>