Compute_gridSourceCSS Grid layout algorithm implementation.
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.
Reference: CSS Grid Layout Module Level 1
The grid layout algorithm executes in these phases:
Column sizing re-runs when:
Row sizing re-runs (only after column re-run) when:
Only one re-run per axis is performed; further changes are ignored.
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.
This module coordinates submodules:
Explicit_grid: Track template parsing and auto-repeat resolutionImplicit_grid: Track count estimation from item placementsPlacement: Grid item placement and auto-placementTrack_sizing: Track sizing algorithm implementationAlignment: Track and item alignmentGrid_item: Item measurement and cachingGrid_track: Track representation and utilitiesval compute_grid_layout :
(module Tree.LAYOUT_PARTIAL_TREE with type t = 't) ->
tree:'t ->
node:Tree.Node_id.t ->
inputs:Tree.Layout_input.t ->
Tree.Layout_output.tcompute_grid_layout (module Tree) ~tree ~node ~inputs computes CSS Grid layout for node.
Returns early with container size if both dimensions are known and run_mode is Compute_size.
Algorithm:
Explicit_grid.compute_explicit_grid_size_in_axis, resolving auto-repeat counts.Implicit_grid.compute_grid_size_estimate.Placement.place_grid_items, expanding implicit grid as needed.Explicit_grid.initialize_grid_tracks, collapsing auto-fit tracks without items.Track_sizing.track_sizing_algorithm for inline axis, then block axis.Alignment.align_tracks per justify-content and align-content.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:
Tree.set_unrounded_layout.Tree.Layout_output includes container size, content size contribution from children, and first baseline from the first row.Performance optimization for grid size estimation.
Explicit grid track initialization from CSS styles.
Track count management and coordinate system conversions for CSS Grid layout.
Cell occupancy tracking for CSS Grid auto-placement.