bez docs grid-layout.md
4.4 kB 57 lines
Markdown
at main

Grid layout #

The structure the engine has for CSS Grid Layout 1: how a grid container's children reach three generated functions and how their answers become fragments. The engine holds no grid rule of its own; placement, track sizing, alignment and distribution belong to generated functions (AGENTS.md -> "No hand-written web feature code").

Where it lives #

Piece Anchor Does
Dispatch crates/layout/src/fragment.rs formatting_of, FormattingContext::Grid a box the formatting-context function (crates/layout/src/formatting_context.rs) answers Grid for lays its children out with layout_grid_children; its starting candidate answers Block for every box, so no grid walk runs until a job admits one
Reading values fragment.rs grid_model, GridModel reads the container's and each in-flow child's computed grid values as written, and each child's min-content and max-content widths
Walk fragment.rs layout_grid_children calls the columns function, lays each item out at its column span's width to learn its height, calls the rows function, lays every item out against its area, calls the item function and places the answer
Intrinsic width fragment.rs grid_content_width, called from content_width asks the columns function for the width the columns need when the container has no definite width
Types crates/layout/src/grid.rs the inputs and outputs of the three functions, and the tokenizer for Stylo's serialized track lists (parse_track_list, expand)
Starting candidates crates/layout/src/generated/grid.rs (columns, rows, item), grid.rs columns_baseline, rows_baseline, item_baseline deliberately wrong: every item in one cell of one zero-wide column, every row zero tall, every item at its area's origin
Style crates/dom/src/computed.rs grid_container_style, grid_item_style, is_grid_container the computed values as serialized strings

The three functions #

  • Columns: container values, definite content size, and one entry per in-flow child with its placement values, margins and intrinsic widths. Answers each item's area and alignment, the number of rows, the columns, and the intrinsic width.
  • Rows: the areas and each item's height at its column span's width. Answers the rows and the height they take.
  • Item: an item's area as a rect and its laid-out size. Answers where its border box sits and the height it takes in place of its laid-out one. Also answers an absolutely positioned child's static position (see Decisions).

Decisions #

  • Stylo's grid preference is on in the engine. Stylo parses display: grid and every grid-* property only with the static preference layout.grid.enabled, which it leaves off; without it display: grid is invalid and the box is a block. Document::parse_with_viewport sets it before any stylesheet is parsed.
  • An item is laid out by the same box layout as any block, with the grid area as its containing block (Available::grid_item). Whether an auto width is shrink-to-fit (Available::shrink_to_fit) follows the alignment the columns function answers.
  • Absolutely positioned children are not items. With no generated function for their containing block in a grid, the item function answers their static position: it is called with static_position set, the container's content-box size and the child's own justify-self and align-self, and its answer is added to the content origin. The starting candidate answers the content box's origin. Reusing the item function keeps one alignment rule in one place and adds no new function to score.
  • inline-grid is stacked as a block and sized by its container's available width: the engine has no inline formatting of grid containers and no rule of its own to size it.
  • No provenance record or floor yet. The generated-score check requires a scorer (a ground-truth cache and a runner) for every provenance record; none exists for grid, so the starting candidates carry no record until one does.

Measured #

The tracks that implemented grid by hand raised wpt_checklayout subtests passing from 1,563 to 2,611 and pages with every subtest passing from 51 to 99. Removing that code returns grid pages to the deliberately wrong candidates; the new numbers are in docs/wpt-checklayout.md.