You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Recognize abs(…) and sign(…) as math functions in arbitrary values - #20716
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.
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: 4
Check name
Status
Explanation
Title check
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
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
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
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.
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.
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.
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
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
abs(…)andsign(…)are missing fromMATH_FUNCTIONSinpackages/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:delay-[calc(abs(var(--i)-2)*50ms)], generatescalc(abs(var(--i)-2) * 50ms). The browser can't readvar(--i)-2as a subtraction, so every item gets0s(instead of100msfor--i: 0).w-[abs(20%-100px)]generateswidth: abs(20%-100px), which is dropped as invalid.hasMathFndoesn't see them, so the value is not inferred as a length.text-[abs(var(--size))]generatescolor: abs(var(--size))instead offont-size, whiletext-[clamp(…)]already generatesfont-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
abs(…)andsign(…)cases, alone and nested incalc(…), to the math operator table indecode-arbitrary-value.test.ts, andtext-[abs(var(--size))]to the existingtexttest inutilities.test.ts. All five fail onmain(e.g.Received: "abs(20%-100px)", andcolor: abs(var(--size))in the snapshot) and pass with this change.npx vitest run packages/tailwindcss: 45 files, 5405 tests passed.mainand on this branch, and loaded the CSS in Chrome 156. Onmainthe delay is0sand thewidthis dropped. On this branch the delay is0.1s, the width is20px, and the font size is40pxfor--size: -40px.prettier --checkon the changed files, andnode ./scripts/changelog.mjs check.Not run:
cargo testand the integration tests, since I don't have the Rust toolchain set up here. The change is in the TypeScript core only.