⎇ 7b6efafe42c49ebdf1d84a7afe8ff4e62fda94ed
src/compute/grid/placement.rs
History
Who touched what
raw
Convert algorithms to traitified styles
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 primary_span = primary_placement_style.indefinite_span();
231 let secondary_span = secondary_placement_style.indefinite_span();
232 let has_definite_primary_axis_position = primary_placement_style.is_definite();
233 let primary_axis_grid_start_line = cell_occupancy_matrix.track_counts(primary_axis).implicit_start_line();
234 let primary_axis_grid_end_line = cell_occupancy_matrix.track_counts(primary_axis).implicit_end_line();
235 let secondary_axis_grid_start_line =
236 cell_occupancy_matrix.track_counts(primary_axis.other_axis()).implicit_start_line();
237
238 let line_area_is_occupied = |primary_span, secondary_span| {
239 !cell_occupancy_matrix.line_area_is_unoccupied(primary_axis, primary_span, secondary_span)
240 };
241
242 let (mut primary_idx, mut secondary_idx) = grid_position;
243
244 if has_definite_primary_axis_position {
245 let definite_primary_placement = primary_placement_style.resolve_definite_grid_lines();
246 let defined_primary_idx = definite_primary_placement.start;
247
248 // Compute starting position for search
249 if defined_primary_idx < primary_idx && secondary_idx != secondary_axis_grid_start_line {
250 secondary_idx = secondary_axis_grid_start_line;
251 primary_idx = defined_primary_idx + 1;
252 } else {
253 primary_idx = defined_primary_idx;
254 }
255
256 // Item has fixed primary axis position: so we simply increment the secondary axis position
257 // until we find a space that the item fits in
258 loop {
259 let primary_span = Line { start: primary_idx, end: primary_idx + primary_span };
260 let secondary_span = Line { start: secondary_idx, end: secondary_idx + secondary_span };
261
262 // If area is occupied, increment the index and try again
263 if line_area_is_occupied(primary_span, secondary_span) {
264 secondary_idx += 1;
265 continue;
266 }
267
268 // Once we find a free space, return that position
269 return (primary_span, secondary_span);
270 }
271 } else {
272 // Item does not have any fixed axis, so we search along the primary axis until we hit the end of the already
273 // existent tracks, and then we reset the primary axis back to zero and increment the secondary axis index.
274 // We continue in this vein until we find a space that the item fits in.
275 loop {
276 let primary_span = Line { start: primary_idx, end: primary_idx + primary_span };
277 let secondary_span = Line { start: secondary_idx, end: secondary_idx + secondary_span };
278
279 // If the primary index is out of bounds, then increment the secondary index and reset the primary
280 // index back to the start of the grid
281 let primary_out_of_bounds = primary_span.end > primary_axis_grid_end_line;
282 if primary_out_of_bounds {
283 secondary_idx += 1;
284 primary_idx = primary_axis_grid_start_line;
285 continue;
286 }
287
288 // If area is occupied, increment the primary index and try again
289 if line_area_is_occupied(primary_span, secondary_span) {
290 primary_idx += 1;
291 continue;
292 }
293
294 // Once we find a free space that's in bounds, return that position
295 return (primary_span, secondary_span);
296 }
297 }
298}
299
300/// Record the grid item in both CellOccupancyMatric and the GridItems list
301/// once a definite placement has been determined
302#[allow(clippy::too_many_arguments)]
303fn record_grid_placement<S: GridItemStyle>(
304 cell_occupancy_matrix: &mut CellOccupancyMatrix,
305 items: &mut Vec<GridItem>,
306 node: NodeId,
307 index: usize,
308 style: S,
309 parent_align_items: AlignItems,
310 parent_justify_items: AlignItems,
311 primary_axis: AbsoluteAxis,
312 primary_span: Line<OriginZeroLine>,
313 secondary_span: Line<OriginZeroLine>,
314 placement_type: CellOccupancyState,
315) {
316 #[cfg(test)]
317 println!("BEFORE placement:");
318 #[cfg(test)]
319 println!("{cell_occupancy_matrix:?}");
320
321 // Mark area of grid as occupied
322 cell_occupancy_matrix.mark_area_as(primary_axis, primary_span, secondary_span, placement_type);
323
324 // Create grid item
325 let (col_span, row_span) = match primary_axis {
326 AbsoluteAxis::Horizontal => (primary_span, secondary_span),
327 AbsoluteAxis::Vertical => (secondary_span, primary_span),
328 };
329 items.push(GridItem::new_with_placement_style_and_order(
330 node,
331 col_span,
332 row_span,
333 style,
334 parent_align_items,
335 parent_justify_items,
336 index as u16,
337 ));
338
339 #[cfg(test)]
340 println!("AFTER placement:");
341 #[cfg(test)]
342 println!("{cell_occupancy_matrix:?}");
343 #[cfg(test)]
344 println!("\n");
345}
346
347#[cfg(test)]
348mod tests {
349
350 mod test_placement_algorithm {
351 use crate::compute::grid::implicit_grid::compute_grid_size_estimate;
352 use crate::compute::grid::types::TrackCounts;
353 use crate::compute::grid::util::*;
354 use crate::compute::grid::CellOccupancyMatrix;
355 use crate::prelude::*;
356 use crate::style::GridAutoFlow;
357
358 use super::super::place_grid_items;
359
360 type ExpectedPlacement = (i16, i16, i16, i16);
361
362 fn placement_test_runner(
363 explicit_col_count: u16,
364 explicit_row_count: u16,
365 children: Vec<(usize, Style, ExpectedPlacement)>,
366 expected_col_counts: TrackCounts,
367 expected_row_counts: TrackCounts,
368 flow: GridAutoFlow,
369 ) {
370 // Setup test
371 let children_iter = || children.iter().map(|(index, style, _)| (*index, NodeId::from(*index), style));
372 let child_styles_iter = children.iter().map(|(_, style, _)| style);
373 let estimated_sizes = compute_grid_size_estimate(explicit_col_count, explicit_row_count, child_styles_iter);
374 let mut items = Vec::new();
375 let mut cell_occupancy_matrix =
376 CellOccupancyMatrix::with_track_counts(estimated_sizes.0, estimated_sizes.1);
377
378 // Run placement algorithm
379 place_grid_items(
380 &mut cell_occupancy_matrix,
381 &mut items,
382 children_iter,
383 flow,
384 AlignSelf::Start,
385 AlignSelf::Start,
386 );
387
388 // Assert that each item has been placed in the right location
389 let mut sorted_children = children.clone();
390 sorted_children.sort_by_key(|child| child.0);
391 for (idx, ((id, _style, expected_placement), item)) in sorted_children.iter().zip(items.iter()).enumerate()
392 {
393 assert_eq!(item.node, NodeId::from(*id));
394 let actual_placement = (item.column.start, item.column.end, item.row.start, item.row.end);
395 assert_eq!(actual_placement, (*expected_placement).into_oz(), "Item {idx} (0-indexed)");
396 }
397
398 // Assert that the correct number of implicit rows have been generated
399 let actual_row_counts = *cell_occupancy_matrix.track_counts(crate::compute::grid::AbsoluteAxis::Vertical);
400 assert_eq!(actual_row_counts, expected_row_counts, "row track counts");
401 let actual_col_counts = *cell_occupancy_matrix.track_counts(crate::compute::grid::AbsoluteAxis::Horizontal);
402 assert_eq!(actual_col_counts, expected_col_counts, "column track counts");
403 }
404
405 #[test]
406 fn test_only_fixed_placement() {
407 let flow = GridAutoFlow::Row;
408 let explicit_col_count = 2;
409 let explicit_row_count = 2;
410 let children = {
411 vec![
412 // node, style (grid coords), expected_placement (oz coords)
413 (1, (line(1), auto(), line(1), auto()).into_grid_child(), (0, 1, 0, 1)),
414 (2, (line(-4), auto(), line(-3), auto()).into_grid_child(), (-1, 0, 0, 1)),
415 (3, (line(-3), auto(), line(-4), auto()).into_grid_child(), (0, 1, -1, 0)),
416 (4, (line(3), span(2), line(5), auto()).into_grid_child(), (2, 4, 4, 5)),
417 ]
418 };
419 let expected_cols = TrackCounts { negative_implicit: 1, explicit: 2, positive_implicit: 2 };
420 let expected_rows = TrackCounts { negative_implicit: 1, explicit: 2, positive_implicit: 3 };
421 placement_test_runner(explicit_col_count, explicit_row_count, children, expected_cols, expected_rows, flow);
422 }
423
424 #[test]
425 fn test_placement_spanning_origin() {
426 let flow = GridAutoFlow::Row;
427 let explicit_col_count = 2;
428 let explicit_row_count = 2;
429 let children = {
430 vec![
431 // node, style (grid coords), expected_placement (oz coords)
432 (1, (line(-1), line(-1), line(-1), line(-1)).into_grid_child(), (2, 3, 2, 3)),
433 (2, (line(-1), span(2), line(-1), span(2)).into_grid_child(), (2, 4, 2, 4)),
434 (3, (line(-4), line(-4), line(-4), line(-4)).into_grid_child(), (-1, 0, -1, 0)),
435 (4, (line(-4), span(2), line(-4), span(2)).into_grid_child(), (-1, 1, -1, 1)),
436 ]
437 };
438 let expected_cols = TrackCounts { negative_implicit: 1, explicit: 2, positive_implicit: 2 };
439 let expected_rows = TrackCounts { negative_implicit: 1, explicit: 2, positive_implicit: 2 };
440 placement_test_runner(explicit_col_count, explicit_row_count, children, expected_cols, expected_rows, flow);
441 }
442
443 #[test]
444 fn test_only_auto_placement_row_flow() {
445 let flow = GridAutoFlow::Row;
446 let explicit_col_count = 2;
447 let explicit_row_count = 2;
448 let children = {
449 let auto_child = (auto(), auto(), auto(), auto()).into_grid_child();
450 vec![
451 // output order, node, style (grid coords), expected_placement (oz coords)
452 (1, auto_child.clone(), (0, 1, 0, 1)),
453 (2, auto_child.clone(), (1, 2, 0, 1)),
454 (3, auto_child.clone(), (0, 1, 1, 2)),
455 (4, auto_child.clone(), (1, 2, 1, 2)),
456 (5, auto_child.clone(), (0, 1, 2, 3)),
457 (6, auto_child.clone(), (1, 2, 2, 3)),
458 (7, auto_child.clone(), (0, 1, 3, 4)),
459 (8, auto_child.clone(), (1, 2, 3, 4)),
460 ]
461 };
462 let expected_cols = TrackCounts { negative_implicit: 0, explicit: 2, positive_implicit: 0 };
463 let expected_rows = TrackCounts { negative_implicit: 0, explicit: 2, positive_implicit: 2 };
464 placement_test_runner(explicit_col_count, explicit_row_count, children, expected_cols, expected_rows, flow);
465 }
466
467 #[test]
468 fn test_only_auto_placement_column_flow() {
469 let flow = GridAutoFlow::Column;
470 let explicit_col_count = 2;
471 let explicit_row_count = 2;
472 let children = {
473 let auto_child = (auto(), auto(), auto(), auto()).into_grid_child();
474 vec![
475 // output order, node, style (grid coords), expected_placement (oz coords)
476 (1, auto_child.clone(), (0, 1, 0, 1)),
477 (2, auto_child.clone(), (0, 1, 1, 2)),
478 (3, auto_child.clone(), (1, 2, 0, 1)),
479 (4, auto_child.clone(), (1, 2, 1, 2)),
480 (5, auto_child.clone(), (2, 3, 0, 1)),
481 (6, auto_child.clone(), (2, 3, 1, 2)),
482 (7, auto_child.clone(), (3, 4, 0, 1)),
483 (8, auto_child.clone(), (3, 4, 1, 2)),
484 ]
485 };
486 let expected_cols = TrackCounts { negative_implicit: 0, explicit: 2, positive_implicit: 2 };
487 let expected_rows = TrackCounts { negative_implicit: 0, explicit: 2, positive_implicit: 0 };
488 placement_test_runner(explicit_col_count, explicit_row_count, children, expected_cols, expected_rows, flow);
489 }
490
491 #[test]
492 fn test_oversized_item() {
493 let flow = GridAutoFlow::Row;
494 let explicit_col_count = 2;
495 let explicit_row_count = 2;
496 let children = {
497 vec![
498 // output order, node, style (grid coords), expected_placement (oz coords)
499 (1, (span(5), auto(), auto(), auto()).into_grid_child(), (0, 5, 0, 1)),
500 ]
501 };
502 let expected_cols = TrackCounts { negative_implicit: 0, explicit: 2, positive_implicit: 3 };
503 let expected_rows = TrackCounts { negative_implicit: 0, explicit: 2, positive_implicit: 0 };
504 placement_test_runner(explicit_col_count, explicit_row_count, children, expected_cols, expected_rows, flow);
505 }
506
507 #[test]
508 fn test_fixed_in_secondary_axis() {
509 let flow = GridAutoFlow::Row;
510 let explicit_col_count = 2;
511 let explicit_row_count = 2;
512 let children = {
513 vec![
514 // output order, node, style (grid coords), expected_placement (oz coords)
515 (1, (span(2), auto(), line(1), auto()).into_grid_child(), (0, 2, 0, 1)),
516 (2, (auto(), auto(), line(2), auto()).into_grid_child(), (0, 1, 1, 2)),
517 (3, (auto(), auto(), line(1), auto()).into_grid_child(), (2, 3, 0, 1)),
518 (4, (auto(), auto(), line(4), auto()).into_grid_child(), (0, 1, 3, 4)),
519 ]
520 };
521 let expected_cols = TrackCounts { negative_implicit: 0, explicit: 2, positive_implicit: 1 };
522 let expected_rows = TrackCounts { negative_implicit: 0, explicit: 2, positive_implicit: 2 };
523 placement_test_runner(explicit_col_count, explicit_row_count, children, expected_cols, expected_rows, flow);
524 }
525
526 #[test]
527 fn test_definite_in_secondary_axis_with_fully_definite_negative() {
528 let flow = GridAutoFlow::Row;
529 let explicit_col_count = 2;
530 let explicit_row_count = 2;
531 let children = {
532 vec![
533 // output order, node, style (grid coords), expected_placement (oz coords)
534 (2, (auto(), auto(), line(2), auto()).into_grid_child(), (0, 1, 1, 2)),
535 (1, (line(-4), auto(), line(2), auto()).into_grid_child(), (-1, 0, 1, 2)),
536 (3, (auto(), auto(), line(1), auto()).into_grid_child(), (-1, 0, 0, 1)),
537 ]
538 };
539 let expected_cols = TrackCounts { negative_implicit: 1, explicit: 2, positive_implicit: 0 };
540 let expected_rows = TrackCounts { negative_implicit: 0, explicit: 2, positive_implicit: 0 };
541 placement_test_runner(explicit_col_count, explicit_row_count, children, expected_cols, expected_rows, flow);
542 }
543
544 #[test]
545 fn test_dense_packing_algorithm() {
546 let flow = GridAutoFlow::RowDense;
547 let explicit_col_count = 4;
548 let explicit_row_count = 4;
549 let children = {
550 vec![
551 // output order, node, style (grid coords), expected_placement (oz coords)
552 (1, (line(2), auto(), line(1), auto()).into_grid_child(), (1, 2, 0, 1)), // Definitely positioned in column 2
553 (2, (span(2), auto(), auto(), auto()).into_grid_child(), (2, 4, 0, 1)), // Spans 2 columns, so positioned after item 1
554 (3, (auto(), auto(), auto(), auto()).into_grid_child(), (0, 1, 0, 1)), // Spans 1 column, so should be positioned before item 1
555 ]
556 };
557 let expected_cols = TrackCounts { negative_implicit: 0, explicit: 4, positive_implicit: 0 };
558 let expected_rows = TrackCounts { negative_implicit: 0, explicit: 4, positive_implicit: 0 };
559 placement_test_runner(explicit_col_count, explicit_row_count, children, expected_cols, expected_rows, flow);
560 }
561
562 #[test]
563 fn test_sparse_packing_algorithm() {
564 let flow = GridAutoFlow::Row;
565 let explicit_col_count = 4;
566 let explicit_row_count = 4;
567 let children = {
568 vec![
569 // output order, node, style (grid coords), expected_placement (oz coords)
570 (1, (auto(), span(3), auto(), auto()).into_grid_child(), (0, 3, 0, 1)), // Width 3
571 (2, (auto(), span(3), auto(), auto()).into_grid_child(), (0, 3, 1, 2)), // Width 3 (wraps to next row)
572 (3, (auto(), span(1), auto(), auto()).into_grid_child(), (3, 4, 1, 2)), // Width 1 (uses second row as we're already on it)
573 ]
574 };
575 let expected_cols = TrackCounts { negative_implicit: 0, explicit: 4, positive_implicit: 0 };
576 let expected_rows = TrackCounts { negative_implicit: 0, explicit: 4, positive_implicit: 0 };
577 placement_test_runner(explicit_col_count, explicit_row_count, children, expected_cols, expected_rows, flow);
578 }
579
580 #[test]
581 fn test_auto_placement_in_negative_tracks() {
582 let flow = GridAutoFlow::RowDense;
583 let explicit_col_count = 2;
584 let explicit_row_count = 2;
585 let children = {
586 vec![
587 // output order, node, style (grid coords), expected_placement (oz coords)
588 (1, (line(-5), auto(), line(1), auto()).into_grid_child(), (-2, -1, 0, 1)), // Row 1. Definitely positioned in column -2
589 (2, (auto(), auto(), line(2), auto()).into_grid_child(), (-2, -1, 1, 2)), // Row 2. Auto positioned in column -2
590 (3, (auto(), auto(), auto(), auto()).into_grid_child(), (-1, 0, 0, 1)), // Row 1. Auto positioned in column -1
591 ]
592 };
593 let expected_cols = TrackCounts { negative_implicit: 2, explicit: 2, positive_implicit: 0 };
594 let expected_rows = TrackCounts { negative_implicit: 0, explicit: 2, positive_implicit: 0 };
595 placement_test_runner(explicit_col_count, explicit_row_count, children, expected_cols, expected_rows, flow);
596 }
597 }
598}