Skip to content

css: reject negative padding; add regression coverage for negative margin - #458

Open
tannevaled wants to merge 2 commits into
mainfrom
fix-negative-padding-margin-coverage
Open

tannevaled wants to merge 2 commits into
mainfrom
fix-negative-padding-margin-coverage

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Summary

Triaging a report from go-slides (a downstream project embedding this engine as its renderer), pinned to the much older v0.17.0: negative margins were said to be "ignored outright."

  • At HEAD, negative margins already work correctly. Negative margin-left/margin-top resolve and apply through resolveWidths and block-margin collapsing exactly as a positive value would. Two new regression tests in layout/negativemargin_test.go cover this: a plain block sibling, and a fixture mirroring the reporting repo's own real CSS (a flex row of pills with a gap, where a ::before { display:block; height:0; margin-left:-52px } pulls an arrow glyph out of its own box and into the gap before it). No margin code change was needed or made.
  • Found the actual, adjacent bug while verifying the boundary: padding's CSS grammar restricts it to [0,∞] (unlike margin, which the spec explicitly allows negative), but nothing in this engine — parse-time or layout-time — rejected a negative padding value. It passed straight through and silently shrank the box's content area (padding-left:-20px on a width:50px box produced a 30px box). Width/height already clamp a negative resolved value back to 0 in layout (pre-existing, tested: TestWidthBoundBorderBoxNegative); padding had no equivalent.

Fix

  • css.Length gets a Negative() method (css/value.go), explicitly false for auto regardless of any leftover Px/Percent.
  • Every padding entry point now rejects a negative value the same way an unparsable one already does, dropping the whole declaration: applyPaddingSide, applyPaddingShorthand, and the padding-block/padding-inline 1-or-2-value shorthands (css/parse.go). The block/inline shorthands needed an explicit up-front check across both of their fields — they previously applied each field independently, so a lone negative field left its valid sibling applied.
  • parseLength itself is untouched and stays permissive: margin still needs it to allow negative values.

Tests

  • css/props_test.go: TestPaddingNegativeValueRejected, TestPaddingShorthandNegativeValueDropsWholeDeclaration (including the two-field shorthand atomicity case), TestMarginNegativeValueStillAccepted (the margin side of the boundary, guarding against over-tightening).
  • css/value_test.go: TestLengthNegative, added after a mutsweep mutation-coverage sweep flagged the Auto guard inside Length.Negative as unproven by the existing suite.
  • layout/negativemargin_test.go: TestNegativeMarginLeftShiftsBlockBox, TestNegativeMarginLeftPullsPseudoBeforeIntoGap.

Test plan

  • go test ./... — all packages pass (one pre-existing, unrelated -race flake in js.TestWorkerPostMessageRoundTrip, reproduced on main without this change too, intermittent ~1/3 runs)
  • go test -race ./css ./layout — clean
  • gofmt -l . — no output (pre-existing unformatted files elsewhere, untouched by this PR)
  • go vet ./... — clean
  • govulncheck ./... — no vulnerabilities affecting this code
  • mutsweep on every new guard line — all 5 caught

🤖 Generated with Claude Code

tannevaled and others added 2 commits November 29, 2026 18:48
…erage

A triage request from go-slides (pinned to the much older engine v0.17.0)
reported that negative margins are ignored outright. At HEAD, negative
margin-left/-top already resolve and apply correctly through resolveWidths
and block-margin collapsing (new layout tests confirm both a plain block
sibling and a real-world pseudo-element-pulled-into-a-gap fixture from the
reporting repo's own CSS). No margin code change was needed.

While verifying the margin/padding boundary, found the actual, adjacent bug:
padding — whose CSS grammar restricts it to [0,∞], unlike margin, which the
spec explicitly allows negative — had no rejection anywhere, parse-time or
layout-time, letting a negative padding silently shrink a box's content
area. Width/height already clamp a negative resolved value to 0 in layout
(existing, tested behavior); padding had no equivalent.

Fix: parseLength itself stays shared and permissive (margin still needs it
to allow negative values); add Length.Negative() and gate every padding
entry point (applyPaddingSide, applyPaddingShorthand, and the
padding-block/padding-inline 1-or-2-value shorthands) on it, dropping the
whole declaration the same way an unparsable value already does. The
block/inline shorthands needed an explicit up-front check across both
fields, since they previously applied each field independently and a lone
negative field left its sibling applied.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
mutsweep flagged value.go:710 (`if l.Auto { return false }`) as a survivor:
every Length parseLength actually returns has Px=0/IsPercent=false when
Auto is true, so the guard agreed with the fallthrough by coincidence and
no existing test distinguished them. Add TestLengthNegative, exercising the
method directly (including a hand-built Auto length with a leftover
negative Px/Percent, which parseLength never produces but the method's own
contract must still hold against) so the guard is held by something that
would fail without it, not just something it happens not to break.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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.

1 participant