Skip to content

Commit 2d5d282

Browse files
authored
Match math function names only at the start of a function name (#20727)
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.
1 parent 5e9d8c9 commit 2d5d282

4 files changed

Lines changed: 134 additions & 4 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
### Fixed
2+
3+
- Don't treat CSS custom functions whose name ends in a math function name as math functions (e.g. `text-[--my-calc(1px)]` is now a `color` instead of a `font-size`, and `w-[--my-calc(1px-2px)]` doesn't get spaces around the `-`)
4+
- Match math function names case-insensitively when inferring the type of an arbitrary value and adding spaces around math operators (e.g. `text-[Calc(1rem+min(1rem,2rem))]`)

‎packages/tailwindcss/src/utilities.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27029,6 +27029,64 @@ test('text', async () => {
2702927029
).toEqual('')
2703027030
})
2703127031

27032+
test('text with math functions and custom functions', async () => {
27033+
expect(
27034+
await run(
27035+
[
27036+
// Math functions are inferred as a font-size
27037+
'text-[calc(1rem+var(--x))]',
27038+
'text-[min(1rem,var(--x))]',
27039+
'text-[calc(min(1rem,var(--x))+2px)]',
27040+
'text-[Calc(1rem+min(1rem,var(--x)))]',
27041+
27042+
// Custom functions are inferred as a color, even when their name ends
27043+
// in the name of a math function
27044+
'text-[--foo(1px)]',
27045+
'text-[--my-calc(1px)]',
27046+
'text-[--item-rem(1px)]',
27047+
'text-[--my-Calc(1px)]',
27048+
],
27049+
css`
27050+
@tailwind utilities;
27051+
`,
27052+
),
27053+
).toMatchInlineSnapshot(`
27054+
"
27055+
.text-\\[Calc\\(1rem\\+min\\(1rem\\,var\\(--x\\)\\)\\)\\] {
27056+
font-size: Calc(1rem + min(1rem, var(--x)));
27057+
}
27058+
27059+
.text-\\[calc\\(1rem\\+var\\(--x\\)\\)\\] {
27060+
font-size: calc(1rem + var(--x));
27061+
}
27062+
27063+
.text-\\[calc\\(min\\(1rem\\,var\\(--x\\)\\)\\+2px\\)\\] {
27064+
font-size: calc(min(1rem, var(--x)) + 2px);
27065+
}
27066+
27067+
.text-\\[min\\(1rem\\,var\\(--x\\)\\)\\] {
27068+
font-size: min(1rem, var(--x));
27069+
}
27070+
27071+
.text-\\[--foo\\(1px\\)\\] {
27072+
color: --foo(1px);
27073+
}
27074+
27075+
.text-\\[--item-rem\\(1px\\)\\] {
27076+
color: --item-rem(1px);
27077+
}
27078+
27079+
.text-\\[--my-Calc\\(1px\\)\\] {
27080+
color: --my-Calc(1px);
27081+
}
27082+
27083+
.text-\\[--my-calc\\(1px\\)\\] {
27084+
color: --my-calc(1px);
27085+
}
27086+
"
27087+
`)
27088+
})
27089+
2703227090
test('text-shadow', async () => {
2703327091
expect(
2703427092
await run(

‎packages/tailwindcss/src/utils/decode-arbitrary-value.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,24 @@ describe('adds spaces around math operators', () => {
157157

158158
// `-infinity` is a keyword and should not have spaces around the `-`
159159
['atan(1 + -infinity)', 'atan(1 + -infinity)'],
160+
161+
// A `-` before a math function can be an operator or a sign
162+
['calc(1px-min(2px,3px))', 'calc(1px - min(2px, 3px))'],
163+
['calc(100%-min(2px,3px))', 'calc(100% - min(2px, 3px))'],
164+
['calc(1px*-min(2px,3px))', 'calc(1px * -min(2px, 3px))'],
165+
166+
// Custom functions whose name ends in a math function name are not math
167+
// functions
168+
['--my-calc(1px-2px)', '--my-calc(1px-2px)'],
169+
['--item-rem(1px-2px,3px)', '--item-rem(1px-2px,3px)'],
170+
['--calc(1px-2px)', '--calc(1px-2px)'],
171+
['--my-calc(calc(1px-2px))', '--my-calc(calc(1px - 2px))'],
172+
['calc(--my-min(1px-2px)+3px)', 'calc(--my-min(1px-2px) + 3px)'],
173+
['--my-Calc(1px-2px)', '--my-Calc(1px-2px)'],
174+
175+
// Function names are case-insensitive
176+
['Asin(0.5+0.2)', 'Asin(0.5 + 0.2)'],
177+
['Calc(1px+min(2px,3px))', 'Calc(1px + min(2px, 3px))'],
160178
]
161179

162180
it.each(table)('%s', (input, output) => {

‎packages/tailwindcss/src/utils/math-operators.ts‎

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const CLOSE_PAREN = 0x29
1515
const COMMA = 0x2c
1616
const SPACE = 0x20
1717
const PERCENT = 0x25
18+
const UNDERSCORE = 0x5f
1819

1920
const MATH_FUNCTIONS = [
2021
'calc',
@@ -38,8 +39,54 @@ const MATH_FUNCTIONS = [
3839
'round',
3940
]
4041

42+
const MATH_FUNCTIONS_SET = new Set(MATH_FUNCTIONS)
43+
44+
// Characters that can be part of a CSS identifier: `a-z`, `A-Z`, `0-9`, `-`,
45+
// `_` and non-ASCII characters.
46+
function isIdentChar(char: number) {
47+
return (
48+
(char >= LOWER_A && char <= LOWER_Z) ||
49+
(char >= UPPER_A && char <= UPPER_Z) ||
50+
(char >= ZERO && char <= NINE) ||
51+
char === SUB ||
52+
char === UNDERSCORE ||
53+
char >= 0x80
54+
)
55+
}
56+
4157
export function hasMathFn(input: string) {
42-
return input.indexOf('(') !== -1 && MATH_FUNCTIONS.some((fn) => input.includes(`${fn}(`))
58+
let paren = input.indexOf('(')
59+
60+
while (paren !== -1) {
61+
// Scan backwards to the start of the function name. A math function name
62+
// only counts when it's the whole name, so `--my-calc(…)` is not `calc(…)`.
63+
// Function names are case-insensitive, so `CALC(…)` is `calc(…)`.
64+
let start = paren
65+
while (start > 0 && isIdentChar(input.charCodeAt(start - 1))) start--
66+
67+
if (start < paren && MATH_FUNCTIONS_SET.has(input.slice(start, paren).toLowerCase())) {
68+
return true
69+
}
70+
71+
paren = input.indexOf('(', paren + 1)
72+
}
73+
74+
return false
75+
}
76+
77+
// Whether the math function name that starts at `start` is the start of a
78+
// function call. A `-` before it can be an operator (`1px-min(…)`) or a sign
79+
// (`-min(…)`), but not part of a custom function name like `--my-calc(…)`.
80+
function isMathFnStart(input: string, start: number) {
81+
if (start === 0) return true
82+
83+
let prev = input.charCodeAt(start - 1)
84+
if (prev !== SUB) return !isIdentChar(prev)
85+
86+
let identStart = start - 1
87+
while (identStart > 0 && isIdentChar(input.charCodeAt(identStart - 1))) identStart--
88+
89+
return !(input.charCodeAt(identStart) === SUB && input.charCodeAt(identStart + 1) === SUB)
4390
}
4491

4592
export function addWhitespaceAroundMathOperators(input: string) {
@@ -85,7 +132,7 @@ export function addWhitespaceAroundMathOperators(input: string) {
85132
result += input[i]
86133

87134
// Scan backwards to determine the function name. This assumes math
88-
// functions are named with lowercase alphanumeric characters.
135+
// functions are named with alphanumeric characters.
89136
let start = i
90137

91138
for (let j = i - 1; j >= 0; j--) {
@@ -95,15 +142,18 @@ export function addWhitespaceAroundMathOperators(input: string) {
95142
start = j // 0-9
96143
} else if (inner >= LOWER_A && inner <= LOWER_Z) {
97144
start = j // a-z
145+
} else if (inner >= UPPER_A && inner <= UPPER_Z) {
146+
start = j // A-Z
98147
} else {
99148
break
100149
}
101150
}
102151

103152
let fn = input.slice(start, i)
104153

105-
// This is a known math function so start formatting
106-
if (MATH_FUNCTIONS.includes(fn)) {
154+
// This is a known math function so start formatting. Function names are
155+
// case-insensitive, so `Calc(…)` is `calc(…)`.
156+
if (MATH_FUNCTIONS_SET.has(fn.toLowerCase()) && isMathFnStart(input, start)) {
107157
formattable.unshift(true)
108158
continue
109159
}

0 commit comments

Comments
 (0)