Skip to content

Core | Utils | parseUnit: Fix calc() negative operands and leading-dot decimals - #1001

Closed
bardrop wants to merge 1 commit into
f5:mainfrom
bardrop-oss:fix/parse-unit-calc
Closed

bardrop wants to merge 1 commit into
f5:mainfrom
bardrop-oss:fix/parse-unit-calc

Conversation

@bardrop

@bardrop bardrop commented Nov 14, 2026

Copy link
Copy Markdown

What & why

Fixes #1000

parseUnit gave wrong numbers for some calc() expressions (used by Annotations since #624):

input (basis) before after
calc(100% - 2 * (10px - 30px)) (200) 20040 240
calc(10px - 2 * -3) 106 16
calc(20 - 4 / -2) 202 22
calc(.5 * 100%) (200) 0.1 100
calc(100% * .25) (200) 200 50
calc(.5% + 10px) (200) 0.2 11

Cause: in evaluateSimpleExpression the left operand of * / / could take a -, so in 200-2*-20 the subtraction sign was eaten into -2*-20 = 40, and 40 was glued onto 200. The number regex also missed leading-dot decimals like .5.

Change (only packages/ts/src/utils/misc.ts):

  • evaluateSimpleExpression now splits the expression into numbers and operators first. A +/- is read as a sign only where a number is expected (at the start or right after an operator). Then * / / are applied, then + / -, left to right. Malformed input still throws, so parseUnit still warns and returns 0.
  • The percentage regex accepts leading-dot values (.5%).

Checklist

  • Lint passes on the files I changed (eslint)
  • Builds locally (packages/ts: pnpm build)
  • New packages/ts/src/utils/misc.spec.ts: the two new cases fail on main and pass now; packages/ts vitest 152 pass.
  • Not run: full pnpm cmds-report (only core TS code changed, no wrappers or docs).

…t decimals

evaluateSimpleExpression let the left operand of * and / take a '-', so
200-2*-20 became 200 followed by 40 (20040). Numbers like .5 or .25 were
not matched at all. The expression is now split into numbers and
operators first; a sign is only part of a number where a number is
expected. Percentages also accept leading-dot values (.5%).

Fixes f5#1000
@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

CLA Assistant Lite bot ✅ All required contributors have signed the F5 CLA for this PR. Thank you!

@bardrop

bardrop commented Nov 14, 2026

Copy link
Copy Markdown
Author

I have hereby read the F5 CLA and agree to its terms

@suryahanumandla
suryahanumandla self-requested a review November 16, 2026 18:55
@rokotyan

Copy link
Copy Markdown
Contributor

Thanks for tackling this. I confirmed that this PR passes all six examples from #1000. I found three cases to address before merging:

  • calc(0 / 0) returns NaN without a warning. On main, it warns and returns 0. The fallback matters because Annotations passes the result to SVG attributes.
  • Replacing percentages with text still breaks some expressions. parseUnit('calc(1% + 3px)', 0.00001) returns 0 instead of 3.0000001 because the resolved percentage uses scientific notation that validation rejects. parseUnit('calc(+10% + 3px)', -20) also returns 0 instead of 1 because the replacement adds a second sign.
  • TypeScript still rejects parseUnit('calc(100% - 20px)', 200) with TS2345. Adding calc() to LengthUnit would also let typed annotation options accept these expressions.

I opened draft #1002 with an implementation that resolves percentages and parenthesized values numerically, plus regression tests.

@bardrop

bardrop commented Nov 17, 2026

Copy link
Copy Markdown
Author

Thanks for checking it so closely. You're right on all three, and #1002 handles them more cleanly than my string-replace approach (numeric percentages, no re-parse, and the LengthUnit type). I'm happy for #1002 to land instead of this one; I'll close this PR once yours is out of draft, or sooner if you prefer.

One small thing I noticed while trying #1002: division by zero still comes back as Infinity with no warning, e.g. parseUnit('calc(10px / (50% - 50%))', 200) or parseUnit('calc(1 / 0)'). Since the result goes into SVG attributes like the 0 / 0 case, checking !Number.isFinite(result) instead of Number.isNaN(result) in the final guard would warn and return 0 for those too.

@rokotyan rokotyan mentioned this pull request Nov 17, 2026
5 tasks done
@lee00678

Copy link
Copy Markdown
Collaborator

@rokotyan do you want to address the comment @bardrop mentioned?

@bardrop

bardrop commented Nov 17, 2026

Copy link
Copy Markdown
Author

Closing this now that #1002 is ready for review, as promised. Thanks @rokotyan for the thorough fix, and @lee00678 for following up on the division-by-zero case.

@bardrop bardrop closed this Nov 17, 2026
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.

parseUnit calc() misparses subtracted negative products/quotients and leading-dot decimals

3 participants