Fix zero-unit stripping inside functions after the first (invalid CSS)#1750
Open
durvesh1992 wants to merge 1 commit into
Open
Fix zero-unit stripping inside functions after the first (invalid CSS)#1750durvesh1992 wants to merge 1 commit into
durvesh1992 wants to merge 1 commit into
Conversation
normalizeZeroDimensions must not strip the unit from a zero inside a function (e.g. calc(0px + 1%) — a unitless 0 can't be added to a %). It tracks the active function via endFunction, but the guard `node.type === 'function' && !endFunction` only ever recorded the FIRST function. For a later function, endFunction had already been reset to 0, so its children were treated as outside a function and their units were stripped — emitting invalid CSS like `max(1px,2px) calc(0 + 1%)`. Track every function with Math.max(endFunction, sourceEndIndex); the existing reset still clears it once walking moves past the last function, so bare zeros outside functions are still normalized. Adds a regression test (fails before / passes after).
|
Someone is attempting to deploy a commit to the Meta Open Source Team on Vercel. A member of the Team first needs to authorize it. |
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
normalizeZeroDimensionsstrips redundant units from zero values (0px→0) but must not do so inside a function — e.g. incalc(0px + 1%), a unitless0is a<number>and cannot be added to a<percentage>. It tracks the active function's range viaendFunction, but the guard only recorded the first function:postcss-value-parserwalks a function node before its children, so when a second function is reached the&& !endFunctionguard blocks the update, the reset line then zeroesendFunction, and the second function's zero values get their units stripped — producing invalid CSS:calc(0px + 1%)(alone)calc(0px + 1%)✅calc(0px + 1%) max(1px,2px)(1st)calc(0px + 1%) …✅max(1px,2px) calc(0px + 1%)(2nd)max(1px,2px) calc(0 + 1%)❌Fix
Record every function's end with
Math.max(endFunction, node.sourceEndIndex ?? 0). The existingsourceIndex > endFunctionreset still clears the range once walking moves past the last function, so bare zeros outside functions (margin: 0px→0,calc(5px) 0px→… 0) still normalize correctly.Test plan
Added a regression test (
width: 'max(1px, 2px) calc(0px + 1%)'stayscalc(0px + 1%)) that fails before and passes after. Full babel-plugin suite: 960 passed, no snapshot regressions.