gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 5544Rust 88Markdown 11TOML 91926 Definitions

⎇ 93b32fa75e2f62a143953f9276aa6306ae7e3b39 tests/hand_written/floats.rs Rust · 53 lines · 2.1 KB History Who touched what raw

Fix panic in FloatContext::subdivide_segment and enforce CSS2 float rule 5 (#988) 93b32fa75 · Nico Burns ·

1#![cfg(feature = "float_layout")]
2use taffy::geometry::Point;
3use taffy::prelude::*;
4use taffy::style::Float;
5use taffy_test_helpers::new_test_tree;
6
7/// Regression test for <https://wpt.live/css/CSS2/floats-clear/floats-146.xht>
8///
9/// A right float placed after two left floats must not be placed higher than the
10/// outer top of the earlier floats (CSS2 float rule 5). This scenario previously
11/// panicked in `FloatContext::subdivide_segment` when the float's end coincided
12/// with an existing segment boundary due to floating point imprecision.
13#[test]
14fn float_no_higher_than_earlier_floats() {
15    let mut taffy = new_test_tree();
16
17    let block = |width: f32, height: f32, float: Float| Style {
18        display: Display::Block,
19        float,
20        size: Size { width: length(width), height: length(height) },
21        ..Default::default()
22    };
23
24    // Spacer block advancing the flow position to a fractional y offset (43.2)
25    let spacer = taffy.new_leaf(block(400.0, 43.2, Float::None)).unwrap();
26    let float_a = taffy.new_leaf(block(258.0, 42.0, Float::Left)).unwrap();
27    let float_b = taffy.new_leaf(block(298.0, 42.0, Float::Left)).unwrap();
28    let float_c = taffy.new_leaf(block(98.0, 42.0, Float::Right)).unwrap();
29
30    let root = taffy
31        .new_with_children(
32            Style {
33                display: Display::Block,
34                size: Size { width: length(400.0), height: auto() },
35                ..Default::default()
36            },
37            &[spacer, float_a, float_b, float_c],
38        )
39        .unwrap();
40
41    taffy.compute_layout(root, Size::MAX_CONTENT).unwrap();
42
43    let layout_a = taffy.layout(float_a).unwrap();
44    let layout_b = taffy.layout(float_b).unwrap();
45    let layout_c = taffy.layout(float_c).unwrap();
46
47    // A is placed below the spacer
48    assert_eq!(layout_a.location, Point { x: 0.0, y: 43.0 });
49    // B does not fit next to A, so is placed below it
50    assert_eq!(layout_b.location, Point { x: 0.0, y: 85.0 });
51    // C must not be placed higher than B (it fits next to B on the right)
52    assert_eq!(layout_c.location, Point { x: 302.0, y: 85.0 });
53}