Skip to content

Recognize abs(…) and sign(…) as math functions in arbitrary values - #20716

Merged
RobinMalfait merged 2 commits into
tailwindlabs:mainfrom
Mwrnk:fix/abs-sign-math-functions
Nov 18, 2026
Merged

RobinMalfait merged 2 commits into
tailwindlabs:mainfrom
Mwrnk:fix/abs-sign-math-functions

Conversation

@Mwrnk

@Mwrnk Mwrnk commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Summary

abs(…) and sign(…) are missing from MATH_FUNCTIONS in packages/tailwindcss/src/utils/math-operators.ts. The list has every other CSS Values 4 math function, and both places that use it skip these two:

  • No whitespace is added around operators inside them. A delay that grows from the middle item, delay-[calc(abs(var(--i)-2)*50ms)], generates calc(abs(var(--i)-2) * 50ms). The browser can't read var(--i)-2 as a subtraction, so every item gets 0s (instead of 100ms for --i: 0). w-[abs(20%-100px)] generates width: abs(20%-100px), which is dropped as invalid.
  • hasMathFn doesn't see them, so the value is not inferred as a length. text-[abs(var(--size))] generates color: abs(var(--size)) instead of font-size, while text-[clamp(…)] already generates font-size.

This adds both names to the list. I found this while reading math-operators.ts, not in a project. Both functions are Baseline 2025 (Chrome 138, Firefox 118, Safari 15.4).

Test plan

  • Added abs(…) and sign(…) cases, alone and nested in calc(…), to the math operator table in decode-arbitrary-value.test.ts, and text-[abs(var(--size))] to the existing text test in utilities.test.ts. All five fail on main (e.g. Received: "abs(20%-100px)", and color: abs(var(--size)) in the snapshot) and pass with this change.
  • npx vitest run packages/tailwindcss: 45 files, 5405 tests passed.
  • Compiled the classes above on main and on this branch, and loaded the CSS in Chrome 156. On main the delay is 0s and the width is dropped. On this branch the delay is 0.1s, the width is 20px, and the font size is 40px for --size: -40px.
  • prettier --check on the changed files, and node ./scripts/changelog.mjs check.

Not run: cargo test and the integration tests, since I don't have the Rust toolchain set up here. The change is in the TypeScript core only.

@Mwrnk
Mwrnk requested a review from a team as a code owner November 13, 2026 18:22
@greptile-apps

greptile-apps Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] This PR appears safe to merge with the quoted-text issue explicitly deferred.

Reviews (8) · Last reviewed commit: "Sort the list of math functions alphabet..." · Reviewed by Greptile

@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3ec89cb2-73c8-4451-ae03-252de657d536
Commits

Reviewing files that changed from the base of the PR and between d06ed53 and 51f94f9.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

Walkthrough

The math-function list now recognizes abs and sign. Tests cover subtraction spacing in these functions, including nested calc() expressions. Utility tests cover text-[abs(var(--size))] producing a font-size declaration with abs(var(--size)) as its value. The changelog records the fix.

Merge Risk: Low · previous review d06ed

Lowercase abs(…) and sign(…) now get correct operator spacing and length inference. Uppercase function names such as ABS(…) still skip spacing, as they already did for other math functions. This is a small existing gap that can be fixed in a follow-up.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: adding abs(…) and sign(…) to the math functions recognized in arbitrary values. The changeset adds both names to MATH_FUNCTIONS in math-operators.ts and adds tests for…
Description check Passed The description states that abs(…) and sign(…) are missing from MATH_FUNCTIONS, which is the change in math-operators.ts. It gives concrete examples of the affected output, describes the added test ca…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: adding abs(…) and sign(…) to the math functions recognized in arbitrary values. The changeset adds both names to MATH_FUNCTIONS in math-operators.ts and adds tests for them, so the title matches the changeset.

Full details: Description check

Explanation

The description states that abs(…) and sign(…) are missing from MATH_FUNCTIONS, which is the change in math-operators.ts. It gives concrete examples of the affected output, describes the added test cases, and includes a test plan. It is directly related to the changeset.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@RobinMalfait
RobinMalfait self-requested a review November 16, 2026 21:26
@RobinMalfait RobinMalfait self-assigned this Nov 16, 2026
@RobinMalfait
RobinMalfait force-pushed the fix/abs-sign-math-functions branch from 0a69182 to b995859 Compare November 16, 2026 21:27
@RobinMalfait

Copy link
Copy Markdown
Contributor

I rebased this PR on the latest main and force pushed it.

Sent by an AI assistant on behalf of @RobinMalfait.

RobinMalfait added a commit that referenced this pull request Nov 18, 2026
This PR makes sure that a CSS custom function whose name ends in a math
function name is not treated as a math function.

`hasMathFn` used a substring match (`input.includes('calc(')`). So
`--my-calc(…)` and `--item-rem(…)` counted as math functions, and the
value was inferred as a number, percentage, ratio or length:

