tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 583HTML 275Markdown 8TOML 41586 Sutichaykuna

⎇ 5dc582cca6cc65591c264159c215d647d03f9b69 src/compute/grid/placement.rs Rust · 602 siq'ikuna · 27.5 KB Ñawpa willay Pi imata llamkarqan ch'uya

Fix grid placement for items with fixed primary axis (#818) 5dc582cca · Nico Burns ·

1//! Implements placing items in the grid and resolving the implicit grid.
2//! <https://www.w3.org/TR/css-grid-1/#placement>
3use super::types::{CellOccupancyMatrix, CellOccupancyState, GridItem};
4use super::OriginZeroLine;
5use crate::geometry::Line;
6use crate::geometry::{AbsoluteAxis, InBothAbsAxis};
7use crate::style::{AlignItems, GridAutoFlow, OriginZeroGridPlacement};
8use crate::tree::NodeId;
9use crate::util::sys::Vec;
10use crate::GridItemStyle;
11
12/// 8.5. Grid Item Placement Algorithm
13/// Place items into the grid, generating new rows/column into the implicit grid as required
14///
15/// [Specification](https://www.w3.org/TR/css-grid-2/#auto-placement-algo)
16pub(super) fn place_grid_items<'a, S, ChildIter>(
17    cell_occupancy_matrix: &mut CellOccupancyMatrix,
18    items: &mut Vec<GridItem>,
19    children_iter: impl Fn() -> ChildIter,
20    grid_auto_flow: GridAutoFlow,
21    align_items: AlignItems,
22    justify_items: AlignItems,
23) where
24    S: GridItemStyle + 'a,
25    ChildIter: Iterator<Item = (usize, NodeId, S)>,
26{
27    let primary_axis = grid_auto_flow.primary_axis();
28    let secondary_axis = primary_axis.other_axis();
29
30    let map_child_style_to_origin_zero_placement = {
31        let explicit_col_count = cell_occupancy_matrix.track_counts(AbsoluteAxis::Horizontal).explicit;
32        let explicit_row_count = cell_occupancy_matrix.track_counts(AbsoluteAxis::Vertical).explicit;
33        move |(index, node, style): (usize, NodeId, S)| -> (_, _, _, S) {
34            let origin_zero_placement = InBothAbsAxis {
35                horizontal: style
36                    .grid_column()
37                    .map(|placement| placement.into_origin_zero_placement(explicit_col_count)),
38                vertical: style.grid_row().map(|placement| placement.into_origin_zero_placement(explicit_row_count)),
39            };
40            (index, node, origin_zero_placement, style)
41        }
42    };
43
44    // 1. Place children with definite positions
45    let mut idx = 0;
46    children_iter()
47        .filter(|(_, _, child_style)| child_style.grid_row().is_definite() && child_style.grid_column().is_definite())
48        .map(map_child_style_to_origin_zero_placement)
49        .for_each(|(index, child_node, child_placement, style)| {
50            idx += 1;
51            #[cfg(test)]
52            println!("Definite Item {idx}\n==============");
53
54            let (row_span, col_span) = place_definite_grid_item(child_placement, primary_axis);
55            record_grid_placement(
56                cell_occupancy_matrix,
57                items,
58                child_node,
59                index,
60                style,
61                align_items,
62                justify_items,
63                primary_axis,
64                row_span,
65                col_span,
66                CellOccupancyState::DefinitelyPlaced,
67            );
68        });
69
70    // 2. Place remaining children with definite secondary axis positions
71    let mut idx = 0;
72    children_iter()
73        .filter(|(_, _, child_style)| {
74            child_style.grid_placement(secondary_axis).is_definite()
75                && !child_style.grid_placement(primary_axis).is_definite()
76        })
77        .map(map_child_style_to_origin_zero_placement)
78        .for_each(|(index, child_node, child_placement, style)| {
79            idx += 1;
80            #[cfg(test)]
81            println!("Definite Secondary Item {idx}\n==============");
82
83            let (primary_span, secondary_span) =
84                place_definite_secondary_axis_item(&*cell_occupancy_matrix, child_placement, grid_auto_flow);
85
86            record_grid_placement(
87                cell_occupancy_matrix,
88                items,
89                child_node,
90                index,
91                style,
92                align_items,
93                justify_items,
94                primary_axis,
95                primary_span,
96                secondary_span,
97                CellOccupancyState::AutoPlaced,
98            );
99        });
100
101    // 3. Determine the number of columns in the implicit grid
102    // By the time we get to this point in the execution, this is actually already accounted for:
103    //
104    // 3.1 Start with the columns from the explicit grid
105    //        => Handled by grid size estimate which is used to pre-size the GridOccupancyMatrix
106    //
107    // 3.2 Among all the items with a definite column position (explicitly positioned items, items positioned in the previous step,
108    //     and items not yet positioned but with a definite column) add columns to the beginning and end of the implicit grid as necessary
109    //     to accommodate those items.
110    //        => Handled by expand_to_fit_range which expands the GridOccupancyMatrix as necessary
111    //            -> Called by mark_area_as
112    //            -> Called by record_grid_placement
113    //
114    // 3.3 If the largest column span among all the items without a definite column position is larger than the width of
115    //     the implicit grid, add columns to the end of the implicit grid to accommodate that column span.
116    //        => Handled by grid size estimate which is used to pre-size the GridOccupancyMatrix
117
118    // 4. Position the remaining grid items
119    // (which either have definite position only in the secondary axis or indefinite positions in both axis)
120    let primary_axis = grid_auto_flow.primary_axis();
121    let secondary_axis = primary_axis.other_axis();
122    let primary_neg_tracks = cell_occupancy_matrix.track_counts(primary_axis).negative_implicit as i16;
123    let secondary_neg_tracks = cell_occupancy_matrix.track_counts(secondary_axis).negative_implicit as i16;
124    let grid_start_position = (OriginZeroLine(-primary_neg_tracks), OriginZeroLine(-secondary_neg_tracks));
125    let mut grid_position = grid_start_position;
126    let mut idx = 0;
127    children_iter()
128        .filter(|(_, _, child_style)| !child_style.grid_placement(secondary_axis).is_definite())
129        .map(map_child_style_to_origin_zero_placement)
130        .for_each(|(index, child_node, child_placement, style)| {
131            idx += 1;
132            #[cfg(test)]
133            println!("\nAuto Item {idx}\n==============");
134
135            // Compute placement
136            let (primary_span, secondary_span) = place_indefinitely_positioned_item(
137                &*cell_occupancy_matrix,
138                child_placement,
139                grid_auto_flow,
140                grid_position,
141            );
142
143            // Record item
144            record_grid_placement(
145                cell_occupancy_matrix,
146                items,
147                child_node,
148                index,
149                style,
150                align_items,
151                justify_items,
152                primary_axis,
153                primary_span,
154                secondary_span,
155                CellOccupancyState::AutoPlaced,
156            );
157
158            // If using the "dense" placement algorithm then reset the grid position back to grid_start_position ready for the next item
159            // Otherwise set it to the position of the current item so that the next item it placed after it.
160            grid_position = match grid_auto_flow.is_dense() {
161                true => grid_start_position,
162                false => (primary_span.end, secondary_span.start),
163            }
164        });
165}
166
167/// 8.5. Grid Item Placement Algorithm
168/// Place a single definitely placed item into the grid
169fn place_definite_grid_item(
170    placement: InBothAbsAxis<Line<OriginZeroGridPlacement>>,
171    primary_axis: AbsoluteAxis,
172) -> (Line<OriginZeroLine>, Line<OriginZeroLine>) {
173    // Resolve spans to tracks
174    let primary_span = placement.get(primary_axis).resolve_definite_grid_lines();
175    let secondary_span = placement.get(primary_axis.other_axis()).resolve_definite_grid_lines();
176
177    (primary_span, secondary_span)
178}
179
180/// 8.5. Grid Item Placement Algorithm
181/// Step 2. Place remaining children with definite secondary axis positions
182fn place_definite_secondary_axis_item(
183    cell_occupancy_matrix: &CellOccupancyMatrix,
184    placement: InBothAbsAxis<Line<OriginZeroGridPlacement>>,
185    auto_flow: GridAutoFlow,
186) -> (Line<OriginZeroLine>, Line<OriginZeroLine>) {
187    let primary_axis = auto_flow.primary_axis();
188    let secondary_axis = primary_axis.other_axis();
189
190    let secondary_axis_placement = placement.get(secondary_axis).resolve_definite_grid_lines();
191    let primary_axis_grid_start_line = cell_occupancy_matrix.track_counts(primary_axis).implicit_start_line();
192    let starting_position = match auto_flow.is_dense() {
193        true => primary_axis_grid_start_line,
194        false => cell_occupancy_matrix
195            .last_of_type(primary_axis, secondary_axis_placement.start, CellOccupancyState::AutoPlaced)
196            .unwrap_or(primary_axis_grid_start_line),
197    };
198
199    let mut position: OriginZeroLine = starting_position;
200    loop {
201        let primary_axis_placement = placement.get(primary_axis).resolve_indefinite_grid_tracks(position);
202
203        let does_fit = cell_occupancy_matrix.line_area_is_unoccupied(
204            primary_axis,
205            primary_axis_placement,
206            secondary_axis_placement,
207        );
208
209        if does_fit {
210            return (primary_axis_placement, secondary_axis_placement);
211        } else {
212            position += 1;
213        }
214    }
215}
216
217/// 8.5. Grid Item Placement Algorithm
218/// Step 4. Position the remaining grid items.
219fn place_indefinitely_positioned_item(
220    cell_occupancy_matrix: &CellOccupancyMatrix,
221    placement: InBothAbsAxis<Line<OriginZeroGridPlacement>>,
222    auto_flow: GridAutoFlow,
223    grid_position: (OriginZeroLine, OriginZeroLine),
224) -> (Line<OriginZeroLine>, Line<OriginZeroLine>) {
225    let primary_axis = auto_flow.primary_axis();
226
227    let primary_placement_style = placement.get(primary_axis);
228    let secondary_placement_style = placement.get(primary_axis.other_axis());
229
230    let secondary_span = secondary_placement_style.indefinite_span();
231    let has_definite_primary_axis_position = primary_placement_style.is_definite();
232    let primary_axis_grid_start_line = cell_occupancy_matrix.track_counts(primary_axis).implicit_start_line();
233    let primary_axis_grid_end_line = cell_occupancy_matrix.track_counts(primary_axis).implicit_end_line();
234    let secondary_axis_grid_start_line =
235        cell_occupancy_matrix.track_counts(primary_axis.other_axis()).implicit_start_line();
236
237    let line_area_is_occupied = |primary_span, secondary_span| {
238        !cell_occupancy_matrix.line_area_is_unoccupied(primary_axis, primary_span, secondary_span)
239    };
240
241    let (mut primary_idx, mut secondary_idx) = grid_position;
242
243    if has_definite_primary_axis_position {
244        let primary_span = primary_placement_style.resolve_definite_grid_lines();
245
246        // Compute secondary axis starting position for search
247        secondary_idx = match auto_flow.is_dense() {
248            // If auto-flow is dense then we always search from the first track
249            true => secondary_axis_grid_start_line,
250            false => {
251                if primary_span.start < primary_idx {
252                    secondary_idx + 1
253                } else {
254                    secondary_idx
255                }
256            }
257        };
258
259        // Item has fixed primary axis position: so we simply increment the secondary axis position
260        // until we find a space that the item fits in
261        loop {
262            let secondary_span = Line { start: secondary_idx, end: secondary_idx + secondary_span };
263
264            // If area is occupied, increment the index and try again
265            if line_area_is_occupied(primary_span, secondary_span) {
266                secondary_idx += 1;
267                continue;
268            }
269
270            // Once we find a free space, return that position
271            return (primary_span, secondary_span);
272        }
273    } else {
274        let primary_span = primary_placement_style.indefinite_span();
275
276        // Item does not have any fixed axis, so we search along the primary axis until we hit the end of the already
277        // existent tracks, and then we reset the primary axis back to zero and increment the secondary axis index.
278        // We continue in this vein until we find a space that the item fits in.
279        loop {
280            let primary_span = Line { start: primary_idx, end: primary_idx + primary_span };
281            let secondary_span = Line { start: secondary_idx, end: secondary_idx + secondary_span };
282
283            // If the primary index is out of bounds, then increment the secondary index and reset the primary
284            // index back to the start of the grid
285            let primary_out_of_bounds = primary_span.end > primary_axis_grid_end_line;
286            if primary_out_of_bounds {
287                secondary_idx += 1;
288                primary_idx = primary_axis_grid_start_line;
289                continue;
290            }
291
292            // If area is occupied, increment the primary index and try again
293            if line_area_is_occupied(primary_span, secondary_span) {
294                primary_idx += 1;
295                continue;
296            }
297
298            // Once we find a free space that's in bounds, return that position
299            return (primary_span, secondary_span);
300        }
301    }
302}
303
304/// Record the grid item in both CellOccupancyMatric and the GridItems list
305/// once a definite placement has been determined
306#[allow(clippy::too_many_arguments)]
307fn record_grid_placement<S: GridItemStyle>(
308    cell_occupancy_matrix: &mut CellOccupancyMatrix,
309    items: &mut Vec<GridItem>,
310    node: NodeId,
311    index: usize,
312    style: S,
313    parent_align_items: AlignItems,
314    parent_justify_items: AlignItems,
315    primary_axis: AbsoluteAxis,
316    primary_span: Line<OriginZeroLine>,
317    secondary_span: Line<OriginZeroLine>,
318    placement_type: CellOccupancyState,
319) {
320    #[cfg(test)]
321    println!("BEFORE placement:");
322    #[cfg(test)]
323    println!("{cell_occupancy_matrix:?}");
324
325    // Mark area of grid as occupied
326    cell_occupancy_matrix.mark_area_as(primary_axis, primary_span, secondary_span, placement_type);
327
328    // Create grid item
329    let (col_span, row_span) = match primary_axis {
330        AbsoluteAxis::Horizontal => (primary_span, secondary_span),
331        AbsoluteAxis::Vertical => (secondary_span, primary_span),
332    };
333    items.push(GridItem::new_with_placement_style_and_order(
334        node,
335        col_span,
336        row_span,
337        style,
338        parent_align_items,
339        parent_justify_items,
340        index as u16,
341    ));
342
343    #[cfg(test)]
344    println!("AFTER placement:");
345    #[cfg(test)]
346    println!("{cell_occupancy_matrix:?}");
347    #[cfg(test)]
348    println!("\n");
349}
350
351#[cfg(test)]
352mod tests {
353
354    mod test_placement_algorithm {
355        use crate::compute::grid::implicit_grid::compute_grid_size_estimate;
356        use crate::compute::grid::types::TrackCounts;
357        use crate::compute::grid::util::*;
358        use crate::compute::grid::CellOccupancyMatrix;
359        use crate::prelude::*;
360        use crate::style::GridAutoFlow;
361
362        use super::super::place_grid_items;
363
364        type ExpectedPlacement = (i16, i16, i16, i16);
365
366        fn placement_test_runner(
367            explicit_col_count: u16,
368            explicit_row_count: u16,
369            children: Vec<(usize, Style, ExpectedPlacement)>,
370            expected_col_counts: TrackCounts,
371            expected_row_counts: TrackCounts,
372            flow: GridAutoFlow,
373        ) {
374            // Setup test
375            let children_iter = || children.iter().map(|(index, style, _)| (*index, NodeId::from(*index), style));
376            let child_styles_iter = children.iter().map(|(_, style, _)| style);
377            let estimated_sizes = compute_grid_size_estimate(explicit_col_count, explicit_row_count, child_styles_iter);
378            let mut items = Vec::new();
379            let mut cell_occupancy_matrix =
380                CellOccupancyMatrix::with_track_counts(estimated_sizes.0, estimated_sizes.1);
381
382            // Run placement algorithm
383            place_grid_items(
384                &mut cell_occupancy_matrix,
385                &mut items,
386                children_iter,
387                flow,
388                AlignSelf::Start,
389                AlignSelf::Start,
390            );
391
392            // Assert that each item has been placed in the right location
393            let mut sorted_children = children.clone();
394            sorted_children.sort_by_key(|child| child.0);
395            for (idx, ((id, _style, expected_placement), item)) in sorted_children.iter().zip(items.iter()).enumerate()
396            {
397                assert_eq!(item.node, NodeId::from(*id));
398                let actual_placement = (item.column.start, item.column.end, item.row.start, item.row.end);
399                assert_eq!(actual_placement, (*expected_placement).into_oz(), "Item {idx} (0-indexed)");
400            }
401
402            // Assert that the correct number of implicit rows have been generated
403            let actual_row_counts = *cell_occupancy_matrix.track_counts(crate::compute::grid::AbsoluteAxis::Vertical);
404            assert_eq!(actual_row_counts, expected_row_counts, "row track counts");
405            let actual_col_counts = *cell_occupancy_matrix.track_counts(crate::compute::grid::AbsoluteAxis::Horizontal);
406            assert_eq!(actual_col_counts, expected_col_counts, "column track counts");
407        }
408
409        #[test]
410        fn test_only_fixed_placement() {
411            let flow = GridAutoFlow::Row;
412            let explicit_col_count = 2;
413            let explicit_row_count = 2;
414            let children = {
415                vec![
416                    // node, style (grid coords), expected_placement (oz coords)
417                    (1, (line(1), auto(), line(1), auto()).into_grid_child(), (0, 1, 0, 1)),
418                    (2, (line(-4), auto(), line(-3), auto()).into_grid_child(), (-1, 0, 0, 1)),
419                    (3, (line(-3), auto(), line(-4), auto()).into_grid_child(), (0, 1, -1, 0)),
420                    (4, (line(3), span(2), line(5), auto()).into_grid_child(), (2, 4, 4, 5)),
421                ]
422            };
423            let expected_cols = TrackCounts { negative_implicit: 1, explicit: 2, positive_implicit: 2 };
424            let expected_rows = TrackCounts { negative_implicit: 1, explicit: 2, positive_implicit: 3 };
425            placement_test_runner(explicit_col_count, explicit_row_count, children, expected_cols, expected_rows, flow);
426        }
427
428        #[test]
429        fn test_placement_spanning_origin() {
430            let flow = GridAutoFlow::Row;
431            let explicit_col_count = 2;
432            let explicit_row_count = 2;
433            let children = {
434                vec![
435                    // node, style (grid coords), expected_placement (oz coords)
436                    (1, (line(-1), line(-1), line(-1), line(-1)).into_grid_child(), (2, 3, 2, 3)),
437                    (2, (line(-1), span(2), line(-1), span(2)).into_grid_child(), (2, 4, 2, 4)),
438                    (3, (line(-4), line(-4), line(-4), line(-4)).into_grid_child(), (-1, 0, -1, 0)),
439                    (4, (line(-4), span(2), line(-4), span(2)).into_grid_child(), (-1, 1, -1, 1)),
440                ]
441            };
442            let expected_cols = TrackCounts { negative_implicit: 1, explicit: 2, positive_implicit: 2 };
443            let expected_rows = TrackCounts { negative_implicit: 1, explicit: 2, positive_implicit: 2 };
444            placement_test_runner(explicit_col_count, explicit_row_count, children, expected_cols, expected_rows, flow);
445        }
446
447        #[test]
448        fn test_only_auto_placement_row_flow() {
449            let flow = GridAutoFlow::Row;
450            let explicit_col_count = 2;
451            let explicit_row_count = 2;
452            let children = {
453                let auto_child = (auto(), auto(), auto(), auto()).into_grid_child();
454                vec![
455                    // output order, node, style (grid coords), expected_placement (oz coords)
456                    (1, auto_child.clone(), (0, 1, 0, 1)),
457                    (2, auto_child.clone(), (1, 2, 0, 1)),
458                    (3, auto_child.clone(), (0, 1, 1, 2)),
459                    (4, auto_child.clone(), (1, 2, 1, 2)),
460                    (5, auto_child.clone(), (0, 1, 2, 3)),
461                    (6, auto_child.clone(), (1, 2, 2, 3)),
462                    (7, auto_child.clone(), (0, 1, 3, 4)),
463                    (8, auto_child.clone(), (1, 2, 3, 4)),
464                ]
465            };
466            let expected_cols = TrackCounts { negative_implicit: 0, explicit: 2, positive_implicit: 0 };
467            let expected_rows = TrackCounts { negative_implicit: 0, explicit: 2, positive_implicit: 2 };
468            placement_test_runner(explicit_col_count, explicit_row_count, children, expected_cols, expected_rows, flow);
469        }
470
471        #[test]
472        fn test_only_auto_placement_column_flow() {
473            let flow = GridAutoFlow::Column;
474            let explicit_col_count = 2;
475            let explicit_row_count = 2;
476            let children = {
477                let auto_child = (auto(), auto(), auto(), auto()).into_grid_child();
478                vec![
479                    // output order, node, style (grid coords), expected_placement (oz coords)
480                    (1, auto_child.clone(), (0, 1, 0, 1)),
481                    (2, auto_child.clone(), (0, 1, 1, 2)),
482                    (3, auto_child.clone(), (1, 2, 0, 1)),
483                    (4, auto_child.clone(), (1, 2, 1, 2)),
484                    (5, auto_child.clone(), (2, 3, 0, 1)),
485                    (6, auto_child.clone(), (2, 3, 1, 2)),
486                    (7, auto_child.clone(), (3, 4, 0, 1)),
487                    (8, auto_child.clone(), (3, 4, 1, 2)),
488                ]
489            };
490            let expected_cols = TrackCounts { negative_implicit: 0, explicit: 2, positive_implicit: 2 };
491            let expected_rows = TrackCounts { negative_implicit: 0, explicit: 2, positive_implicit: 0 };
492            placement_test_runner(explicit_col_count, explicit_row_count, children, expected_cols, expected_rows, flow);
493        }
494
495        #[test]
496        fn test_oversized_item() {
497            let flow = GridAutoFlow::Row;
498            let explicit_col_count = 2;
499            let explicit_row_count = 2;
500            let children = {
501                vec![
502                    // output order, node, style (grid coords), expected_placement (oz coords)
503                    (1, (span(5), auto(), auto(), auto()).into_grid_child(), (0, 5, 0, 1)),
504                ]
505            };
506            let expected_cols = TrackCounts { negative_implicit: 0, explicit: 2, positive_implicit: 3 };
507            let expected_rows = TrackCounts { negative_implicit: 0, explicit: 2, positive_implicit: 0 };
508            placement_test_runner(explicit_col_count, explicit_row_count, children, expected_cols, expected_rows, flow);
509        }
510
511        #[test]
512        fn test_fixed_in_secondary_axis() {
513            let flow = GridAutoFlow::Row;
514            let explicit_col_count = 2;
515            let explicit_row_count = 2;
516            let children = {
517                vec![
518                    // output order, node, style (grid coords), expected_placement (oz coords)
519                    (1, (span(2), auto(), line(1), auto()).into_grid_child(), (0, 2, 0, 1)),
520                    (2, (auto(), auto(), line(2), auto()).into_grid_child(), (0, 1, 1, 2)),
521                    (3, (auto(), auto(), line(1), auto()).into_grid_child(), (2, 3, 0, 1)),
522                    (4, (auto(), auto(), line(4), auto()).into_grid_child(), (0, 1, 3, 4)),
523                ]
524            };
525            let expected_cols = TrackCounts { negative_implicit: 0, explicit: 2, positive_implicit: 1 };
526            let expected_rows = TrackCounts { negative_implicit: 0, explicit: 2, positive_implicit: 2 };
527            placement_test_runner(explicit_col_count, explicit_row_count, children, expected_cols, expected_rows, flow);
528        }
529
530        #[test]
531        fn test_definite_in_secondary_axis_with_fully_definite_negative() {
532            let flow = GridAutoFlow::Row;
533            let explicit_col_count = 2;
534            let explicit_row_count = 2;
535            let children = {
536                vec![
537                    // output order, node, style (grid coords), expected_placement (oz coords)
538                    (2, (auto(), auto(), line(2), auto()).into_grid_child(), (0, 1, 1, 2)),
539                    (1, (line(-4), auto(), line(2), auto()).into_grid_child(), (-1, 0, 1, 2)),
540                    (3, (auto(), auto(), line(1), auto()).into_grid_child(), (-1, 0, 0, 1)),
541                ]
542            };
543            let expected_cols = TrackCounts { negative_implicit: 1, explicit: 2, positive_implicit: 0 };
544            let expected_rows = TrackCounts { negative_implicit: 0, explicit: 2, positive_implicit: 0 };
545            placement_test_runner(explicit_col_count, explicit_row_count, children, expected_cols, expected_rows, flow);
546        }
547
548        #[test]
549        fn test_dense_packing_algorithm() {
550            let flow = GridAutoFlow::RowDense;
551            let explicit_col_count = 4;
552            let explicit_row_count = 4;
553            let children = {
554                vec![
555                    // output order, node, style (grid coords), expected_placement (oz coords)
556                    (1, (line(2), auto(), line(1), auto()).into_grid_child(), (1, 2, 0, 1)), // Definitely positioned in column 2
557                    (2, (span(2), auto(), auto(), auto()).into_grid_child(), (2, 4, 0, 1)), // Spans 2 columns, so positioned after item 1
558                    (3, (auto(), auto(), auto(), auto()).into_grid_child(), (0, 1, 0, 1)), // Spans 1 column, so should be positioned before item 1
559                ]
560            };
561            let expected_cols = TrackCounts { negative_implicit: 0, explicit: 4, positive_implicit: 0 };
562            let expected_rows = TrackCounts { negative_implicit: 0, explicit: 4, positive_implicit: 0 };
563            placement_test_runner(explicit_col_count, explicit_row_count, children, expected_cols, expected_rows, flow);
564        }
565
566        #[test]
567        fn test_sparse_packing_algorithm() {
568            let flow = GridAutoFlow::Row;
569            let explicit_col_count = 4;
570            let explicit_row_count = 4;
571            let children = {
572                vec![
573                    // output order, node, style (grid coords), expected_placement (oz coords)
574                    (1, (auto(), span(3), auto(), auto()).into_grid_child(), (0, 3, 0, 1)), // Width 3
575                    (2, (auto(), span(3), auto(), auto()).into_grid_child(), (0, 3, 1, 2)), // Width 3 (wraps to next row)
576                    (3, (auto(), span(1), auto(), auto()).into_grid_child(), (3, 4, 1, 2)), // Width 1 (uses second row as we're already on it)
577                ]
578            };
579            let expected_cols = TrackCounts { negative_implicit: 0, explicit: 4, positive_implicit: 0 };
580            let expected_rows = TrackCounts { negative_implicit: 0, explicit: 4, positive_implicit: 0 };
581            placement_test_runner(explicit_col_count, explicit_row_count, children, expected_cols, expected_rows, flow);
582        }
583
584        #[test]
585        fn test_auto_placement_in_negative_tracks() {
586            let flow = GridAutoFlow::RowDense;
587            let explicit_col_count = 2;
588            let explicit_row_count = 2;
589            let children = {
590                vec![
591                    // output order, node, style (grid coords), expected_placement (oz coords)
592                    (1, (line(-5), auto(), line(1), auto()).into_grid_child(), (-2, -1, 0, 1)), // Row 1. Definitely positioned in column -2
593                    (2, (auto(), auto(), line(2), auto()).into_grid_child(), (-2, -1, 1, 2)), // Row 2. Auto positioned in column -2
594                    (3, (auto(), auto(), auto(), auto()).into_grid_child(), (-1, 0, 0, 1)), // Row 1. Auto positioned in column -1
595                ]
596            };
597            let expected_cols = TrackCounts { negative_implicit: 2, explicit: 2, positive_implicit: 0 };
598            let expected_rows = TrackCounts { negative_implicit: 0, explicit: 2, positive_implicit: 0 };
599            placement_test_runner(explicit_col_count, explicit_row_count, children, expected_cols, expected_rows, flow);
600        }
601    }
602}