Skip to content

Commit ddb0bef

Browse files
authored
Fix missing space around - when using % regression (#18289)
This PR fixes a regression we shipped in v4.1.9, when using arbitrary values and injecting spaces around operator. When you use `w-[calc(100%-var(--foo))]`, you expect that this generates valid CSS: ```css width: calc(100% - var(--foo)); ``` But due to a regression, we generated: ```css width: calc(100%-var(--foo)); ``` Which is invalid CSS. This is because the algorithm we used to know when we had to inject a space around the `-` didn't take the `%` sign into account. We also didn't handle uppercase units like `123PX` properly. This PR fixes both issues. ## Test plan 1. Added a regression test for the `%` 2. Added a regression test for uppercase units like `123PX` Fixes: #18288
1 parent 427649e commit ddb0bef

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10-
- Nothing yet!
10+
### Fixed
11+
12+
- Fix missing space around `-` when using `%` ([#18289](https://github.com/tailwindlabs/tailwindcss/pull/18289))
1113

1214
## [4.1.9] - 2025-06-11
1315

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ describe('adds spaces around math operators', () => {
8484
['calc(theme(spacing.foo-2))', 'calc(theme(spacing.foo-2))'],
8585
['calc(theme(spacing.foo-bar))', 'calc(theme(spacing.foo-bar))'],
8686

87+
// With percentages
88+
['calc(100%-var(--foo))', 'calc(100% - var(--foo))'],
89+
90+
// With uppercase units
91+
['calc(100PX-theme(spacing.1))', 'calc(100PX - theme(spacing.1))'],
92+
8793
// Preserving CSS keyword tokens like fit-content without splitting around hyphens in complex expressions
8894
['min(fit-content,calc(100dvh-4rem))', 'min(fit-content, calc(100dvh - 4rem))'],
8995
[

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const LOWER_A = 0x61
22
const LOWER_Z = 0x7a
3+
const UPPER_A = 0x41
4+
const UPPER_Z = 0x5a
35
const LOWER_E = 0x65
46
const UPPER_E = 0x45
57
const ZERO = 0x30
@@ -12,6 +14,7 @@ const OPEN_PAREN = 0x28
1214
const CLOSE_PAREN = 0x29
1315
const COMMA = 0x2c
1416
const SPACE = 0x20
17+
const PERCENT = 0x25
1518

1619
const MATH_FUNCTIONS = [
1720
'calc',
@@ -62,7 +65,12 @@ export function addWhitespaceAroundMathOperators(input: string) {
6265

6366
// If we saw a number before, and we see normal a-z character, then we
6467
// assume this is a value such as `123px`
65-
else if (valuePos !== null && char >= LOWER_A && char <= LOWER_Z) {
68+
else if (
69+
valuePos !== null &&
70+
(char === PERCENT ||
71+
(char >= LOWER_A && char <= LOWER_Z) ||
72+
(char >= UPPER_A && char <= UPPER_Z))
73+
) {
6674
valuePos = i
6775
}
6876

0 commit comments

Comments
 (0)