From 648710d4f09a660e2c094027c234789e1075b1ff Mon Sep 17 00:00:00 2001 From: sibbng Date: Wed, 14 Dec 2022 14:47:22 +0300 Subject: [PATCH 1/3] fix normalizing subtraction followed by a variable --- src/util/dataTypes.js | 15 +++++++++++---- tests/arbitrary-values.test.css | 6 ++++++ tests/arbitrary-values.test.html | 2 ++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/util/dataTypes.js b/src/util/dataTypes.js index 1d888e211ba3..9936935a59fe 100644 --- a/src/util/dataTypes.js +++ b/src/util/dataTypes.js @@ -10,6 +10,9 @@ function isCSSFunction(value) { return cssFunctions.some((fn) => new RegExp(`^${fn}\\(.*\\)`).test(value)) } +const placeholder = '--tw-placeholder' +const placeholderRe = new RegExp(placeholder, 'g') + // This is not a data type, but rather a function that can normalize the // correct values. export function normalize(value, isRoot = true) { @@ -49,10 +52,14 @@ export function normalize(value, isRoot = true) { // Add spaces around operators inside math functions like calc() that do not follow an operator // or '('. value = value.replace(/(calc|min|max|clamp)\(.+\)/g, (match) => { - return match.replace( - /(-?\d*\.?\d(?!\b-.+[,)](?![^+\-/*])\D)(?:%|[a-z]+)?|\))([+\-/*])/g, - '$1 $2 ' - ) + let vars = [] + return match + .replace(/var\((--.+?)[,)]/g, (match, g1) => { + vars.push(g1) + return match.replace(g1, placeholder) + }) + .replace(/(-?\d*\.?\d(?!\b-\d.+[,)](?![^+\-/*])\D)(?:%|[a-z]+)?|\))([+\-/*])/g, '$1 $2 ') + .replace(placeholderRe, () => vars.shift()) }) return value diff --git a/tests/arbitrary-values.test.css b/tests/arbitrary-values.test.css index 6968de7258c5..4450f8b46d77 100644 --- a/tests/arbitrary-values.test.css +++ b/tests/arbitrary-values.test.css @@ -191,6 +191,12 @@ .w-\[\'\}\{\}\'\] { width: '}{}'; } +.min-w-\[calc\(1-var\(--something\)\*0\.5\)\] { + min-width: calc(1 - var(--something) * 0.5); +} +.min-w-\[calc\(1-\(var\(--something\)\*0\.5\)\)\] { + min-width: calc(1 - (var(--something) * 0.5)); +} .min-w-\[3\.23rem\] { min-width: 3.23rem; } diff --git a/tests/arbitrary-values.test.html b/tests/arbitrary-values.test.html index 083c76def893..09ce28c1d348 100644 --- a/tests/arbitrary-values.test.html +++ b/tests/arbitrary-values.test.html @@ -67,6 +67,8 @@
+
+
From c39294c9732eb1977e432713e9c9a18a7caea3e6 Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Wed, 14 Dec 2022 09:03:29 -0500 Subject: [PATCH 2/3] Add test --- tests/arbitrary-values.test.css | 3 +++ tests/arbitrary-values.test.html | 1 + 2 files changed, 4 insertions(+) diff --git a/tests/arbitrary-values.test.css b/tests/arbitrary-values.test.css index 4450f8b46d77..dead24b1c190 100644 --- a/tests/arbitrary-values.test.css +++ b/tests/arbitrary-values.test.css @@ -197,6 +197,9 @@ .min-w-\[calc\(1-\(var\(--something\)\*0\.5\)\)\] { min-width: calc(1 - (var(--something) * 0.5)); } +.min-w-\[calc\(1-\(\(12-3\)\*0\.5\)\)\] { + min-width: calc(1 - ((12 - 3) * 0.5)); +} .min-w-\[3\.23rem\] { min-width: 3.23rem; } diff --git a/tests/arbitrary-values.test.html b/tests/arbitrary-values.test.html index 09ce28c1d348..4d8be18cdca2 100644 --- a/tests/arbitrary-values.test.html +++ b/tests/arbitrary-values.test.html @@ -69,6 +69,7 @@
+
From d6b8c3eefda6d2b536c9443ce8568ba8996af01f Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Wed, 14 Dec 2022 09:04:36 -0500 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ae90ce6d6ee..631d40443600 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Deterministic sorting of arbitrary variants ([#10016](https://github.com/tailwindlabs/tailwindcss/pull/10016)) - Add `data` key to theme types ([#10023](https://github.com/tailwindlabs/tailwindcss/pull/10023)) - Prevent invalid arbitrary variant selectors from failing the build ([#10059](https://github.com/tailwindlabs/tailwindcss/pull/10059)) +- Properly handle subtraction followed by a variable ([#10074](https://github.com/tailwindlabs/tailwindcss/pull/10074)) ### Changed