Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don’t split vars with numbers in them inside arbitrary values #8091

Merged
merged 2 commits into from Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Types: allow for arbitrary theme values (for 3rd party plugins) ([#7926](https://github.com/tailwindlabs/tailwindcss/pull/7926))
- Don’t split vars with numbers in them inside arbitrary values ([#8091](https://github.com/tailwindlabs/tailwindcss/pull/8091))

### Added

Expand Down
14 changes: 10 additions & 4 deletions src/util/dataTypes.js
Expand Up @@ -42,10 +42,16 @@ export function normalize(value, isRoot = true) {

// Add spaces around operators inside calc() that do not follow an operator
// or '('.
return value.replace(
/(-?\d*\.?\d(?!\b-.+[,)](?![^+\-/*])\D)(?:%|[a-z]+)?|\))([+\-/*])/g,
'$1 $2 '
)
value = value.replace(/calc\(.+\)/g, (match) => {
return match.replace(
/(-?\d*\.?\d(?!\b-.+[,)](?![^+\-/*])\D)(?:%|[a-z]+)?|\))([+\-/*])/g,
'$1 $2 '
)
})

// Add spaces around some operators not inside calc() that do not follow an operator
// or '('.
return value.replace(/(-?\d*\.?\d(?!\b-.+[,)](?![^+\-/*])\D)(?:%|[a-z]+)?|\))([\/])/g, '$1 $2 ')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is strictly necessary, because rgba(0 0 0/0.1) seems to work as expected, but it generates cleaner css!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this because we also apply spaces to aspect-[16/9]. I'd rather remove the spaces if that's fine though.

}

export function url(value) {
Expand Down
42 changes: 42 additions & 0 deletions tests/normalize-data-types.test.js
@@ -0,0 +1,42 @@
import { normalize } from '../src/util/dataTypes'

let table = [
['foo', 'foo'],
['foo-bar', 'foo-bar'],
['16/9', '16 / 9'],

// '_'s are converted to spaces except when escaped
['foo_bar', 'foo bar'],
['foo__bar', 'foo bar'],
['foo\\_bar', 'foo_bar'],

// Urls are preserved as-is
[
'url("https://example.com/abc+def/some-path/2022-01-01-abc/some_underscoered_path")',
'url("https://example.com/abc+def/some-path/2022-01-01-abc/some_underscoered_path")',
],

// var(…) is preserved as is
['var(--foo)', 'var(--foo)'],
['var(--headings-h1-size)', 'var(--headings-h1-size)'],

// calc(…) get's spaces around operators
['calc(1+2)', 'calc(1 + 2)'],
['calc(100%+1rem)', 'calc(100% + 1rem)'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would maybe add 1 more test with nested calc just to verify that that works as well calc(1+calc(100%-20px)) for example should result in calc(1 + calc(100% - 20px))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added and it works as expected 🎉

['calc(1+calc(100%-20px))', 'calc(1 + calc(100% - 20px))'],
['calc(var(--headings-h1-size)*100)', 'calc(var(--headings-h1-size) * 100)'],
[
'calc(var(--headings-h1-size)*calc(100%+50%))',
'calc(var(--headings-h1-size) * calc(100% + 50%))',
],
['var(--heading-h1-font-size)', 'var(--heading-h1-font-size)'],
['var(--my-var-with-more-than-3-words)', 'var(--my-var-with-more-than-3-words)'],
['var(--width, calc(100%+1rem))', 'var(--width, calc(100% + 1rem))'],

// Misc
['color(0_0_0/1.0)', 'color(0 0 0 / 1.0)'],
]

it.each(table)('normalize data: %s', (input, output) => {
expect(normalize(input)).toBe(output)
})