strata-layout 0.1.2

Chromium-parity Box Model, Flexbox (L1), and CSS Grid (L2) layout engine in pure Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! Verification Suite for CSS Positioning, Containing Blocks, and Stacking Schemes.

#![allow(clippy::field_reassign_with_default)]

use strata_layout::{ContainingBlock, LayoutNode, Rect};
use strata_primitives::geom::EdgeInsets;
use strata_style::types::{ComputedStyle, Dimension, Display, Position};

#[test]
fn test_containing_block_resolution_across_static_ancestors() {
    // Relative outer container (800 x 600)
    let mut root_style = ComputedStyle::default();
    root_style.display = Display::Block;
    root_style.position = Position::Relative;
    root_style.width = Dimension::Px(800.0);
    root_style.height = Dimension::Px(600.0);

    // Static intermediate wrapper with offset padding
    let mut static_style = ComputedStyle::default();
    static_style.display = Display::Block;
    static_style.position = Position::Static;
    static_style.margin = EdgeInsets::all(40.0);
    static_style.width = Dimension::Px(400.0);
    static_style.height = Dimension::Px(300.0);

    // Absolute child inside static wrapper: top: 15, right: 25, w: 120, h: 60
    let mut abs_style = ComputedStyle::default();
    abs_style.display = Display::Block;
    abs_style.position = Position::Absolute;
    abs_style.insets.top = Dimension::Px(15.0);
    abs_style.insets.right = Dimension::Px(25.0);
    abs_style.width = Dimension::Px(120.0);
    abs_style.height = Dimension::Px(60.0);

    let abs_node = LayoutNode::new(abs_style);
    let static_node = LayoutNode::with_children(static_style, vec![abs_node]);
    let mut root = LayoutNode::with_children(root_style, vec![static_node]);

    root.compute_layout(800.0, 600.0);

    // The absolute child's containing block must be root's padding box (0, 0, 800, 600)
    // rather than the intermediate static container.
    // x = root.x + root.w - right - w = 0 + 800 - 25 - 120 = 655.0
    // y = root.y + top = 0 + 15 = 15.0
    let abs_geom = &root.children[0].children[0].geometry.border_box;
    assert_eq!(abs_geom.x, 655.0);
    assert_eq!(abs_geom.y, 15.0);
    assert_eq!(abs_geom.width, 120.0);
    assert_eq!(abs_geom.height, 60.0);
}

#[test]
fn test_double_anchored_horizontal_and_vertical_stretch() {
    // Container: 600 x 500
    let mut root_style = ComputedStyle::default();
    root_style.display = Display::Block;
    root_style.position = Position::Relative;
    root_style.width = Dimension::Px(600.0);
    root_style.height = Dimension::Px(500.0);

    // Inset-stretched child: top: 20, bottom: 30, left: 40, right: 50, auto dimensions
    let mut child_style = ComputedStyle::default();
    child_style.display = Display::Block;
    child_style.position = Position::Absolute;
    child_style.insets.top = Dimension::Px(20.0);
    child_style.insets.bottom = Dimension::Px(30.0);
    child_style.insets.left = Dimension::Px(40.0);
    child_style.insets.right = Dimension::Px(50.0);
    child_style.width = Dimension::Auto;
    child_style.height = Dimension::Auto;

    let mut root = LayoutNode::with_children(root_style, vec![LayoutNode::new(child_style)]);
    root.compute_layout(600.0, 500.0);

    let child_geom = &root.children[0].geometry.border_box;
    // Expected stretched width = 600 - 40 - 50 = 510
    // Expected stretched height = 500 - 20 - 30 = 450
    assert_eq!(child_geom.x, 40.0);
    assert_eq!(child_geom.y, 20.0);
    assert_eq!(child_geom.width, 510.0);
    assert_eq!(child_geom.height, 450.0);
}

