Skip to content

Commit 4b12f83

Browse files
committed
Fix incorrect spaces around - in calc() expression (#12283)
* sync package-lock.json * prevent formatting inside `env()` when formatting `calc` function * prevent formatting keywords in `calc` * add dedicated normalization tests * `calc()` in `env()` should be formatted * update changelog
1 parent 32a62b7 commit 4b12f83

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

CHANGELOG.md

+3-1
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 incorrect spaces around `-` in `calc()` expression ([#12283](https://github.com/tailwindlabs/tailwindcss/pull/12283))
1113

1214
## [3.3.4] - 2023-10-24
1315

src/util/dataTypes.js

+30
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,29 @@ export function normalize(value, context = null, isRoot = true) {
8484
*/
8585
function normalizeMathOperatorSpacing(value) {
8686
let preventFormattingInFunctions = ['theme']
87+
let preventFormattingKeywords = [
88+
'min-content',
89+
'max-content',
90+
'fit-content',
91+
92+
// Env
93+
'safe-area-inset-top',
94+
'safe-area-inset-right',
95+
'safe-area-inset-bottom',
96+
'safe-area-inset-left',
97+
98+
'titlebar-area-x',
99+
'titlebar-area-y',
100+
'titlebar-area-width',
101+
'titlebar-area-height',
102+
103+
'keyboard-inset-top',
104+
'keyboard-inset-right',
105+
'keyboard-inset-bottom',
106+
'keyboard-inset-left',
107+
'keyboard-inset-width',
108+
'keyboard-inset-height',
109+
]
87110

88111
return value.replace(/(calc|min|max|clamp)\(.+\)/g, (match) => {
89112
let result = ''
@@ -126,6 +149,13 @@ function normalizeMathOperatorSpacing(value) {
126149
result += consumeUntil([')', ','])
127150
}
128151

152+
// Skip formatting of known keywords
153+
else if (preventFormattingKeywords.some((keyword) => peek(keyword))) {
154+
let keyword = preventFormattingKeywords.find((keyword) => peek(keyword))
155+
result += keyword
156+
i += keyword.length - 1
157+
}
158+
129159
// Skip formatting inside known functions
130160
else if (preventFormattingInFunctions.some((fn) => peek(fn))) {
131161
result += consumeUntil([')'])

tests/arbitrary-values.test.js

+28
Original file line numberDiff line numberDiff line change
@@ -653,3 +653,31 @@ crosscheck(({ stable, oxide }) => {
653653
})
654654
})
655655
})
656+
657+
it('should not insert spaces around operators inside `env()`', () => {
658+
let config = {
659+
content: [{ raw: html`<div class="grid-cols-[calc(env(safe-area-inset-bottom)+1px)]"></div>` }],
660+
}
661+
662+
return run('@tailwind utilities', config).then((result) => {
663+
expect(result.css).toMatchFormattedCss(css`
664+
.grid-cols-\[calc\(env\(safe-area-inset-bottom\)\+1px\)\] {
665+
grid-template-columns: calc(env(safe-area-inset-bottom) + 1px);
666+
}
667+
`)
668+
})
669+
})
670+
671+
it('should not insert spaces around `-` in arbitrary values that use `max-content`', () => {
672+
let config = {
673+
content: [{ raw: html`<div class="grid-cols-[repeat(3,_minmax(0,_max-content))]"></div>` }],
674+
}
675+
676+
return run('@tailwind utilities', config).then((result) => {
677+
expect(result.css).toMatchFormattedCss(css`
678+
.grid-cols-\[repeat\(3\,_minmax\(0\,_max-content\)\)\] {
679+
grid-template-columns: repeat(3, minmax(0, max-content));
680+
}
681+
`)
682+
})
683+
})

tests/normalize-data-types.test.js

+15
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,21 @@ let table = [
6969
['calc(theme(spacing.foo-2))', 'calc(theme(spacing.foo-2))'],
7070
['calc(theme(spacing.foo-bar))', 'calc(theme(spacing.foo-bar))'],
7171

72+
// Prevent formatting inside `var()` functions
73+
['calc(var(--foo-bar-bar)*2)', 'calc(var(--foo-bar-bar) * 2)'],
74+
75+
// Prevent formatting inside `env()` functions
76+
['calc(env(safe-area-inset-bottom)*2)', 'calc(env(safe-area-inset-bottom) * 2)'],
77+
// Should format inside `calc()` nested in `env()`
78+
['env(safe-area-inset-bottom,calc(10px+20px))', 'env(safe-area-inset-bottom,calc(10px + 20px))'],
79+
[
80+
'calc(env(safe-area-inset-bottom,calc(10px+20px))+5px)',
81+
'calc(env(safe-area-inset-bottom,calc(10px + 20px)) + 5px)',
82+
],
83+
84+
// Prevent formatting keywords
85+
['minmax(min-content,25%)', 'minmax(min-content,25%)'],
86+
7287
// Misc
7388
['color(0_0_0/1.0)', 'color(0 0 0/1.0)'],
7489
['color(0_0_0_/_1.0)', 'color(0 0 0 / 1.0)'],

0 commit comments

Comments
 (0)