Skip to content

Truly independent web browser

LibWeb: Apply inline-size containment to replaced natural sizes

Andreas Klingf93cea9d9e47
diff --git a/Libraries/LibWeb/Rust/src/layout/node_facts.rs b/Libraries/LibWeb/Rust/src/layout/node_facts.rs
index f1fdb2ee610..3c52b4e4814 100644
--- a/Libraries/LibWeb/Rust/src/layout/node_facts.rs
+++ b/Libraries/LibWeb/Rust/src/layout/node_facts.rs
@@ -44,6 +44,45 @@ fn style_has_size_containment(style: ComputedValuesView<'_>) -> bool {
style.has_size_containment() || style.is_size_container()
}
+// https://drafts.csswg.org/css-contain-2/#containment-inline-size
+// Giving an element inline-size containment applies size containment to the inline-axis sizing of its principal box.
+fn style_has_inline_size_containment(style: ComputedValuesView<'_>) -> bool {
+ let display = style.display();
+ if display.is_table_inside() || display.is_internal_table() {
+ return false;
+ }
+ style.has_inline_size_containment() || style.is_inline_size_container()
+}
+
+// NB: Like size containment in that axis, a replaced element takes its explicit intrinsic inner inline size, or zero,
+// as its natural inline size, and has no natural aspect ratio.
+fn apply_inline_size_containment(facts: &mut ReplacedContentFacts, style: ComputedValuesView<'_>) {
+ let is_horizontal = style.writing_mode() == crate::css::css_enums::writing_mode::HORIZONTAL_TB;
+ let (has_length, length) = if is_horizontal {
+ (
+ style.contain_intrinsic_width_has_length(),
+ style.contain_intrinsic_width_px(),
+ )
+ } else {
+ (
+ style.contain_intrinsic_height_has_length(),
+ style.contain_intrinsic_height_px(),
+ )
+ };
+ let inline_size = if has_length {
+ CssPixels::nearest_value_for(length)
+ } else {
+ CssPixels::default()
+ };
+ if is_horizontal {
+ facts.auto_content_size.width = Some(inline_size);
+ } else {
+ facts.auto_content_size.height = Some(inline_size);
+ }
+ // NB: The natural aspect ratio must not restore an inline size from the block size.
+ facts.auto_content_size.aspect_ratio = None;
+}
+
/// The replaced content facts of an enrolled node, from its kind, its computed style and what its
/// element published as the input of its replaced content.
pub(crate) fn derived_replaced_content_facts(data: &NodeData, input: ReplacedContentInput) -> ReplacedContentFacts {
@@ -864,6 +903,20 @@ impl<'pass> NodeFacts<'pass> {
}
fn physical_replaced_content(&self) -> ReplacedContentFacts {
+ let mut facts = self.zoomed_physical_replaced_content();
+ // NB: The explicit intrinsic inner inline size already resolves against computed lengths, so it replaces the
+ // natural inline size after zoom, including the natural size that an <svg> root negotiates.
+ if node_may_have_replaced_content_facts(self.data())
+ && self.data().kind.get() != NodeKind::SVGImageBox
+ && let Some(style) = node_style_view(self.data())
+ && style_has_inline_size_containment(style)
+ {
+ apply_inline_size_containment(&mut facts, style);
+ }
+ facts
+ }
+
+ fn zoomed_physical_replaced_content(&self) -> ReplacedContentFacts {
let mut facts = self.unzoomed_physical_replaced_content();
// NB: SVG geometry and explicit intrinsic inner sizes already resolve against computed lengths.
if self.data().kind.get() == NodeKind::SVGSVGBox
@@ -1327,13 +1380,7 @@ impl<'pass> NodeFacts<'pass> {
// "Giving an element inline-size containment applies size containment to the inline-axis sizing of its principal
// box."
pub(crate) fn node_has_inline_size_containment(&self) -> bool {
- // NB: So it has no effect where size containment would have none.
- let display = self.display();
- if display.is_table_inside() || display.is_internal_table() {
- return false;
- }
- let style = self.style();
- style.has_inline_size_containment() || style.is_inline_size_container()
+ style_has_inline_size_containment(*self.style())
}
pub(crate) fn has_preferred_aspect_ratio(&self) -> bool {
diff --git a/Tests/LibWeb/Text/expected/css/contain-inline-size-replaced-elements.txt b/Tests/LibWeb/Text/expected/css/contain-inline-size-replaced-elements.txt
index 51fea1e88d5..cc6a5c865e6 100644
--- a/Tests/LibWeb/Text/expected/css/contain-inline-size-replaced-elements.txt
+++ b/Tests/LibWeb/Text/expected/css/contain-inline-size-replaced-elements.txt
@@ -1,4 +1,4 @@
-svg with viewBox: 784x392
-zoomed canvas: 80x40
+svg with viewBox: 100x150
+zoomed canvas: 200x40
zoomed size-contained canvas: 200x40
-zoomed canvas with new intrinsic size: 80x40
+zoomed canvas with new intrinsic size: 100x40
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-sizing/contain-intrinsic-size/contain-intrinsic-size-028.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-sizing/contain-intrinsic-size/contain-intrinsic-size-028.txt
index e2b89b409ed..548beb0f998 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-sizing/contain-intrinsic-size/contain-intrinsic-size-028.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-sizing/contain-intrinsic-size/contain-intrinsic-size-028.txt
@@ -2,8 +2,8 @@ Harness status: OK
Found 48 tests
-4 Pass
-44 Fail
+44 Pass
+4 Fail
Pass .test 1
Pass .test 2
Pass .test 3
@@ -12,43 +12,43 @@ Fail .test 5
Fail .test 6
Fail .test 7
Fail .test 8
-Fail .test 9
-Fail .test 10
-Fail .test 11
-Fail .test 12
-Fail .test 13
-Fail .test 14
-Fail .test 15
-Fail .test 16
-Fail .test 17
-Fail .test 18
-Fail .test 19
-Fail .test 20
-Fail .test 21
-Fail .test 22
-Fail .test 23
-Fail .test 24
-Fail .test 25
-Fail .test 26
-Fail .test 27
-Fail .test 28
-Fail .test 29
-Fail .test 30
-Fail .test 31
-Fail .test 32
-Fail .test 33
-Fail .test 34
-Fail .test 35
-Fail .test 36
-Fail .test 37
-Fail .test 38
-Fail .test 39
-Fail .test 40
-Fail .test 41
-Fail .test 42
-Fail .test 43
-Fail .test 44
-Fail .test 45
-Fail .test 46
-Fail .test 47
-Fail .test 48
\ No newline at end of file
+Pass .test 9
+Pass .test 10
+Pass .test 11
+Pass .test 12
+Pass .test 13
+Pass .test 14
+Pass .test 15
+Pass .test 16
+Pass .test 17
+Pass .test 18
+Pass .test 19
+Pass .test 20
+Pass .test 21
+Pass .test 22
+Pass .test 23
+Pass .test 24
+Pass .test 25
+Pass .test 26
+Pass .test 27
+Pass .test 28
+Pass .test 29
+Pass .test 30
+Pass .test 31
+Pass .test 32
+Pass .test 33
+Pass .test 34
+Pass .test 35
+Pass .test 36
+Pass .test 37
+Pass .test 38
+Pass .test 39
+Pass .test 40
+Pass .test 41
+Pass .test 42
+Pass .test 43
+Pass .test 44
+Pass .test 45
+Pass .test 46
+Pass .test 47
+Pass .test 48
\ No newline at end of file