Skip to content

Commit

Permalink
Fix incorrect spaces around - in calc() expression (#12283)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
RobinMalfait committed Oct 25, 2023
1 parent 32a62b7 commit 4b12f83
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Nothing yet!
### Fixed

- Fix incorrect spaces around `-` in `calc()` expression ([#12283](https://github.com/tailwindlabs/tailwindcss/pull/12283))

## [3.3.4] - 2023-10-24

Expand Down
30 changes: 30 additions & 0 deletions src/util/dataTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,29 @@ export function normalize(value, context = null, isRoot = true) {
*/
function normalizeMathOperatorSpacing(value) {
let preventFormattingInFunctions = ['theme']
let preventFormattingKeywords = [
'min-content',
'max-content',
'fit-content',

// Env
'safe-area-inset-top',
'safe-area-inset-right',
'safe-area-inset-bottom',
'safe-area-inset-left',

'titlebar-area-x',
'titlebar-area-y',
'titlebar-area-width',
'titlebar-area-height',

'keyboard-inset-top',
'keyboard-inset-right',
'keyboard-inset-bottom',
'keyboard-inset-left',
'keyboard-inset-width',
'keyboard-inset-height',
]

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

// Skip formatting of known keywords
else if (preventFormattingKeywords.some((keyword) => peek(keyword))) {
let keyword = preventFormattingKeywords.find((keyword) => peek(keyword))
result += keyword
i += keyword.length - 1
}

// Skip formatting inside known functions
else if (preventFormattingInFunctions.some((fn) => peek(fn))) {
result += consumeUntil([')'])
Expand Down
28 changes: 28 additions & 0 deletions tests/arbitrary-values.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,3 +653,31 @@ crosscheck(({ stable, oxide }) => {
})
})
})

it('should not insert spaces around operators inside `env()`', () => {
let config = {
content: [{ raw: html`<div class="grid-cols-[calc(env(safe-area-inset-bottom)+1px)]"></div>` }],
}

return run('@tailwind utilities', config).then((result) => {
expect(result.css).toMatchFormattedCss(css`
.grid-cols-\[calc\(env\(safe-area-inset-bottom\)\+1px\)\] {
grid-template-columns: calc(env(safe-area-inset-bottom) + 1px);
}
`)
})
})

it('should not insert spaces around `-` in arbitrary values that use `max-content`', () => {
let config = {
content: [{ raw: html`<div class="grid-cols-[repeat(3,_minmax(0,_max-content))]"></div>` }],
}

return run('@tailwind utilities', config).then((result) => {
expect(result.css).toMatchFormattedCss(css`
.grid-cols-\[repeat\(3\,_minmax\(0\,_max-content\)\)\] {
grid-template-columns: repeat(3, minmax(0, max-content));
}
`)
})
})
15 changes: 15 additions & 0 deletions tests/normalize-data-types.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ let table = [
['calc(theme(spacing.foo-2))', 'calc(theme(spacing.foo-2))'],
['calc(theme(spacing.foo-bar))', 'calc(theme(spacing.foo-bar))'],

// Prevent formatting inside `var()` functions
['calc(var(--foo-bar-bar)*2)', 'calc(var(--foo-bar-bar) * 2)'],

// Prevent formatting inside `env()` functions
['calc(env(safe-area-inset-bottom)*2)', 'calc(env(safe-area-inset-bottom) * 2)'],
// Should format inside `calc()` nested in `env()`
['env(safe-area-inset-bottom,calc(10px+20px))', 'env(safe-area-inset-bottom,calc(10px + 20px))'],
[
'calc(env(safe-area-inset-bottom,calc(10px+20px))+5px)',
'calc(env(safe-area-inset-bottom,calc(10px + 20px)) + 5px)',
],

// Prevent formatting keywords
['minmax(min-content,25%)', 'minmax(min-content,25%)'],

// Misc
['color(0_0_0/1.0)', 'color(0 0 0/1.0)'],
['color(0_0_0_/_1.0)', 'color(0 0 0 / 1.0)'],
Expand Down

0 comments on commit 4b12f83

Please sign in to comment.