#[test]
fn test_absolute_auto_margin_centering() {
    // Container: 800 x 600
    let mut root_style = ComputedStyle::default();
    root_style.display = Display::Block;
    root_style.position = Position::Relative;
    root_style.width = Dimension::Px(800.0);
    root_style.height = Dimension::Px(600.0);

    // Centered modal dialog: inset-0 with auto margins, definite size (300 x 200)
    let mut modal_style = ComputedStyle::default();
    modal_style.display = Display::Block;
    modal_style.position = Position::Absolute;
    modal_style.insets.top = Dimension::Px(0.0);
    modal_style.insets.bottom = Dimension::Px(0.0);
    modal_style.insets.left = Dimension::Px(0.0);
    modal_style.insets.right = Dimension::Px(0.0);
    modal_style.margin_auto_top = true;
    modal_style.margin_auto_bottom = true;
    modal_style.margin_auto_left = true;
    modal_style.margin_auto_right = true;
    modal_style.width = Dimension::Px(300.0);
    modal_style.height = Dimension::Px(200.0);

    let mut root = LayoutNode::with_children(root_style, vec![LayoutNode::new(modal_style)]);
    root.compute_layout(800.0, 600.0);

    let modal_geom = &root.children[0].geometry.border_box;
    // Free horizontal space = 800 - 300 = 500 -> margin = 250 -> x = 250
    // Free vertical space = 600 - 200 = 400 -> margin = 200 -> y = 200
    assert_eq!(modal_geom.x, 250.0);
    assert_eq!(modal_geom.y, 200.0);
    assert_eq!(modal_geom.width, 300.0);
    assert_eq!(modal_geom.height, 200.0);
}

#[test]
fn test_sticky_positioning_clamping_logic() {
    let mut parent = LayoutNode::new(ComputedStyle {
        display: Display::Block,
        position: Position::Relative,
        width: Dimension::Px(400.0),
        height: Dimension::Px(800.0),
        ..Default::default()
    });

    // In-flow header preceding the sticky element
    let header = LayoutNode::new(ComputedStyle {
        display: Display::Block,
        width: Dimension::Px(400.0),
        height: Dimension::Px(100.0),
        ..Default::default()
    });
    parent.add_child(header);

    let mut sticky_child = LayoutNode::new(ComputedStyle {
        display: Display::Block,
        position: Position::Sticky,
        width: Dimension::Px(400.0),
        height: Dimension::Px(50.0),
        ..Default::default()
    });
    sticky_child.style.insets.top = Dimension::Px(10.0);
    parent.add_child(sticky_child);

    // Initial layout at scroll offset (0, 0)
    parent.compute_layout(400.0, 800.0);
    // In normal flow, sticky element sits immediately below the 100px header at y=100
    assert_eq!(parent.children[1].geometry.border_box.y, 100.0);

    // Re-evaluate sticky positioning with scroll offset 150.0
    let cb_scrolled = ContainingBlock::with_scroll(
        parent.geometry.padding_box,
        Rect::new(0.0, 0.0, 400.0, 600.0),
        0.0,
        150.0,
    );
    strata_layout::solve_positioning_children(&mut parent, cb_scrolled);

    // Target y = scroll 150 + top 10 = 160.0, within parent bounds [100, 750]
    assert_eq!(parent.children[1].geometry.border_box.y, 160.0);

    // Re-evaluate sticky positioning with extreme scroll offset 900.0
    let cb_overscrolled = ContainingBlock::with_scroll(
        parent.geometry.padding_box,
        Rect::new(0.0, 0.0, 400.0, 600.0),
        0.0,
        900.0,
    );
    strata_layout::solve_positioning_children(&mut parent, cb_overscrolled);

    // Must be clamped to parent bottom (800) - child height (50) = 750.0
    assert_eq!(parent.children[1].geometry.border_box.y, 750.0);
}

#[test]
fn test_percentage_insets_resolution() {
    let mut root_style = ComputedStyle::default();
    root_style.display = Display::Block;
    root_style.position = Position::Relative;
    root_style.width = Dimension::Px(1000.0);
    root_style.height = Dimension::Px(800.0);

    let mut abs_style = ComputedStyle::default();
    abs_style.display = Display::Block;
    abs_style.position = Position::Absolute;
    abs_style.insets.left = Dimension::Percent(20.0);
    abs_style.insets.top = Dimension::Percent(25.0);
    abs_style.width = Dimension::Percent(50.0);
    abs_style.height = Dimension::Percent(40.0);

    let mut root = LayoutNode::with_children(root_style, vec![LayoutNode::new(abs_style)]);
    root.compute_layout(1000.0, 800.0);

    let child_geom = &root.children[0].geometry.border_box;
    // left = 20% of 1000 = 200
    // top = 25% of 800 = 200
    // width = 50% of 1000 = 500
    // height = 40% of 800 = 320
    assert_eq!(child_geom.x, 200.0);
    assert_eq!(child_geom.y, 200.0);
    assert_eq!(child_geom.width, 500.0);
    assert_eq!(child_geom.height, 320.0);
}

