Repository navigation
css: reject negative padding; add regression coverage for negative margin - #458
Open
tannevaled wants to merge 2 commits into
Open
tannevaled wants to merge 2 commits into
tannevaled wants to merge 2 commits into
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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."
margin-left/margin-topresolve and apply throughresolveWidthsand block-margin collapsing exactly as a positive value would. Two new regression tests inlayout/negativemargin_test.gocover this: a plain block sibling, and a fixture mirroring the reporting repo's own real CSS (a flex row of pills with agap, 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.[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:-20pxon awidth:50pxbox 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.Lengthgets aNegative()method (css/value.go), explicitlyfalseforautoregardless of any leftoverPx/Percent.applyPaddingSide,applyPaddingShorthand, and thepadding-block/padding-inline1-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.parseLengthitself 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 amutsweepmutation-coverage sweep flagged theAutoguard insideLength.Negativeas unproven by the existing suite.layout/negativemargin_test.go:TestNegativeMarginLeftShiftsBlockBox,TestNegativeMarginLeftPullsPseudoBeforeIntoGap.Test plan
go test ./...— all packages pass (one pre-existing, unrelated-raceflake injs.TestWorkerPostMessageRoundTrip, reproduced onmainwithout this change too, intermittent ~1/3 runs)go test -race ./css ./layout— cleangofmt -l .— no output (pre-existing unformatted files elsewhere, untouched by this PR)go vet ./...— cleangovulncheck ./...— no vulnerabilities affecting this codemutsweepon every new guard line — all 5 caught🤖 Generated with Claude Code