```
text-[--my-calc(1px)]   → font-size: --my-calc(1px)   (now: color)
text-[--item-rem(1px)]  → font-size: --item-rem(1px)  (now: color)
bg-[--my-calc(1px)]     → background-position: …      (now: background-color)
```

With `abs` and `sign` in `MATH_FUNCTIONS`
(#20716),
`text-[--design(1px)]` would hit the same bug.

`addWhitespaceAroundMathOperators` had the same issue. Its backward scan
for the function name stops at `-`, so `w-[--my-calc(1px-2px)]` became
`width: --my-calc(1px - 2px)`.

## Changes

- `hasMathFn` scans back from each `(` to the start of the identifier
(`a-z`, `A-Z`, `0-9`, `-`, `_`, non-ASCII), and checks that the whole
name is a math function. It's also about 4x faster than the 19
`includes` calls in a micro-benchmark on typical values.
- `addWhitespaceAroundMathOperators` skips a math function name that
follows an identifier character, or that is part of a dashed identifier
(`--…`). A single `-` before the name still counts as an operator or a
sign, so `calc(1px-min(2px,3px))` still becomes `calc(1px - min(2px,
3px))`. The early bail is unchanged.
- Both compare the whole name in lowercase, because CSS function names
are case-insensitive. So `Calc(1rem+min(1rem,2rem))` is a length and
gets spaces, and `Asin(0.5+0.2)` keeps the spaces it got on `main`. The
early bail of the formatter still looks for lowercase names, so an input
like `CALC(1px+2px)` is inferred as a length but is not formatted, as on
`main`.

Two cases change on purpose. `text-[CALC(1px)]` is now a `font-size`
instead of a `color`. And `text-[-min(1px,2px)]` is now a `color`
instead of a `font-size`. In CSS, `-min(` is a function named `-min`,
not a negated `min(…)`. The formatter still adds the spaces in it.

## Test plan

- Added a `text` test: `calc(…)`, `min(…)` and `calc(min(…))` are still
a `font-size`. `--foo(…)`, `--my-calc(…)` and `--item-rem(…)` are a
`color`.
- Added formatter cases to `decode-arbitrary-value.test.ts`, including
`calc(1px-min(2px,3px))`, `calc(1px*-min(2px,3px))`,
`--my-calc(1px-2px)`, `--my-calc(calc(1px-2px))`, `Asin(0.5+0.2)` and
`Calc(1px+min(2px,3px))`.
- The new tests fail without the fix.
- `pnpm vitest run --project tailwindcss` passes.
- I compared the output of 1120 candidates on `main` and on this branch:
every arbitrary-value candidate with a `(` from `utilities.test.ts` and
`variants.test.ts`, plus edge cases in 22 utilities. Only the edge cases
with custom functions, names like `fooXcalc(…)`, `écalc(…)`, `\_calc(…)`
and `-min(…)`, and mixed-case names like `Calc(…)` and `CALC(…)`
changed. No candidate from the test files changed.
@RobinMalfait
RobinMalfait force-pushed the fix/abs-sign-math-functions branch from b995859 to d06ed53 Compare November 18, 2026 16:58
@RobinMalfait
RobinMalfait enabled auto-merge (squash) November 18, 2026 16:58

@RobinMalfait RobinMalfait left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! I added a small commit on top that sorts the list of math functions alphabetically.

Sent by an AI assistant on behalf of @RobinMalfait.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

⚠️ Outside diff range comments (1)

Minor · Make the math-function prefilter case-insensitive. · math-operators.ts:96

packages/tailwindcss/src/utils/math-operators.ts:96
Minor · Functional Correctness · Quick win

Make the math-function prefilter case-insensitive.

The prefilter at line 96 compares the raw input against lowercase names. An uppercase name such as ABS( never matches, so w-[ABS(20%-100px)] returns unchanged. The later name check already lowercases the name, and the surrounding comments state that function names are case-insensitive. Lowercasing the gate makes it consistent with that check.

Without the spacing, 20%-100px is not a valid calc()-style subtraction. The -100px token is read as a negative dimension. This gap predates this PR, which leaves the gate unchanged. It affects the new abs and sign names in the same way as the existing names.

Suggested fix

-  if (!MATH_FUNCTIONS.some((fn) => input.includes(fn))) {
+  if (!MATH_FUNCTIONS.some((fn) => input.toLowerCase().includes(fn))) {
Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 36ee8fa7-5eec-4082-aa17-2d4ca0f04148
Commits

Reviewing files that changed from the base of the PR and between b995859 and d06ed53.

Files selected for processing: 2

  • packages/tailwindcss/src/utilities.test.ts
  • packages/tailwindcss/src/utils/math-operators.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@RobinMalfait
RobinMalfait force-pushed the fix/abs-sign-math-functions branch from d06ed53 to 51f94f9 Compare November 18, 2026 17:08
@RobinMalfait
RobinMalfait merged commit e0a9863 into tailwindlabs:main Nov 18, 2026
13 checks passed
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.

2 participants