Implements CSS Grid Level 1 specification for two-dimensional grid layout. This is an internal module implementing the multi-phase grid layout algorithm: explicit grid resolution, auto-placement, track sizing, and alignment.
Track alignment: Align tracks within the container using justify-content and align-content.
Item positioning: Align and position grid items within their grid areas, computing baselines for the container.
Key Invariants
Track sizing runs twice: once for the inline axis (columns), then for the block axis (rows). The inline pass determines container width, which may affect row sizing.
Track sizing may re-run if: (a) percentage tracks exist and container size was initially indefinite, or (b) intrinsic track content contributions change after the first pass.
Items crossing intrinsic or flexible tracks cache their content contributions to avoid redundant measurements during re-runs.
Auto-fit tracks collapse (size to zero) if they contain no items. Auto-fill tracks never collapse.
Grid items are sorted back into original source order before final positioning to match them with their style nodes.
Track Sizing Re-runs
Column sizing re-runs when:
Container width was indefinite and any column has percentage sizing.
Any item crossing an intrinsically-sized column has a changed min-content contribution.
Row sizing re-runs (only after column re-run) when:
Container height was indefinite and any row has percentage sizing.
Any item crossing an intrinsically-sized row has a changed min-content contribution.
Only one re-run per axis is performed; further changes are ignored.
Baseline Computation
Grid container baselines are computed from the first row containing items. If any item in that row is baseline-aligned, the first such item's baseline is used; otherwise, the first item's baseline (or bottom edge if none) is used.
Integration
This module coordinates submodules:
Explicit_grid: Track template parsing and auto-repeat resolution
Implicit_grid: Track count estimation from item placements
Resolve track indexes and determine which items cross flexible/intrinsic tracks.
Run Track_sizing.track_sizing_algorithm for inline axis, then block axis.
Resolve percentage track base sizes if container was indefinite.
Re-run track sizing if percentage tracks exist or content contributions changed.
Align tracks with Alignment.align_tracks per justify-content and align-content.
Align and position in-flow items, then absolutely positioned items, computing baselines.
Return layout output with container size, content size, and first baseline.
The container border-box size is constrained by min/max size styles, clamped to at least padding + border, and defaults to content size when style size is indefinite.
For indefinite containers, percentage track sizes initially resolve to zero, then re-resolve after content size is known. This may trigger column and row re-runs.
Absolutely positioned items resolve grid-row/column-start/end to track indexes, using container edges as fallback for indefinite placements.
Preconditions:
node has Display.Grid style.
tree provides access to child nodes and their styles.
Postconditions:
All children (in-flow, absolutely positioned, hidden) have layouts computed and stored via Tree.set_unrounded_layout.
Returned Tree.Layout_output includes container size, content size contribution from children, and first baseline from the first row.