Skip to content

fix(layout): absorb a margin collapsing through to an isolated box's own edge - #441

Merged
tannevaled merged 2 commits into
mainfrom
isolated-margin-collapse
Nov 15, 2026
Merged

tannevaled merged 2 commits into
mainfrom
isolated-margin-collapse

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Summary

Fixes a margin that collapses through to the edge of an isolated layout (a table cell, a float) being silently discarded instead of landing inside that box's own height.

Closes #440.

layoutIsolated (layout/flex.go, used for table cells, flex/grid items, and replaced-element sizing) and placeFloat (layout/floats.go) both start a fresh block formatting context, because the node they lay out establishes its own (CSS 2.1 §8.3.1/§9.4.1). A descendant's margin that collapses through to either edge of that isolated layout has nowhere further to go.

Confirmed real trigger: news.ycombinator.com's own vote-arrow icon, .votearrow{width:10px;height:10px;margin:3px 2px 6px} inside a table cell's <center><a> wrapper. Margin box = 3+10+6 = 19px. A live Chromium comparison of the real page and real stylesheet (installed Playwright's bundled Chrome for Testing for this, since this environment had none) confirms the real row height is 19px; this engine rendered it at 10px. This was the unresolved root cause behind FIDELITY.md's own "HN's title row is ~3px shorter than Chrome's" gap (round 149), which had correctly guessed the vote-arrow cell as the likely source but never isolated why.

  • Leading margin: place() already folds an absorbed top margin into the box's own Y (firstContentTop) — correct for an ordinary nested block sharing its parent's own BFC, but wrong for the outermost isolated box, which has no real parent position to shift into. Now folded into height instead, with Y pinned to the isolated origin.
  • Trailing margin: place() defers it into the BFC's own pending carry, materialized only by fb.commit() — both call sites discarded that commit's result. Now extends the box's height.
  • Scoped to a BLOCK first child specifically, not any non-zero Y: ordinary inline content (no block children) can also leave Y non-zero for an unrelated, benign reason (line-box ascent/leading), and folding that into height too would double-count it — caught live during testing (a plain <span>1.</span> cell was inflated), fixed, and covered by a regression test.
  • Not the whole HN gap: the full real page still doesn't match Chrome's row height end-to-end (a pre-existing fakeMeasurer test-harness line-height discrepancy, confirmed identical with or without this fix — out of scope here). FIDELITY.md's own Known-gaps bullet is updated to say so honestly.

Test plan

  • layout/isolatedmargin_test.go: the table-cell case (exact 19px), the float case (same mechanism, mirrored), and the inline-content regression guard.
  • Git-stash-confirmed: both positive tests fail with the exact pre-fix symptom (10px instead of 19px); the regression guard correctly still passes either way.
  • go build/go vet/gofmt -l/go test ./.../govulncheck ./... clean.
  • layout package coverage 100.0% (the floor).
  • The combined webengine_nojs webengine_nonet webengine_fontsext build-tag lane builds and vets clean.

🤖 Generated with Claude Code

tannevaled and others added 2 commits November 15, 2026 20:05
…own edge

layoutIsolated (table cells, flex/grid items) and placeFloat both
start a fresh block formatting context, where CSS 2.1's own
collapsing rule stops. A descendant's leading or trailing margin
that collapsed through to that edge had nowhere left to go but was
discarded instead of landing in the box's own height.

Found on news.ycombinator.com: .votearrow's margin box (3px top +
10px + 6px bottom = 19px) inside a table cell rendered as 10px.
Confirmed via a live Chromium comparison of the real markup and CSS.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@tannevaled
tannevaled merged commit d89cf3c into main Nov 15, 2026
13 checks passed
@tannevaled
tannevaled deleted the isolated-margin-collapse branch November 15, 2026 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

layout: a margin collapsing through to an isolated box's own edge is discarded

1 participant