#[test]
fn test_sticky_element_clamped_to_static_parent_container() {
    // Grandparent: Relative with height 3000px
    let mut grandparent = LayoutNode::new(ComputedStyle {
        display: Display::Block,
        position: Position::Relative,
        width: Dimension::Px(600.0),
        height: Dimension::Px(3000.0),
        ..Default::default()
    });

    // Parent: Static block container with height 400px
    let mut parent = LayoutNode::new(ComputedStyle {
        display: Display::Block,
        position: Position::Static,
        width: Dimension::Px(600.0),
        height: Dimension::Px(400.0),
        ..Default::default()
    });

    // Sticky child with height 50px, top 0px
    let mut sticky_child = LayoutNode::new(ComputedStyle {
        display: Display::Block,
        position: Position::Sticky,
        width: Dimension::Px(600.0),
        height: Dimension::Px(50.0),
        ..Default::default()
    });
    sticky_child.style.insets.top = Dimension::Px(0.0);

    parent.add_child(sticky_child);
    grandparent.add_child(parent);

    // Initial layout
    grandparent.compute_layout(600.0, 3000.0);

    // Scroll by 600px
    let cb_scrolled = ContainingBlock::with_scroll(
        grandparent.geometry.padding_box,
        Rect::new(0.0, 0.0, 600.0, 800.0),
        0.0,
        600.0,
    );
    strata_layout::solve_positioning_children(&mut grandparent, cb_scrolled);

    // Child must be clamped to parent bottom (400) - child height (50) = 350.0,
    // NOT allowed to escape parent into the 3000px grandparent!
    let sticky_geom = &grandparent.children[0].children[0].geometry.border_box;
    assert_eq!(
        sticky_geom.y, 350.0,
        "Sticky child must be clamped to static parent container bottom"
    );
}

#[test]
fn test_absolute_auto_width_and_height_with_right_inset_in_block_and_flex_parents() {
    use strata_style::types::FlexDirection;

    for is_flex in [false, true] {
        let mut parent_style = ComputedStyle {
            display: if is_flex {
                Display::Flex
            } else {
                Display::Block
            },
            flex_direction: FlexDirection::Row,
            position: Position::Relative,
            width: Dimension::Px(360.0),
            height: Dimension::Px(80.0),
            ..Default::default()
        };
        if is_flex {
            parent_style.justify_content = strata_style::types::JustifyContent::SpaceBetween;
        }

        // Absolute dropdown wrapper: width: auto, height: auto, right: 8px, top: 36px
        let mut dropdown_wrap_style = ComputedStyle {
            display: Display::Block,
            position: Position::Absolute,
            width: Dimension::Auto,
            height: Dimension::Auto,
            ..Default::default()
        };
        dropdown_wrap_style.insets.right = Dimension::Px(8.0);
        dropdown_wrap_style.insets.top = Dimension::Px(36.0);

        // Fixed-width w-48 (192px) menu panel inside the auto-sized absolute wrapper
        let menu_panel = LayoutNode::new(ComputedStyle {
            display: Display::Block,
            width: Dimension::Px(192.0),
            height: Dimension::Px(112.0),
            ..Default::default()
        });

        let dropdown_wrap = LayoutNode::with_children(dropdown_wrap_style, vec![menu_panel]);
        let mut parent = LayoutNode::with_children(parent_style, vec![dropdown_wrap]);
        parent.compute_layout(360.0, 80.0);

        let wrap_box = parent.children[0].geometry.border_box;
        let panel_box = parent.children[0].children[0].geometry.border_box;

        // Expected x = 360 - 8 - 192 = 160.0 (NOT 360 - 8 - 0 = 352.0)
        assert_eq!(
            wrap_box.width, 192.0,
            "is_flex={is_flex}: auto width must shrink-to-fit w-48 child"
        );
        assert_eq!(
            wrap_box.height, 112.0,
            "is_flex={is_flex}: auto height must wrap laid-out child"
        );
        assert_eq!(
            wrap_box.x, 160.0,
            "is_flex={is_flex}: right inset must subtract intrinsic width"
        );
        assert_eq!(
            wrap_box.y, 36.0,
            "is_flex={is_flex}: top inset must anchor to 36px"
        );
        assert_eq!(
            panel_box.x, 160.0,
            "is_flex={is_flex}: child subtree must translate to item_x"
        );
        assert_eq!(
            panel_box.y, 36.0,
            "is_flex={is_flex}: child subtree must translate to item_y"
        );
        assert!(
            wrap_box.contains_point(220.0, 90.0),
            "border_box hit-test must cover dropdown"
        );
    }
}

