tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 208Rust 7TOML 3CSS 1294 Sutichaykuna

⎇ 9958624168dfe671c82cbe3afb8ba6a7d35f7454 tests/generated/grid_max_width_greater_than_max_content.rs Rust · 81 siq'ikuna · 4.0 KB Ñawpa willay Pi imata llamkarqan ch'uya

Fix: Make CSS Grid algorithm correctly apply max width/height and available space when it is the root node (#491) 995862416 · Nico Burns ·

1#[test]
2fn grid_max_width_greater_than_max_content() {
3    #[allow(unused_imports)]
4    use taffy::{prelude::*, tree::Layout};
5    let mut taffy = taffy::Taffy::new();
6    let node00 = taffy
7        .new_leaf_with_measure(
8            taffy::style::Style { ..Default::default() },
9            taffy::tree::MeasureFunc::Raw(|known_dimensions, available_space| {
10                const TEXT: &str = "HH\u{200b}HH\u{200b}HH\u{200b}HH";
11                super::measure_standard_text(
12                    known_dimensions,
13                    available_space,
14                    TEXT,
15                    super::WritingMode::Horizontal,
16                    None,
17                )
18            }),
19        )
20        .unwrap();
21    let node01 = taffy
22        .new_leaf_with_measure(
23            taffy::style::Style { ..Default::default() },
24            taffy::tree::MeasureFunc::Raw(|known_dimensions, available_space| {
25                const TEXT: &str = "HH\u{200b}HH\u{200b}HH\u{200b}HH";
26                super::measure_standard_text(
27                    known_dimensions,
28                    available_space,
29                    TEXT,
30                    super::WritingMode::Horizontal,
31                    None,
32                )
33            }),
34        )
35        .unwrap();
36    let node0 = taffy
37        .new_with_children(
38            taffy::style::Style {
39                display: taffy::style::Display::Grid,
40                grid_template_columns: vec![auto(), auto()],
41                max_size: taffy::geometry::Size { width: taffy::style::Dimension::Length(400f32), height: auto() },
42                ..Default::default()
43            },
44            &[node00, node01],
45        )
46        .unwrap();
47    let node = taffy
48        .new_with_children(
49            taffy::style::Style {
50                display: taffy::style::Display::Grid,
51                grid_template_columns: vec![max_content()],
52                ..Default::default()
53            },
54            &[node0],
55        )
56        .unwrap();
57    taffy.compute_layout(node, taffy::geometry::Size::MAX_CONTENT).unwrap();
58    println!("\nComputed tree:");
59    taffy::util::print_tree(&taffy, node);
60    println!();
61    let Layout { size, location, .. } = taffy.layout(node).unwrap();
62    assert_eq!(size.width, 160f32, "width of node {:?}. Expected {}. Actual {}", node, 160f32, size.width);
63    assert_eq!(size.height, 10f32, "height of node {:?}. Expected {}. Actual {}", node, 10f32, size.height);
64    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node, 0f32, location.x);
65    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node, 0f32, location.y);
66    let Layout { size, location, .. } = taffy.layout(node0).unwrap();
67    assert_eq!(size.width, 160f32, "width of node {:?}. Expected {}. Actual {}", node0, 160f32, size.width);
68    assert_eq!(size.height, 10f32, "height of node {:?}. Expected {}. Actual {}", node0, 10f32, size.height);
69    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node0, 0f32, location.x);
70    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node0, 0f32, location.y);
71    let Layout { size, location, .. } = taffy.layout(node00).unwrap();
72    assert_eq!(size.width, 80f32, "width of node {:?}. Expected {}. Actual {}", node00, 80f32, size.width);
73    assert_eq!(size.height, 10f32, "height of node {:?}. Expected {}. Actual {}", node00, 10f32, size.height);
74    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node00, 0f32, location.x);
75    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node00, 0f32, location.y);
76    let Layout { size, location, .. } = taffy.layout(node01).unwrap();
77    assert_eq!(size.width, 80f32, "width of node {:?}. Expected {}. Actual {}", node01, 80f32, size.width);
78    assert_eq!(size.height, 10f32, "height of node {:?}. Expected {}. Actual {}", node01, 10f32, size.height);
79    assert_eq!(location.x, 80f32, "x of node {:?}. Expected {}. Actual {}", node01, 80f32, location.x);
80    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node01, 0f32, location.y);
81}