Repository navigation
Commit 2d5d282
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
- .changelog
- packages/tailwindcss/src
- utils
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27029 | 27029 | | |
27030 | 27030 | | |
27031 | 27031 | | |
| 27032 | + | |
| 27033 | + | |
| 27034 | + | |
| 27035 | + | |
| 27036 | + | |
| 27037 | + | |
| 27038 | + | |
| 27039 | + | |
| 27040 | + | |
| 27041 | + | |
| 27042 | + | |
| 27043 | + | |
| 27044 | + | |
| 27045 | + | |
| 27046 | + | |
| 27047 | + | |
| 27048 | + | |
| 27049 | + | |
| 27050 | + | |
| 27051 | + | |
| 27052 | + | |
| 27053 | + | |
| 27054 | + | |
| 27055 | + | |
| 27056 | + | |
| 27057 | + | |
| 27058 | + | |
| 27059 | + | |
| 27060 | + | |
| 27061 | + | |
| 27062 | + | |
| 27063 | + | |
| 27064 | + | |
| 27065 | + | |
| 27066 | + | |
| 27067 | + | |
| 27068 | + | |
| 27069 | + | |
| 27070 | + | |
| 27071 | + | |
| 27072 | + | |
| 27073 | + | |
| 27074 | + | |
| 27075 | + | |
| 27076 | + | |
| 27077 | + | |
| 27078 | + | |
| 27079 | + | |
| 27080 | + | |
| 27081 | + | |
| 27082 | + | |
| 27083 | + | |
| 27084 | + | |
| 27085 | + | |
| 27086 | + | |
| 27087 | + | |
| 27088 | + | |
| 27089 | + | |
27032 | 27090 | | |
27033 | 27091 | | |
27034 | 27092 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
157 | 157 | | |
158 | 158 | | |
159 | 159 | | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
160 | 178 | | |
161 | 179 | | |
162 | 180 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| |||
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
41 | 57 | | |
42 | | - | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
43 | 90 | | |
44 | 91 | | |
45 | 92 | | |
| |||
85 | 132 | | |
86 | 133 | | |
87 | 134 | | |
88 | | - | |
| 135 | + | |
89 | 136 | | |
90 | 137 | | |
91 | 138 | | |
| |||
95 | 142 | | |
96 | 143 | | |
97 | 144 | | |
| 145 | + | |
| 146 | + | |
98 | 147 | | |
99 | 148 | | |
100 | 149 | | |
101 | 150 | | |
102 | 151 | | |
103 | 152 | | |
104 | 153 | | |
105 | | - | |
106 | | - | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
107 | 157 | | |
108 | 158 | | |
109 | 159 | | |
| |||
0 commit comments