#[test]
fn test_absolute_auto_height_with_bottom_and_right_insets() {
    let mut root_style = ComputedStyle {
        display: Display::Flex,
        flex_direction: strata_style::types::FlexDirection::Column,
        position: Position::Relative,
        width: Dimension::Px(400.0),
        height: Dimension::Px(300.0),
        ..Default::default()
    };
    root_style.align_items = strata_style::types::AlignItems::Center;

    let mut popover_style = ComputedStyle {
        display: Display::Flex,
        flex_direction: strata_style::types::FlexDirection::Column,
        position: Position::Absolute,
        width: Dimension::Auto,
        height: Dimension::Auto,
        ..Default::default()
    };
    popover_style.insets.right = Dimension::Px(12.0);
    popover_style.insets.bottom = Dimension::Px(16.0);

    let item_a = LayoutNode::new(ComputedStyle {
        display: Display::Block,
        width: Dimension::Px(180.0),
        height: Dimension::Px(36.0),
        ..Default::default()
    });
    let item_b = LayoutNode::new(ComputedStyle {
        display: Display::Block,
        width: Dimension::Px(180.0),
        height: Dimension::Px(44.0),
        ..Default::default()
    });

    let popover = LayoutNode::with_children(popover_style, vec![item_a, item_b]);
    let mut root = LayoutNode::with_children(root_style, vec![popover]);
    root.compute_layout(400.0, 300.0);

    let pop_box = root.children[0].geometry.border_box;
    // Total width = 180, total height = 36 + 44 = 80
    // x = 400 - 12 - 180 = 208
    // y = 300 - 16 - 80 = 204
    assert_eq!(pop_box.width, 180.0);
    assert_eq!(pop_box.height, 80.0);
    assert_eq!(pop_box.x, 208.0);
    assert_eq!(pop_box.y, 204.0);
    assert_eq!(root.children[0].children[0].geometry.border_box.y, 204.0);
    assert_eq!(root.children[0].children[1].geometry.border_box.y, 240.0);
}

#[test]
fn test_nested_relative_insets_do_not_compound_across_positioned_ancestors() {
    // 3 nested Position::Relative flex containers wrapping a Position::Relative inner slice
    // with `top: -50px` — previously compounded -50px 4x to -200px!
    let row0 = LayoutNode::new(ComputedStyle {
        display: Display::Block,
        width: Dimension::Px(400.0),
        height: Dimension::Px(22.0),
        ..Default::default()
    });
    let row1 = LayoutNode::new(ComputedStyle {
        display: Display::Block,
        width: Dimension::Px(400.0),
        height: Dimension::Px(22.0),
        ..Default::default()
    });

    let mut slice_style = ComputedStyle {
        display: Display::Flex,
        flex_direction: strata_style::types::FlexDirection::Column,
        position: Position::Relative,
        width: Dimension::Px(400.0),
        height: Dimension::Auto,
        ..Default::default()
    };
    slice_style.insets.top = Dimension::Px(-50.0);
    slice_style.insets.left = Dimension::Px(12.0);
    let slice = LayoutNode::with_children(slice_style, vec![row0, row1]);

    let viewport = LayoutNode::with_children(
        ComputedStyle {
            display: Display::Block,
            position: Position::Relative,
            width: Dimension::Px(400.0),
            height: Dimension::Px(240.0),
            ..Default::default()
        },
        vec![slice],
    );
    let body = LayoutNode::with_children(
        ComputedStyle {
            display: Display::Flex,
            flex_direction: strata_style::types::FlexDirection::Row,
            position: Position::Relative,
            width: Dimension::Px(400.0),
            height: Dimension::Px(240.0),
            ..Default::default()
        },
        vec![viewport],
    );
    let mut root = LayoutNode::with_children(
        ComputedStyle {
            display: Display::Flex,
            flex_direction: strata_style::types::FlexDirection::Column,
            position: Position::Relative,
            width: Dimension::Px(400.0),
            height: Dimension::Px(240.0),
            ..Default::default()
        },
        vec![body],
    );

    root.compute_layout(400.0, 240.0);
    let slice_box = root.children[0].children[0].children[0].geometry.border_box;
    let row0_box = root.children[0].children[0].children[0].children[0]
        .geometry
        .border_box;
    let row1_box = root.children[0].children[0].children[0].children[1]
        .geometry
        .border_box;

    assert_eq!(slice_box.x, 12.0, "relative left must apply exactly once");
    assert_eq!(slice_box.y, -50.0, "relative top must apply exactly once");
    assert_eq!(row0_box.y, -50.0);
    assert_eq!(row1_box.y, -28.0);

    // Calling compute_layout a second time (cache hit path) must remain identically idempotent!
    root.compute_layout(400.0, 240.0);
    let slice_box_2 = root.children[0].children[0].children[0].geometry.border_box;
    assert_eq!(slice_box_2.x, 12.0);
    assert_eq!(slice_box_2.y, -50.0);
}