Skip to content

Truly independent web browser

LibWeb: Resolve a replaced grid item’s percentage max-width at zero

sideshowbarker6cdf1dac5dd5
diff --git a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
index 4111187fefc..f5681e1769a 100644
--- a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
@@ -2732,6 +2732,34 @@ impl<'pass> GridFormattingContext<'pass> {
let max = self.maximum_size(item, axis);
let maximum = if max.is_length_percentage() && !max.contains_percentage() {
max.to_px(CssPixels::default())
+ } else if axis.is_column()
+ && max.is_length_percentage()
+ && formatting_context::cyclic_percentage_intrinsic_contribution(
+ self.facts(item.box_).is_replaced_box(),
+ true,
+ AvailableSize::MinContent,
+ formatting_context::CyclicPercentageSizeProperty::PreferredOrMaxSize,
+ ) == formatting_context::CyclicPercentageIntrinsicContribution::ResolveAsZero
+ {
+ // https://drafts.csswg.org/css-sizing-3/#cyclic-percentage-contribution
+ // If the box is replaced, a cyclic percentage in the value of any max size property or preferred size
+ // property ('width'/'max-width'/'height'/'max-height'), is resolved against zero when calculating the
+ // min-content contribution in the corresponding axis.
+ // NB: Only in the inline axis: A replaced box's percentage block size behaves as auto, so that it takes
+ // its height from its aspect ratio rather than collapsing its row. And the zero-based maximum caps
+ // only the box's own size, so the contribution keeps the item's margins, borders and padding.
+ let mut available_space = self.item_available_space(item);
+ available_space.inline_size = AvailableSize::definite(CssPixels::default());
+ let mut constraints = self.track_sizing_constraints();
+ constraints.percentage_basis_inline_size = Some(CssPixels::default());
+ let inner_maximum = self.sizing().calculate_inner_size_for_property(
+ sizing_context::SizedBox::new(item.box_, &self.cells_of(item)),
+ axis.sizing_axis(),
+ SizingProperty::MaxWidth,
+ available_space,
+ constraints,
+ );
+ self.add_outer_size(item, axis, inner_maximum)
} else {
CssPixels::from_raw(i32::MAX)
};
diff --git a/Tests/LibWeb/Layout/expected/grid/replaced-item-percentage-max-width-min-content-contribution.txt b/Tests/LibWeb/Layout/expected/grid/replaced-item-percentage-max-width-min-content-contribution.txt
new file mode 100644
index 00000000000..9a9b3598df0
--- /dev/null
+++ b/Tests/LibWeb/Layout/expected/grid/replaced-item-percentage-max-width-min-content-contribution.txt
@@ -0,0 +1,50 @@
+Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline overflow: [0,0 1408x600]
+ BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 516.015625 0+0+0] [BFC] children: not-inline
+ BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 500.015625 0+0+8] children: not-inline
+ Box <div.outer> at [8,8] [0+0+0 1400 0+0+-616] [0+0+0 400.015625 0+0+0] [GFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [8,8] [0+0+0 700 0+0+0] [0+0+0 400.015625 0+0+0] [BFC] children: not-inline
+ Box <div.inner> at [8,8] [0+0+0 700 0+0+0] [0+0+0 400.015625 0+0+0] [GFC] children: not-inline
+ CanvasBox <canvas> at [8,8] [0+0+0 700.03125 0+0+0] [0+0+0 400.015625 0+0+0] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [708,8] [0+0+0 700 0+0+0] [0+0+0 400.015625 0+0+0] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 5, rect: [708,8 37.109375x16] baseline: 12.796875
+ "right"
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [8,408.015625] [0+0+0 784 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ Box <div.min-content-track> at [8,408.015625] [0+0+0 400 0+0+384] [0+0+0 50 0+0+0] [GFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ CanvasBox <canvas.with-margins> at [58,408.015625] [50+0+0 100 0+0+50] [0+0+0 50 0+0+0] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [108,408.015625] [0+0+0 300 0+0+0] [0+0+0 50 0+0+0] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 5, rect: [108,408.015625 37.109375x16] baseline: 12.796875
+ "right"
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [8,458.015625] [0+0+0 784 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ Box <div.min-content-track> at [8,458.015625] [0+0+0 400 0+0+384] [0+0+0 50 0+0+0] [GFC] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ CanvasBox <canvas.border-box> at [18,468.015625] [0+0+10 130 10+0+0] [0+0+10 30 10+0+0] children: not-inline
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [108,458.015625] [0+0+0 300 0+0+0] [0+0+0 50 0+0+0] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 5, rect: [108,458.015625 37.109375x16] baseline: 12.796875
+ "right"
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [8,508.015625] [0+0+0 784 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+
+SC for Viewport<#document> [0,0 800x600] (z-index: auto)
+ SC for BlockContainer<HTML> [0,0 800x516.015625] (z-index: auto)
diff --git a/Tests/LibWeb/Layout/input/grid/replaced-item-percentage-max-width-min-content-contribution.html b/Tests/LibWeb/Layout/input/grid/replaced-item-percentage-max-width-min-content-contribution.html
new file mode 100644
index 00000000000..4d6fc792616
--- /dev/null
+++ b/Tests/LibWeb/Layout/input/grid/replaced-item-percentage-max-width-min-content-contribution.html
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<style>
+ .outer { display: grid; grid-template-columns: 1fr 1fr; width: 1400px; }
+ .inner { display: grid; grid-template-columns: repeat(6, 1fr); }
+ canvas { display: block; width: 1274px; max-width: 100%; height: auto; grid-column: 1 / 7; }
+ .min-content-track { display: grid; grid-template-columns: min-content 1fr; width: 400px; }
+ .with-margins { width: 200px; height: 50px; margin-inline: 50px; grid-column: auto; }
+ .border-box { box-sizing: border-box; width: 200px; height: 50px; padding: 10px; max-width: calc(100px + 50%); grid-column: auto; }
+</style>
+<div class="outer">
+ <div><div class="inner"><canvas width="1274" height="728"></canvas></div></div>
+ <div>right</div>
+</div>
+<div class="min-content-track">
+ <canvas class="with-margins" width="200" height="50"></canvas>
+ <div>right</div>
+</div>
+<div class="min-content-track">
+ <canvas class="border-box" width="200" height="50"></canvas>
+ <div>right</div>
+</div>