Skip to content

Truly independent web browser

LibWeb: Keep the precision of fractional flex factors in grid layout

Andreas Kling070f4680d762
diff --git a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
index 3cc06742682..81e78aabba8 100644
--- a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
@@ -5320,8 +5320,7 @@ pub(crate) fn expand_flexible_tracks_indefinite(tracks: &mut [Track<'_>], items:
// the track’s base size by its flex factor; otherwise, the track’s base size.
for track in tracks.iter() {
if let Some(factor) = track.max_sizing.flex_factor() {
- let divisor = CssPixels::nearest_value_for(factor.max(1.0));
- flex_fraction = flex_fraction.max(formatting_context::PixelFraction::new(track.base_size, divisor));
+ flex_fraction = flex_fraction.max(fr_size_fraction(track.base_size, factor.max(1.0)));
}
}
// For each grid item that crosses a flexible track, the result of finding the size of an fr using all the
@@ -5338,9 +5337,7 @@ pub(crate) fn expand_flexible_tracks_indefinite(tracks: &mut [Track<'_>], items:
}
for track in tracks {
if let Some(factor) = track.max_sizing.flex_factor() {
- track.base_size = track
- .base_size
- .max(flex_fraction.multiply(CssPixels::nearest_value_for(factor)));
+ track.base_size = track.base_size.max(size_of_flex_factor(flex_fraction, factor));
}
}
}
@@ -6031,6 +6028,20 @@ pub(crate) fn initialize_track_sizes(tracks: &mut [Track<'_>], available: Availa
has_flexible_tracks
}
+/// The size of an fr as `space` divided by a sum of flex factors, keeping the precision of fractional flex factors.
+fn fr_size_fraction(space: CssPixels, flex_factor_sum: f64) -> formatting_context::PixelFraction {
+ let (numerator, denominator) = CssPixels::fraction_nearest_values_for(space.to_double(), flex_factor_sum);
+ if denominator == CssPixels::default() {
+ return formatting_context::PixelFraction::zero();
+ }
+ formatting_context::PixelFraction::new(numerator, denominator)
+}
+
+/// The product of the size of an fr and a flex factor.
+fn size_of_flex_factor(fr_size: formatting_context::PixelFraction, flex_factor: f64) -> CssPixels {
+ CssPixels::nearest_value_for(fr_size.numerator.to_double() / fr_size.denominator.to_double() * flex_factor)
+}
+
pub(crate) fn find_fr_size(tracks: &[Track<'_>], space_to_fill: CssPixels) -> formatting_context::PixelFraction {
// https://www.w3.org/TR/css-grid-2/#algo-find-fr-size
let mut inflexible = vec![false; tracks.len()];
@@ -6045,20 +6056,20 @@ pub(crate) fn find_fr_size(tracks: &[Track<'_>], space_to_fill: CssPixels) -> fo
// 2. Let flex factor sum be the sum of the flex factors of the flexible tracks.
// If this value is less than 1, set it to 1 instead.
- let mut flex_factor_sum = CssPixels::default();
+ let mut flex_factor_sum = 0.0;
for (index, track) in tracks.iter().enumerate() {
if inflexible[index] {
continue;
}
if let Some(factor) = track.max_sizing.flex_factor() {
- flex_factor_sum += CssPixels::nearest_value_for(factor);
+ flex_factor_sum += factor;
}
}
- if flex_factor_sum < CssPixels::from_integer(1) {
- flex_factor_sum = CssPixels::from_integer(1);
+ if flex_factor_sum < 1.0 {
+ flex_factor_sum = 1.0;
}
// 3. Let the hypothetical fr size be the leftover space divided by the flex factor sum.
- let hypothetical_fr_size = formatting_context::PixelFraction::new(leftover_space, flex_factor_sum);
+ let hypothetical_fr_size = fr_size_fraction(leftover_space, flex_factor_sum);
// 4. If the product of the hypothetical fr size and a flexible track’s flex factor is less than the track’s
// base size, restart this algorithm treating all such tracks as inflexible.
@@ -6070,7 +6081,7 @@ pub(crate) fn find_fr_size(tracks: &[Track<'_>], space_to_fill: CssPixels) -> fo
let Some(factor) = track.max_sizing.flex_factor() else {
continue;
};
- let scaled = hypothetical_fr_size.multiply(CssPixels::nearest_value_for(factor));
+ let scaled = size_of_flex_factor(hypothetical_fr_size, factor);
if scaled < track.base_size {
inflexible[index] = true;
restart = true;
@@ -6091,7 +6102,7 @@ pub(crate) fn expand_flexible_tracks(tracks: &mut [Track<'_>], space_to_fill: Cs
let Some(factor) = track.max_sizing.flex_factor() else {
continue;
};
- let scaled = flex_fraction.multiply(CssPixels::nearest_value_for(factor));
+ let scaled = size_of_flex_factor(flex_fraction, factor);
if scaled > track.base_size {
track.base_size = scaled;
}
diff --git a/Tests/LibWeb/Layout/expected/grid/fixed-tracks-followed-by-repeat-auto-fill.txt b/Tests/LibWeb/Layout/expected/grid/fixed-tracks-followed-by-repeat-auto-fill.txt
index b38cbd64cfd..e1b440e73b3 100644
--- a/Tests/LibWeb/Layout/expected/grid/fixed-tracks-followed-by-repeat-auto-fill.txt
+++ b/Tests/LibWeb/Layout/expected/grid/fixed-tracks-followed-by-repeat-auto-fill.txt
@@ -16,38 +16,38 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
TextNode <#text> (not painted)
BlockContainer <(anonymous)> (not painted) [BFC] children: inline
TextNode <#text> (not painted)
- BlockContainer <div.grid-item> at [238,18] [0+0+10 33.765625 10+0+0] [0+0+10 14 10+0+0] [BFC] children: inline
+ BlockContainer <div.grid-item> at [238,18] [0+0+10 33.78125 10+0+0] [0+0+10 14 10+0+0] [BFC] children: inline
frag 0 from TextNode start: 0, length: 1, rect: [250.90625,18 7.953125x14] baseline: 11.203125
"3"
TextNode <#text> (not painted)
BlockContainer <(anonymous)> (not painted) [BFC] children: inline
TextNode <#text> (not painted)
- BlockContainer <div.grid-item> at [301.765625,18] [0+0+10 33.765625 10+0+0] [0+0+10 14 10+0+0] [BFC] children: inline
- frag 0 from TextNode start: 0, length: 1, rect: [315.25,18 6.78125x14] baseline: 11.203125
+ BlockContainer <div.grid-item> at [301.78125,18] [0+0+10 33.78125 10+0+0] [0+0+10 14 10+0+0] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 1, rect: [315.28125,18 6.78125x14] baseline: 11.203125
"4"
TextNode <#text> (not painted)
BlockContainer <(anonymous)> (not painted) [BFC] children: inline
TextNode <#text> (not painted)
- BlockContainer <div.grid-item> at [365.53125,18] [0+0+10 33.765625 10+0+0] [0+0+10 14 10+0+0] [BFC] children: inline
- frag 0 from TextNode start: 0, length: 1, rect: [378.71875,18 7.390625x14] baseline: 11.203125
+ BlockContainer <div.grid-item> at [365.5625,18] [0+0+10 33.78125 10+0+0] [0+0+10 14 10+0+0] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 1, rect: [378.75,18 7.390625x14] baseline: 11.203125
"5"
TextNode <#text> (not painted)
BlockContainer <(anonymous)> (not painted) [BFC] children: inline
TextNode <#text> (not painted)
- BlockContainer <div.grid-item> at [429.296875,18] [0+0+10 33.765625 10+0+0] [0+0+10 14 10+0+0] [BFC] children: inline
- frag 0 from TextNode start: 0, length: 1, rect: [442.359375,18 7.640625x14] baseline: 11.203125
+ BlockContainer <div.grid-item> at [429.34375,18] [0+0+10 33.78125 10+0+0] [0+0+10 14 10+0+0] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 1, rect: [442.40625,18 7.640625x14] baseline: 11.203125
"6"
TextNode <#text> (not painted)
BlockContainer <(anonymous)> (not painted) [BFC] children: inline
TextNode <#text> (not painted)
- BlockContainer <div.grid-item> at [493.0625,18] [0+0+10 33.765625 10+0+0] [0+0+10 14 10+0+0] [BFC] children: inline
- frag 0 from TextNode start: 0, length: 1, rect: [506.125,18 7.625x14] baseline: 11.203125
+ BlockContainer <div.grid-item> at [493.125,18] [0+0+10 33.78125 10+0+0] [0+0+10 14 10+0+0] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 1, rect: [506.203125,18 7.625x14] baseline: 11.203125
"7"
TextNode <#text> (not painted)
BlockContainer <(anonymous)> (not painted) [BFC] children: inline
TextNode <#text> (not painted)
- BlockContainer <div.grid-item> at [556.828125,18] [0+0+10 33.765625 10+0+0] [0+0+10 14 10+0+0] [BFC] children: inline
- frag 0 from TextNode start: 0, length: 1, rect: [569.5625,18 8.296875x14] baseline: 11.203125
+ BlockContainer <div.grid-item> at [556.90625,18] [0+0+10 33.78125 10+0+0] [0+0+10 14 10+0+0] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 1, rect: [569.640625,18 8.296875x14] baseline: 11.203125
"8"
TextNode <#text> (not painted)
BlockContainer <(anonymous)> (not painted) [BFC] children: inline
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-grid/grid-definition/flex-factor-sum-less-than-1-001.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-grid/grid-definition/flex-factor-sum-less-than-1-001.txt
index 0c9973dfbc5..0016bacd96f 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-grid/grid-definition/flex-factor-sum-less-than-1-001.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-grid/grid-definition/flex-factor-sum-less-than-1-001.txt
@@ -2,9 +2,8 @@ Harness status: OK
Found 4 tests
-2 Pass
-2 Fail
-Fail .grid 1
-Fail .grid 2
+4 Pass
+Pass .grid 1
+Pass .grid 2
Pass .grid 3
Pass .grid 4
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-grid/grid-tracks-fractional-fr.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-grid/grid-tracks-fractional-fr.txt
index ad85c2f8dc5..8c4699dc77e 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-grid/grid-tracks-fractional-fr.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-grid/grid-tracks-fractional-fr.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail Tests fractional fr-units which should multiply to whole numbers.
\ No newline at end of file
+1 Pass
+Pass Tests fractional fr-units which should multiply to whole numbers.
\ No newline at end of file