Compute_grid.PlacementSourceCSS Grid item placement algorithm.
Implements the 8.5 Grid Item Placement Algorithm from the CSS Grid specification, which determines the final position of each grid item within the implicit grid.
Reference: CSS Grid Spec ยง8.5
The placement algorithm processes grid items in four phases:
The algorithm operates on a Cell_occupancy.t matrix that tracks which cells are occupied and dynamically expands the implicit grid as needed. It respects grid-auto-flow settings (row/column, dense/sparse) and resolves named grid lines via Named.t.
Uses origin-zero coordinates where explicit grid tracks are numbered from 0. Negative implicit tracks have negative indices; positive implicit tracks extend beyond the explicit grid. See Grid_track_counts for coordinate system details.
Called by the grid layout algorithm after computing grid size estimates. Populates items list and cell_occupancy_matrix with placed items. The cell occupancy matrix tracks both placement state (definite vs. auto) and implicit grid growth.
val place_grid_items :
(module Tree.LAYOUT_PARTIAL_TREE with type t = 'tree) ->
cell_occupancy_matrix:Cell_occupancy.t ->
items:Grid_item.t list Stdlib.ref ->
tree:'tree ->
parent_node:Tree.Node_id.t ->
grid_auto_flow:Style.grid_auto_flow ->
align_items:Style.align_items ->
justify_items:Style.align_items ->
named_line_resolver:Named.t ->
unitplace_grid_items (module Tree) ~cell_occupancy_matrix ~items ~tree ~parent_node ~grid_auto_flow ~align_items ~justify_items ~named_line_resolver places all grid items into the grid.
Processes children of parent_node in four phases: definite placement, definite secondary axis, implicit grid expansion, and auto-placement. Mutates cell_occupancy_matrix to track occupancy and expand implicit grid. Accumulates placed items into items in reverse order then reverses at end.
The algorithm:
grid_auto_flow strategyDense auto-flow always searches from the grid start. Sparse auto-flow continues from the last placed item's position. Items are filtered to exclude display: none and position: absolute children.
Preconditions:
cell_occupancy_matrix pre-sized via grid size estimateitems is empty or will be clearednamed_line_resolver initialized with template and explicit counts