Clamp block item stretch widths by their min and max width (#664)
* Clamp the stretch width of block items
* Add tests for min and max width for block items
9129d5adbe275e09942bc7207e9f992435bbced3 · ruwaq Nico Burns · (2 wata ñawpaq) · tayta 3526f73a4 · kay commitpi sach'ata qhaway
Semántico sayk'uy · 5
tests/generated/block/block_item_max_width.rs
tests/generated/block/block_item_min_width_overrides_max_width.rs
tests/generated/block/mod.rs
diff --git a/src/compute/block.rs b/src/compute/block.rs
index 7e6d300be..0b09947a0 100644
--- a/src/compute/block.rs
+++ b/src/compute/block.rs
@@ -361,7 +361,13 @@ fn perform_final_layout_on_in_flow_children(
let item_non_auto_x_margin_sum = item_non_auto_margin.horizontal_axis_sum();
let known_dimensions = item
.size
- .map_width(|width| Some(width.unwrap_or(container_inner_width - item_non_auto_x_margin_sum)))
+ .map_width(|width| {
+ Some(
+ width
+ .unwrap_or(container_inner_width - item_non_auto_x_margin_sum)
+ .maybe_clamp(item.min_size.width, item.max_size.width),
+ )
+ })
.maybe_clamp(item.min_size, item.max_size);
let item_layout = tree.perform_child_layout(
diff --git a/test_fixtures/block/block_item_max_width.html b/test_fixtures/block/block_item_max_width.html
new file mode 100644
index 000000000..a3e22cb37
--- /dev/null
+++ b/test_fixtures/block/block_item_max_width.html
@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <script src="../../scripts/gentest/test_helper.js"></script>
+ <link rel="stylesheet" type="text/css" href="../../scripts/gentest/test_base_style.css">
+ <title>
+ Test description
+ </title>
+</head>
+<body>
+
+<div id="test-root" style="display: block; width: 200px; height: 200px;">
+ <div style="max-width: 100px; height: 50px;"></div>
+ <div style="max-width: 300px; height: 50px;"></div>
+</div>
+
+</body>
+</html>
\ No newline at end of file
diff --git a/test_fixtures/block/block_item_min_width_overrides_max_width.html b/test_fixtures/block/block_item_min_width_overrides_max_width.html
new file mode 100644
index 000000000..8dc12bd36
--- /dev/null
+++ b/test_fixtures/block/block_item_min_width_overrides_max_width.html
@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <script src="../../scripts/gentest/test_helper.js"></script>
+ <link rel="stylesheet" type="text/css" href="../../scripts/gentest/test_base_style.css">
+ <title>
+ Test description
+ </title>
+</head>
+<body>
+
+<div id="test-root" style="display: block; width: 100px; height: 100px;">
+ <div style="min-width: 200px; max-width: 50px; height: 50px;"></div>
+</div>
+
+</body>
+</html>
\ No newline at end of file
diff --git a/tests/generated/block/block_item_max_width.rs b/tests/generated/block/block_item_max_width.rs
new file mode 100644
index 000000000..b95af00bc
--- /dev/null
+++ b/tests/generated/block/block_item_max_width.rs
@@ -0,0 +1,109 @@
+#[test]
+fn block_item_max_width() {
+ #[allow(unused_imports)]
+ use taffy::{prelude::*, tree::Layout, TaffyTree};
+ let mut taffy: TaffyTree<crate::TextMeasure> = TaffyTree::new();
+ let node0 = taffy
+ .new_leaf(taffy::style::Style {
+ size: taffy::geometry::Size { width: auto(), height: taffy::style::Dimension::Length(50f32) },
+ max_size: taffy::geometry::Size { width: taffy::style::Dimension::Length(100f32), height: auto() },
+ ..Default::default()
+ })
+ .unwrap();
+ let node1 = taffy
+ .new_leaf(taffy::style::Style {
+ size: taffy::geometry::Size { width: auto(), height: taffy::style::Dimension::Length(50f32) },
+ max_size: taffy::geometry::Size { width: taffy::style::Dimension::Length(300f32), height: auto() },
+ ..Default::default()
+ })
+ .unwrap();
+ let node = taffy
+ .new_with_children(
+ taffy::style::Style {
+ display: taffy::style::Display::Block,
+ size: taffy::geometry::Size {
+ width: taffy::style::Dimension::Length(200f32),
+ height: taffy::style::Dimension::Length(200f32),
+ },
+ ..Default::default()
+ },
+ &[node0, node1],
+ )
+ .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, 200f32, "width of node {:?}. Expected {}. Actual {}", node, 200f32, size.width);
+ assert_eq!(size.height, 200f32, "height of node {:?}. Expected {}. Actual {}", node, 200f32, 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, 100f32, "width of node {:?}. Expected {}. Actual {}", node0, 100f32, size.width);
+ assert_eq!(size.height, 50f32, "height of node {:?}. Expected {}. Actual {}", node0, 50f32, 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(node1).unwrap();
+ assert_eq!(size.width, 200f32, "width of node {:?}. Expected {}. Actual {}", node1, 200f32, size.width);
+ assert_eq!(size.height, 50f32, "height of node {:?}. Expected {}. Actual {}", node1, 50f32, size.height);
+ assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node1, 0f32, location.x);
+ assert_eq!(location.y, 50f32, "y of node {:?}. Expected {}. Actual {}", node1, 50f32, location.y);
+ #[cfg(feature = "content_size")]
+ assert_eq!(
+ layout.scroll_width(),
+ 0f32,
+ "scroll_width of node {:?}. Expected {}. Actual {}",
+ node1,
+ 0f32,
+ layout.scroll_width()
+ );
+ #[cfg(feature = "content_size")]
+ assert_eq!(
+ layout.scroll_height(),
+ 0f32,
+ "scroll_height of node {:?}. Expected {}. Actual {}",
+ node1,
+ 0f32,
+ layout.scroll_height()
+ );
+}
diff --git a/tests/generated/block/block_item_min_width_overrides_max_width.rs b/tests/generated/block/block_item_min_width_overrides_max_width.rs
new file mode 100644
index 000000000..02e381b3d
--- /dev/null
+++ b/tests/generated/block/block_item_min_width_overrides_max_width.rs
@@ -0,0 +1,79 @@
+#[test]
+fn block_item_min_width_overrides_max_width() {
+ #[allow(unused_imports)]
+ use taffy::{prelude::*, tree::Layout, TaffyTree};
+ let mut taffy: TaffyTree<crate::TextMeasure> = TaffyTree::new();
+ let node0 = taffy
+ .new_leaf(taffy::style::Style {
+ size: taffy::geometry::Size { width: auto(), height: taffy::style::Dimension::Length(50f32) },
+ min_size: taffy::geometry::Size { width: taffy::style::Dimension::Length(200f32), height: auto() },
+ max_size: taffy::geometry::Size { width: taffy::style::Dimension::Length(50f32), height: auto() },
+ ..Default::default()
+ })
+ .unwrap();
+ let node = taffy
+ .new_with_children(
+ taffy::style::Style {
+ display: taffy::style::Display::Block,
+ size: taffy::geometry::Size {
+ width: taffy::style::Dimension::Length(100f32),
+ height: taffy::style::Dimension::Length(100f32),
+ },
+ ..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, 100f32, "width of node {:?}. Expected {}. Actual {}", node, 100f32, size.width);
+ assert_eq!(size.height, 100f32, "height of node {:?}. Expected {}. Actual {}", node, 100f32, 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(),
+ 100f32,
+ "scroll_width of node {:?}. Expected {}. Actual {}",
+ node,
+ 100f32,
+ 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, 200f32, "width of node {:?}. Expected {}. Actual {}", node0, 200f32, size.width);
+ assert_eq!(size.height, 50f32, "height of node {:?}. Expected {}. Actual {}", node0, 50f32, 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()
+ );
+}
diff --git a/tests/generated/block/mod.rs b/tests/generated/block/mod.rs
index 2d788da3c..ec4df52ee 100644
--- a/tests/generated/block/mod.rs
+++ b/tests/generated/block/mod.rs
@@ -78,6 +78,8 @@ mod block_display_none_with_position_absolute;
mod block_inset_fixed;
mod block_inset_percentage;
mod block_intrinsic_width;
+mod block_item_max_width;
+mod block_item_min_width_overrides_max_width;
mod block_margin_auto_bottom;
mod block_margin_auto_bottom_and_top;
mod block_margin_auto_left;