Skip to content

Commit ce7ac96

Browse files
Properly handle subtraction followed by a variable in arbitrary values (#10074)
* fix normalizing subtraction followed by a variable * Add test * Update changelog Co-authored-by: Jordan Pittman <jordan@cryptica.me>
1 parent 25d17db commit ce7ac96

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2626
- Deterministic sorting of arbitrary variants ([#10016](https://github.com/tailwindlabs/tailwindcss/pull/10016))
2727
- Add `data` key to theme types ([#10023](https://github.com/tailwindlabs/tailwindcss/pull/10023))
2828
- Prevent invalid arbitrary variant selectors from failing the build ([#10059](https://github.com/tailwindlabs/tailwindcss/pull/10059))
29+
- Properly handle subtraction followed by a variable ([#10074](https://github.com/tailwindlabs/tailwindcss/pull/10074))
2930

3031
### Changed
3132

src/util/dataTypes.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ function isCSSFunction(value) {
1010
return cssFunctions.some((fn) => new RegExp(`^${fn}\\(.*\\)`).test(value))
1111
}
1212

13+
const placeholder = '--tw-placeholder'
14+
const placeholderRe = new RegExp(placeholder, 'g')
15+
1316
// This is not a data type, but rather a function that can normalize the
1417
// correct values.
1518
export function normalize(value, isRoot = true) {
@@ -49,10 +52,14 @@ export function normalize(value, isRoot = true) {
4952
// Add spaces around operators inside math functions like calc() that do not follow an operator
5053
// or '('.
5154
value = value.replace(/(calc|min|max|clamp)\(.+\)/g, (match) => {
52-
return match.replace(
53-
/(-?\d*\.?\d(?!\b-.+[,)](?![^+\-/*])\D)(?:%|[a-z]+)?|\))([+\-/*])/g,
54-
'$1 $2 '
55-
)
55+
let vars = []
56+
return match
57+
.replace(/var\((--.+?)[,)]/g, (match, g1) => {
58+
vars.push(g1)
59+
return match.replace(g1, placeholder)
60+
})
61+
.replace(/(-?\d*\.?\d(?!\b-\d.+[,)](?![^+\-/*])\D)(?:%|[a-z]+)?|\))([+\-/*])/g, '$1 $2 ')
62+
.replace(placeholderRe, () => vars.shift())
5663
})
5764

5865
return value

tests/arbitrary-values.test.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,15 @@
191191
.w-\[\'\}\{\}\'\] {
192192
width: '}{}';
193193
}
194+
.min-w-\[calc\(1-var\(--something\)\*0\.5\)\] {
195+
min-width: calc(1 - var(--something) * 0.5);
196+
}
197+
.min-w-\[calc\(1-\(var\(--something\)\*0\.5\)\)\] {
198+
min-width: calc(1 - (var(--something) * 0.5));
199+
}
200+
.min-w-\[calc\(1-\(\(12-3\)\*0\.5\)\)\] {
201+
min-width: calc(1 - ((12 - 3) * 0.5));
202+
}
194203
.min-w-\[3\.23rem\] {
195204
min-width: 3.23rem;
196205
}

tests/arbitrary-values.test.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
<div class="w-[var(--width)]"></div>
6868
<div class="w-[var(--width,calc(100%+1rem))]"></div>
6969
<div class="w-[calc(100%/3-1rem*2)]"></div>
70+
<div class="min-w-[calc(1-var(--something)*0.5)]"></div>
71+
<div class="min-w-[calc(1-(var(--something)*0.5))]"></div>
72+
<div class="min-w-[calc(1-((12-3)*0.5))]"></div>
7073

7174
<div class="min-w-[3.23rem]"></div>
7275
<div class="min-w-[calc(100%+1rem)]"></div>

0 commit comments

Comments
 (0)