From be60ff4d45fe5cd656eaa0a032e8415e7a84c82b Mon Sep 17 00:00:00 2001 From: Nico Burns Date: Tue, 14 May 2024 14:34:41 +0100 Subject: [PATCH] Use grid area size rather than available space when applying aspect ratio to grid items --- RELEASES.md | 1 + src/compute/grid/types/grid_item.rs | 4 +- .../grid/chrome_issue_325928327.html | 21 ++++ .../generated/grid/chrome_issue_325928327.rs | 112 ++++++++++++++++++ tests/generated/grid/mod.rs | 1 + 5 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 test_fixtures/grid/chrome_issue_325928327.html create mode 100644 tests/generated/grid/chrome_issue_325928327.rs diff --git a/RELEASES.md b/RELEASES.md index c76236dda..66d121561 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -6,6 +6,7 @@ - Content alignment (`align-content`/`justify-content`) behaviour was updated to match the latest spec (and Chrome 123+) (#635) - Ensure that root Flexbox nodes are floored by their padding-border (#651, #655) +- Use grid area size not available space when applying aspect ratio to grid containers (#656) ## 0.4.3 diff --git a/src/compute/grid/types/grid_item.rs b/src/compute/grid/types/grid_item.rs index 5f0e7906b..ba18bc58a 100644 --- a/src/compute/grid/types/grid_item.rs +++ b/src/compute/grid/types/grid_item.rs @@ -348,7 +348,7 @@ impl GridItem { tree.measure_child_size( self.node, known_dimensions, - available_space, + inner_node_size, available_space.map(|opt| match opt { Some(size) => AvailableSpace::Definite(size), None => AvailableSpace::MinContent, @@ -387,7 +387,7 @@ impl GridItem { tree.measure_child_size( self.node, known_dimensions, - available_space, + inner_node_size, available_space.map(|opt| match opt { Some(size) => AvailableSpace::Definite(size), None => AvailableSpace::MaxContent, diff --git a/test_fixtures/grid/chrome_issue_325928327.html b/test_fixtures/grid/chrome_issue_325928327.html new file mode 100644 index 000000000..0604d6b37 --- /dev/null +++ b/test_fixtures/grid/chrome_issue_325928327.html @@ -0,0 +1,21 @@ + + + + + + + Test description + + + + +
+
+
+
+
+
+
+ + + \ No newline at end of file diff --git a/tests/generated/grid/chrome_issue_325928327.rs b/tests/generated/grid/chrome_issue_325928327.rs new file mode 100644 index 000000000..b2c65685c --- /dev/null +++ b/tests/generated/grid/chrome_issue_325928327.rs @@ -0,0 +1,112 @@ +#[test] +fn chrome_issue_325928327() { + #[allow(unused_imports)] + use taffy::{prelude::*, tree::Layout, TaffyTree}; + let mut taffy: TaffyTree = TaffyTree::new(); + let node00 = taffy + .new_leaf(taffy::style::Style { + size: taffy::geometry::Size { width: auto(), height: taffy::style::Dimension::Percent(1f32) }, + aspect_ratio: Some(1f32), + ..Default::default() + }) + .unwrap(); + let node0 = taffy + .new_with_children( + taffy::style::Style { + size: taffy::geometry::Size { width: auto(), height: taffy::style::Dimension::Percent(1f32) }, + ..Default::default() + }, + &[node00], + ) + .unwrap(); + let node = taffy + .new_with_children( + taffy::style::Style { + display: taffy::style::Display::Grid, + justify_items: Some(taffy::style::JustifyItems::Center), + size: taffy::geometry::Size { + width: taffy::style::Dimension::Percent(1f32), + height: taffy::style::Dimension::Length(40f32), + }, + ..Default::default() + }, + &[node0], + ) + .unwrap(); + taffy.compute_layout_with_measure(node, taffy::geometry::Size::MAX_CONTENT, crate::test_measure_function).unwrap(); + println!("\nComputed tree:"); + taffy.print_tree(node); + println!(); + #[cfg_attr(not(feature = "content_size"), allow(unused_variables))] + let layout @ Layout { size, location, .. } = taffy.layout(node).unwrap(); + assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node, 40f32, size.width); + assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node, 40f32, size.height); + assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node, 0f32, location.x); + assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node, 0f32, location.y); + #[cfg(feature = "content_size")] + assert_eq!( + layout.scroll_width(), + 0f32, + "scroll_width of node {:?}. Expected {}. Actual {}", + node, + 0f32, + layout.scroll_width() + ); + #[cfg(feature = "content_size")] + assert_eq!( + layout.scroll_height(), + 0f32, + "scroll_height of node {:?}. Expected {}. Actual {}", + node, + 0f32, + layout.scroll_height() + ); + #[cfg_attr(not(feature = "content_size"), allow(unused_variables))] + let layout @ Layout { size, location, .. } = taffy.layout(node0).unwrap(); + assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node0, 40f32, size.width); + assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node0, 40f32, size.height); + assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node0, 0f32, location.x); + assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node0, 0f32, location.y); + #[cfg(feature = "content_size")] + assert_eq!( + layout.scroll_width(), + 0f32, + "scroll_width of node {:?}. Expected {}. Actual {}", + node0, + 0f32, + layout.scroll_width() + ); + #[cfg(feature = "content_size")] + assert_eq!( + layout.scroll_height(), + 0f32, + "scroll_height of node {:?}. Expected {}. Actual {}", + node0, + 0f32, + layout.scroll_height() + ); + #[cfg_attr(not(feature = "content_size"), allow(unused_variables))] + let layout @ Layout { size, location, .. } = taffy.layout(node00).unwrap(); + assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node00, 40f32, size.width); + assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node00, 40f32, size.height); + assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node00, 0f32, location.x); + assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node00, 0f32, location.y); + #[cfg(feature = "content_size")] + assert_eq!( + layout.scroll_width(), + 0f32, + "scroll_width of node {:?}. Expected {}. Actual {}", + node00, + 0f32, + layout.scroll_width() + ); + #[cfg(feature = "content_size")] + assert_eq!( + layout.scroll_height(), + 0f32, + "scroll_height of node {:?}. Expected {}. Actual {}", + node00, + 0f32, + layout.scroll_height() + ); +} diff --git a/tests/generated/grid/mod.rs b/tests/generated/grid/mod.rs index 6dccc9a9c..031519195 100644 --- a/tests/generated/grid/mod.rs +++ b/tests/generated/grid/mod.rs @@ -1,3 +1,4 @@ +mod chrome_issue_325928327; #[cfg(feature = "grid")] mod grid_absolute_align_self_sized_all; #[cfg(feature = "grid")]