Add support for display: flow-root
1#![cfg(all(feature = "block_layout", feature = "float_layout"))]
2
3use taffy::geometry::{Point, Rect, Size};
4use taffy::prelude::*;
5use taffy::style::Float;
6use taffy_test_helpers::new_test_tree;
7
8#[test]
9fn flow_root_does_not_collapse_margins_through_parent() {
10 let child_style = Style {
11 display: Display::Block,
12 size: Size { width: length(40.0), height: length(10.0) },
13 margin: Rect { top: length(20.0), bottom: length(30.0), ..Rect::zero() },
14 ..Default::default()
15 };
16
17 let mut taffy = new_test_tree();
18 let plain_child = taffy.new_leaf(child_style.clone()).unwrap();
19 let plain_parent = taffy
20 .new_with_children(
21 Style {
22 display: Display::Block,
23 size: Size { width: length(100.0), height: auto() },
24 ..Default::default()
25 },
26 &[plain_child],
27 )
28 .unwrap();
29
30 let flow_child = taffy.new_leaf(child_style).unwrap();
31 let flow_parent = taffy
32 .new_with_children(
33 Style {
34 display: Display::FlowRoot,
35 size: Size { width: length(100.0), height: auto() },
36 ..Default::default()
37 },
38 &[flow_child],
39 )
40 .unwrap();
41 let root = taffy
42 .new_with_children(
43 Style {
44 display: Display::Block,
45 size: Size { width: length(200.0), height: auto() },
46 ..Default::default()
47 },
48 &[plain_parent, flow_parent],
49 )
50 .unwrap();
51 taffy.compute_layout(root, Size::MAX_CONTENT).unwrap();
52
53 assert_eq!(taffy.layout(plain_parent).unwrap().size.height, 10.0);
54 assert_eq!(taffy.layout(flow_parent).unwrap().size.height, 60.0);
55 assert_eq!(taffy.layout(flow_child).unwrap().location.y, 20.0);
56}
57
58#[test]
59fn flow_root_contains_floated_children() {
60 let mut taffy = new_test_tree();
61 let float = taffy
62 .new_leaf(Style {
63 display: Display::Block,
64 float: Float::Left,
65 size: Size { width: length(20.0), height: length(30.0) },
66 ..Default::default()
67 })
68 .unwrap();
69 let flow_root = taffy
70 .new_with_children(
71 Style {
72 display: Display::FlowRoot,
73 size: Size { width: length(100.0), height: auto() },
74 ..Default::default()
75 },
76 &[float],
77 )
78 .unwrap();
79 taffy.compute_layout(flow_root, Size::MAX_CONTENT).unwrap();
80
81 assert_eq!(taffy.layout(flow_root).unwrap().size.height, 30.0);
82}
83
84#[test]
85fn flow_root_sibling_avoids_float() {
86 let mut taffy = new_test_tree();
87 let float = taffy
88 .new_leaf(Style {
89 display: Display::Block,
90 float: Float::Left,
91 size: Size { width: length(30.0), height: length(20.0) },
92 ..Default::default()
93 })
94 .unwrap();
95 let flow_child =
96 taffy.new_leaf(Style { size: Size { width: auto(), height: length(10.0) }, ..Default::default() }).unwrap();
97 let flow_root = taffy
98 .new_with_children(
99 Style { display: Display::FlowRoot, size: Size { width: auto(), height: auto() }, ..Default::default() },
100 &[flow_child],
101 )
102 .unwrap();
103 let parent = taffy
104 .new_with_children(
105 Style {
106 display: Display::Block,
107 size: Size { width: length(100.0), height: auto() },
108 ..Default::default()
109 },
110 &[float, flow_root],
111 )
112 .unwrap();
113 taffy.compute_layout(parent, Size::MAX_CONTENT).unwrap();
114
115 assert_eq!(taffy.layout(flow_root).unwrap().location, Point { x: 30.0, y: 0.0 });
116 assert_eq!(taffy.layout(flow_root).unwrap().size.width, 70.0);
117}