package toffee

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Module Compute_blockSource

CSS Block layout algorithm implementation.

Implements the CSS block layout algorithm for containers with block-level children. This module computes layout for nodes using display:block, handling margin collapsing, auto-sizing, and positioning of both in-flow and absolutely positioned children.

Algorithm Overview

Block layout proceeds in phases:

  1. Generate item list from children, resolving styles and filtering display:none nodes
  2. Determine container width from known dimensions or intrinsic content width
  3. Perform final layout on in-flow children with margin collapsing
  4. Layout absolutely positioned children in the containing block
  5. Compute final content size and margin collapse state

Key Invariants

  • Vertical margins collapse between adjacent in-flow block boxes under specific conditions (no padding/border separation, overflow:visible, position:relative)
  • Absolutely positioned children are removed from normal flow and sized relative to the containing block
  • Container width stretches children by default unless they are tables or have explicit width
  • Auto margins resolve to 0 for vertical margins of in-flow blocks, but expand to fill space for horizontal margins
  • Margin collapsing can occur through empty blocks with no padding, border, or height

Implementation Details

The algorithm handles margin collapsing via Collapsible_margin_set tracking. In-flow layout maintains two margin sets: first_child_top_margin_set for collapsing with container's top margin, and active_collapsible_margin_set for tracking bottom margins. Nodes can be "collapsed through" when they have no content, padding, border, or height constraints.

Absolutely positioned children use static position as fallback when inset properties are auto. Static position is computed during in-flow layout pass even though the child is not placed in normal flow.

Scrollbar gutters are reserved in the opposite axis (vertical scroll reserves horizontal space). Tables receive special treatment and do not participate in stretch-sizing.

Sourceval compute_block_layout : (module Tree.LAYOUT_PARTIAL_TREE with type t = 't) -> 't -> Tree.Node_id.t -> Tree.Layout_input.t -> Tree.Layout_output.t

compute_block_layout tree_module tree node_id inputs computes block layout for node_id.

Implements CSS block layout with margin collapsing, auto-sizing, and absolute positioning. Returns Layout_output containing final size, content size, and margin collapse state.

Algorithm:

  1. Resolve size constraints from style properties and parent size
  2. Short-circuit if size fully determined and mode is Compute_size
  3. Generate block items from children, filtering display:none
  4. Determine container width (known dimension or intrinsic content-based)
  5. Layout in-flow children with margin collapsing and auto-margins
  6. Layout absolutely positioned children in containing block area
  7. Compute whether node can be collapsed through
  8. Return final size, content size, and margin sets

The implementation tracks margin collapsing state through Collapsible_margin_set. Vertical margins of adjacent in-flow blocks collapse when separated only by collapsible space. Auto horizontal margins expand to fill available space. Absolute children use static position when inset is auto.