Truly independent web browser
LibWeb: Re-resolve grid columns once rows give percentage heights
Andreas Klingde659c3a3d22
diff --git a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rsindex c9f21b2bee9..59a429b41d8 100644--- a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs@@ -987,6 +987,9 @@ pub(crate) struct GridFormattingContext<'pass> {automatic_content_block_size: CssPixels,row_alignment_container_size: CssPixels,use_row_alignment_container_size: bool,+ // Set once the rows are sized, so that the columns can be resolved again with percentage heights resolved against+ // them.+ use_row_sizes_for_column_percentages: bool,}#[derive(Clone, Copy)]@@ -1081,6 +1084,7 @@ impl<'pass> GridFormattingContext<'pass> {automatic_content_block_size: CssPixels::default(),row_alignment_container_size: CssPixels::default(),use_row_alignment_container_size: false,+ use_row_sizes_for_column_percentages: false,}}@@ -1107,6 +1111,7 @@ impl<'pass> GridFormattingContext<'pass> {self.automatic_content_block_size = CssPixels::default();self.row_alignment_container_size = CssPixels::default();self.use_row_alignment_container_size = false;+ self.use_row_sizes_for_column_percentages = false;}fn subgrid_block_size_memo(&self) -> &SubgridBlockSizeMemo {@@ -1987,11 +1992,92 @@ impl<'pass> GridFormattingContext<'pass> {mut constraints: ContainingBlockConstraints,) -> ContainingBlockConstraints {if constraints.percentage_basis_block_size.is_none() {- constraints.percentage_basis_block_size = self.fixed_track_span_size(item, Axis::Row);+ constraints.percentage_basis_block_size = if self.use_row_sizes_for_column_percentages {+ Some(self.containing_block_size(item, Axis::Row))+ } else {+ self.fixed_track_span_size(item, Axis::Row)+ };}constraints}+ // The items whose column contributions can change once the rows are sized: a percentage block size of a box with a+ // preferred aspect ratio transfers to its inline size.+ fn items_with_column_contributions_depending_on_row_sizes(&self) -> Vec<usize> {+ (0..self.items.len())+ .filter(|&index| {+ let item = self.items[index];+ if !self.facts(item.box_).has_preferred_aspect_ratio() {+ return false;+ }+ let style = self.style(item.box_);+ (style.height().contains_percentage()+ || style.min_height().contains_percentage()+ || style.max_height().contains_percentage())+ && self.fixed_track_span_size(item, Axis::Row).is_none()+ })+ .collect()+ }++ // https://drafts.csswg.org/css-grid-2/#algo-overview+ // 3. If the min-content contribution of any grid item has changed based on the row sizes and alignment calculated+ // in step 2, re-resolve the sizes of the grid columns with the new min-content and max-content contributions+ // (once only).+ // NB: The items are then sized again within the rows, which are not resolved again, as in other engines.+ fn re_resolve_columns_with_row_sizes(&mut self) {+ let candidates = self.items_with_column_contributions_depending_on_row_sizes();+ if candidates.is_empty() {+ return;+ }+ // NB: The contributions are taken as in the first column pass, before the items have their used sizes.+ let item_sizes_were_definite: Vec<(bool, bool)> = self+ .items+ .iter()+ .map(|&item| {+ let used = self.used(item);+ (used.has_definite_inline_size(), used.has_definite_block_size())+ })+ .collect();+ for &item in &self.items {+ let used = self.used(item);+ used.has_definite_inline_size.set(false);+ used.has_definite_block_size.set(false);+ }+ // NB: Both contributions feed the column sizing, so either changing counts.+ let contributions = |context: &Self| -> Vec<(CssPixels, CssPixels)> {+ candidates+ .iter()+ .map(|&index| {+ let item = context.items[index];+ (+ context.min_content_contribution(item, Axis::Column),+ context.max_content_contribution(item, Axis::Column),+ )+ })+ .collect()+ };+ let before = contributions(self);+ self.use_row_sizes_for_column_percentages = true;+ if contributions(self) == before {+ self.use_row_sizes_for_column_percentages = false;+ for (&item, &(inline, block)) in self.items.iter().zip(&item_sizes_were_definite) {+ let used = self.used(item);+ used.has_definite_inline_size.set(inline);+ used.has_definite_block_size.set(block);+ }+ return;+ }+ self.run_track_sizing(Axis::Column);+ self.resolve_item_metrics(Axis::Column);+ self.resolve_item_sizes(Axis::Column);+ self.resolve_item_metrics(Axis::Row);+ self.resolve_item_sizes(Axis::Row);+ // NB: Sizing the columns again marks the inline sizes definite, but leaves the block sizes as they were made.+ for (&item, &(_, block)) in self.items.iter().zip(&item_sizes_were_definite) {+ self.used(item).has_definite_block_size.set(block);+ }+ }+fn intrinsic_contribution_constraints(&self, item: GridItem, axis: Axis) -> ContainingBlockConstraints {let mut constraints = self.container_constraints();if axis.is_column() {@@ -3775,6 +3861,7 @@ impl<'pass> GridFormattingContext<'pass> {self.row_alignment_container_size = block_size;self.use_row_alignment_container_size = true;}+ self.re_resolve_columns_with_row_sizes();self.resolve_track_spacing(Axis::Column);self.resolve_track_spacing(Axis::Row);diff --git a/Tests/LibWeb/Layout/expected/grid/ratio-only-replaced-item-size-transfer-matrix.txt b/Tests/LibWeb/Layout/expected/grid/ratio-only-replaced-item-size-transfer-matrix.txtindex ce293fc218b..50090e0651e 100644--- a/Tests/LibWeb/Layout/expected/grid/ratio-only-replaced-item-size-transfer-matrix.txt+++ b/Tests/LibWeb/Layout/expected/grid/ratio-only-replaced-item-size-transfer-matrix.txt@@ -41,11 +41,11 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] childrenSVGGeometryBox <rect> at [0,0] [0+0+0 2 0+0+0] [0+0+0 1 0+0+0] children: not-inlineBlockContainer <(anonymous)> at [8,141] [0+0+0 784 0+0+0] [0+0+0 0 0+0+0] children: inlineTextNode <#text> (not painted)- Box <div> at [8,141] [0+0+0 20 0+0+764] [0+0+0 10 0+0+0] [GFC] children: not-inline- ImageBox <img.cyclic-calculated-transferred-minimum> at [8,141] [0+0+0 20 0+0+0] [0+0+0 15 0+0+0] children: not-inline+ Box <div> at [8,141] [0+0+0 30 0+0+754] [0+0+0 10 0+0+0] [GFC] children: not-inline+ ImageBox <img.cyclic-calculated-transferred-minimum> at [8,141] [0+0+0 30 0+0+0] [0+0+0 15 0+0+0] children: not-inline(SVG-as-image isolated context)- Viewport <#document> at [0,0] [0+0+0 20 0+0+0] [0+0+0 15 0+0+0] [BFC] children: not-inline- SVGSVGBox <svg> at [0,0] [0+0+0 20 0+0+0] [0+0+0 15 0+0+0] viewport-transform=[10 0 0 10 0 2.5] [SVG] children: not-inline+ Viewport <#document> at [0,0] [0+0+0 30 0+0+0] [0+0+0 15 0+0+0] [BFC] children: not-inline+ SVGSVGBox <svg> at [0,0] [0+0+0 30 0+0+0] [0+0+0 15 0+0+0] viewport-transform=[15 0 0 15 0 0] [SVG] children: not-inlineSVGGeometryBox <rect> at [0,0] [0+0+0 2 0+0+0] [0+0+0 1 0+0+0] children: not-inlineBlockContainer <(anonymous)> at [8,151] [0+0+0 784 0+0+0] [0+0+0 0 0+0+0] children: inlineTextNode <#text> (not painted)diff --git a/Tests/LibWeb/Text/expected/css/aspect-ratio-percentage-height-matrix.txt b/Tests/LibWeb/Text/expected/css/aspect-ratio-percentage-height-matrix.txtindex 73fa3f77b0c..f526e5af75d 100644--- a/Tests/LibWeb/Text/expected/css/aspect-ratio-percentage-height-matrix.txt+++ b/Tests/LibWeb/Text/expected/css/aspect-ratio-percentage-height-matrix.txt@@ -2526,14 +2526,14 @@ grid block width=auto height=100% tall none: 10x5 child=2.5grid block width=auto height=100% tall max-height: 10x5 child=2.5grid block width=auto height=100% tall min-height: 120x60 child=30grid block width=auto height=100% tall padding: 20x10 child=0-grid block width=auto height=calc empty none: 0x10 child=5-grid block width=auto height=calc empty max-height: 0x10 child=5+grid block width=auto height=calc empty none: 20x10 child=5+grid block width=auto height=calc empty max-height: 20x10 child=5grid block width=auto height=calc empty min-height: 120x60 child=30-grid block width=auto height=calc empty padding: 20x15 child=2.5-grid block width=auto height=calc tall none: 10x12.5 child=6.25-grid block width=auto height=calc tall max-height: 10x12.5 child=6.25+grid block width=auto height=calc empty padding: 30x15 child=2.5+grid block width=auto height=calc tall none: 25x12.5 child=6.25+grid block width=auto height=calc tall max-height: 25x12.5 child=6.25grid block width=auto height=calc tall min-height: 120x60 child=30-grid block width=auto height=calc tall padding: 20x15 child=2.5+grid block width=auto height=calc tall padding: 30x15 child=2.5grid block width=auto height=auto empty none: 0x0 child=0grid block width=auto height=auto empty max-height: 0x0 child=0grid block width=auto height=auto empty min-height: 120x60 child=30@@ -2598,14 +2598,14 @@ grid flow-root width=auto height=100% tall none: 10x5 child=2.5grid flow-root width=auto height=100% tall max-height: 10x5 child=2.5grid flow-root width=auto height=100% tall min-height: 120x60 child=30grid flow-root width=auto height=100% tall padding: 20x10 child=0-grid flow-root width=auto height=calc empty none: 0x10 child=5-grid flow-root width=auto height=calc empty max-height: 0x10 child=5+grid flow-root width=auto height=calc empty none: 20x10 child=5+grid flow-root width=auto height=calc empty max-height: 20x10 child=5grid flow-root width=auto height=calc empty min-height: 120x60 child=30-grid flow-root width=auto height=calc empty padding: 20x15 child=2.5-grid flow-root width=auto height=calc tall none: 10x12.5 child=6.25-grid flow-root width=auto height=calc tall max-height: 10x12.5 child=6.25+grid flow-root width=auto height=calc empty padding: 30x15 child=2.5+grid flow-root width=auto height=calc tall none: 25x12.5 child=6.25+grid flow-root width=auto height=calc tall max-height: 25x12.5 child=6.25grid flow-root width=auto height=calc tall min-height: 120x60 child=30-grid flow-root width=auto height=calc tall padding: 20x15 child=2.5+grid flow-root width=auto height=calc tall padding: 30x15 child=2.5grid flow-root width=auto height=auto empty none: 0x0 child=0grid flow-root width=auto height=auto empty max-height: 0x0 child=0grid flow-root width=auto height=auto empty min-height: 120x60 child=30@@ -2670,14 +2670,14 @@ grid inline-block width=auto height=100% tall none: 10x5 child=2.5grid inline-block width=auto height=100% tall max-height: 10x5 child=2.5grid inline-block width=auto height=100% tall min-height: 120x60 child=30grid inline-block width=auto height=100% tall padding: 20x10 child=0-grid inline-block width=auto height=calc empty none: 0x10 child=5-grid inline-block width=auto height=calc empty max-height: 0x10 child=5+grid inline-block width=auto height=calc empty none: 20x10 child=5+grid inline-block width=auto height=calc empty max-height: 20x10 child=5grid inline-block width=auto height=calc empty min-height: 120x60 child=30-grid inline-block width=auto height=calc empty padding: 20x15 child=2.5-grid inline-block width=auto height=calc tall none: 10x12.5 child=6.25-grid inline-block width=auto height=calc tall max-height: 10x12.5 child=6.25+grid inline-block width=auto height=calc empty padding: 30x15 child=2.5+grid inline-block width=auto height=calc tall none: 25x12.5 child=6.25+grid inline-block width=auto height=calc tall max-height: 25x12.5 child=6.25grid inline-block width=auto height=calc tall min-height: 120x60 child=30-grid inline-block width=auto height=calc tall padding: 20x15 child=2.5+grid inline-block width=auto height=calc tall padding: 30x15 child=2.5grid inline-block width=auto height=auto empty none: 0x0 child=0grid inline-block width=auto height=auto empty max-height: 0x0 child=0grid inline-block width=auto height=auto empty min-height: 120x60 child=30@@ -2742,14 +2742,14 @@ grid float width=auto height=100% tall none: 10x5 child=2.5grid float width=auto height=100% tall max-height: 10x5 child=2.5grid float width=auto height=100% tall min-height: 120x60 child=30grid float width=auto height=100% tall padding: 20x10 child=0-grid float width=auto height=calc empty none: 0x10 child=5-grid float width=auto height=calc empty max-height: 0x10 child=5+grid float width=auto height=calc empty none: 20x10 child=5+grid float width=auto height=calc empty max-height: 20x10 child=5grid float width=auto height=calc empty min-height: 120x60 child=30-grid float width=auto height=calc empty padding: 20x15 child=2.5-grid float width=auto height=calc tall none: 10x12.5 child=6.25-grid float width=auto height=calc tall max-height: 10x12.5 child=6.25+grid float width=auto height=calc empty padding: 30x15 child=2.5+grid float width=auto height=calc tall none: 25x12.5 child=6.25+grid float width=auto height=calc tall max-height: 25x12.5 child=6.25grid float width=auto height=calc tall min-height: 120x60 child=30-grid float width=auto height=calc tall padding: 20x15 child=2.5+grid float width=auto height=calc tall padding: 30x15 child=2.5grid float width=auto height=auto empty none: 0x0 child=0grid float width=auto height=auto empty max-height: 0x0 child=0grid float width=auto height=auto empty min-height: 120x60 child=30@@ -2814,14 +2814,14 @@ grid flex width=auto height=100% tall none: 10x5 child=2.5grid flex width=auto height=100% tall max-height: 10x5 child=2.5grid flex width=auto height=100% tall min-height: 120x60 child=30grid flex width=auto height=100% tall padding: 20x10 child=0-grid flex width=auto height=calc empty none: 0x10 child=5-grid flex width=auto height=calc empty max-height: 0x10 child=5+grid flex width=auto height=calc empty none: 20x10 child=5+grid flex width=auto height=calc empty max-height: 20x10 child=5grid flex width=auto height=calc empty min-height: 120x60 child=30-grid flex width=auto height=calc empty padding: 20x15 child=2.5-grid flex width=auto height=calc tall none: 10x12.5 child=6.25-grid flex width=auto height=calc tall max-height: 10x12.5 child=6.25+grid flex width=auto height=calc empty padding: 30x15 child=2.5+grid flex width=auto height=calc tall none: 25x12.5 child=6.25+grid flex width=auto height=calc tall max-height: 25x12.5 child=6.25grid flex width=auto height=calc tall min-height: 120x60 child=30-grid flex width=auto height=calc tall padding: 20x15 child=2.5+grid flex width=auto height=calc tall padding: 30x15 child=2.5grid flex width=auto height=auto empty none: 0x0 child=0grid flex width=auto height=auto empty max-height: 0x0 child=0grid flex width=auto height=auto empty min-height: 120x60 child=30@@ -2886,14 +2886,14 @@ grid grid width=auto height=100% tall none: 10x5 child=0grid grid width=auto height=100% tall max-height: 10x5 child=0grid grid width=auto height=100% tall min-height: 120x60 child=0grid grid width=auto height=100% tall padding: 20x10 child=0-grid grid width=auto height=calc empty none: 0x10 child=5-grid grid width=auto height=calc empty max-height: 0x10 child=5+grid grid width=auto height=calc empty none: 20x10 child=5+grid grid width=auto height=calc empty max-height: 20x10 child=5grid grid width=auto height=calc empty min-height: 120x60 child=30-grid grid width=auto height=calc empty padding: 20x15 child=2.5-grid grid width=auto height=calc tall none: 10x12.5 child=0-grid grid width=auto height=calc tall max-height: 10x12.5 child=0+grid grid width=auto height=calc empty padding: 30x15 child=2.5+grid grid width=auto height=calc tall none: 25x12.5 child=0+grid grid width=auto height=calc tall max-height: 25x12.5 child=0grid grid width=auto height=calc tall min-height: 120x60 child=0-grid grid width=auto height=calc tall padding: 20x15 child=0+grid grid width=auto height=calc tall padding: 30x15 child=0grid grid width=auto height=auto empty none: 0x0 child=0grid grid width=auto height=auto empty max-height: 0x0 child=0grid grid width=auto height=auto empty min-height: 120x60 child=30