Skip to content

Commit 761717c

Browse files
authored
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 d1317d5 commit 761717c

5 files changed

+77
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Fix source maps issue resulting in a crash ([#11319](https://github.com/tailwindlabs/tailwindcss/pull/11319))
1414
- Fallback to RegEx based parser when using custom transformers or extractors ([#11335](https://github.com/tailwindlabs/tailwindcss/pull/11335))
1515
- Bump `lightningcss` and reflect related improvements in tests ([#11550](https://github.com/tailwindlabs/tailwindcss/pull/11550))
16+
- Fix incorrect spaces around `-` in `calc()` expression ([#12283](https://github.com/tailwindlabs/tailwindcss/pull/12283))
1617

1718
### Added
1819

package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/util/dataTypes.js

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

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

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

tests/arbitrary-values.test.js

+28
Original file line numberDiff line numberDiff line change
@@ -638,3 +638,31 @@ it('should support underscores in arbitrary modifiers', () => {
638638
`)
639639
})
640640
})
641+
642+
it('should not insert spaces around operators inside `env()`', () => {
643+
let config = {
644+
content: [{ raw: html`<div class="grid-cols-[calc(env(safe-area-inset-bottom)+1px)]"></div>` }],
645+
}
646+
647+
return run('@tailwind utilities', config).then((result) => {
648+
expect(result.css).toMatchFormattedCss(css`
649+
.grid-cols-\[calc\(env\(safe-area-inset-bottom\)\+1px\)\] {
650+
grid-template-columns: calc(env(safe-area-inset-bottom) + 1px);
651+
}
652+
`)
653+
})
654+
})
655+
656+
it('should not insert spaces around `-` in arbitrary values that use `max-content`', () => {
657+
let config = {
658+
content: [{ raw: html`<div class="grid-cols-[repeat(3,_minmax(0,_max-content))]"></div>` }],
659+
}
660+
661+
return run('@tailwind utilities', config).then((result) => {
662+
expect(result.css).toMatchFormattedCss(css`
663+
.grid-cols-\[repeat\(3\,_minmax\(0\,_max-content\)\)\] {
664+
grid-template-columns: repeat(3, minmax(0, max-content));
665+
}
666+
`)
667+
})
668+
})

tests/normalize-data-types.test.js

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

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

0 commit comments

Comments
 (0)