From eb76164706af4999d36f06d7fa1b0b9ffbd12f3b Mon Sep 17 00:00:00 2001 From: Geoff Rich <4992896+geoffrich@users.noreply.github.com> Date: Wed, 2 Mar 2022 23:25:52 -0800 Subject: [PATCH] [fix] do not collapse whitespace-only css vars (#7303) Fixes #7152, see also #7288 --foo:; used to be an invalid CSS custom property value, while -foo: ; was valid. By collapsing the whitespace in these declaration values, we were breaking scenarios where an empty custom property was desired. The spec was updated to trim whitespace and treat these values identically, but Chromium browsers still treat --foo; as invalid. This was recently fixed and will be released in Chrome 99, but this would still be a good fix to maintain backwards compatibility. --- src/compiler/compile/css/Stylesheet.ts | 4 ++++ test/css/samples/css-vars/expected.css | 2 +- test/css/samples/css-vars/input.svelte | 5 +++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/compiler/compile/css/Stylesheet.ts b/src/compiler/compile/css/Stylesheet.ts index 8f88f0cbd406..0a3098db6d63 100644 --- a/src/compiler/compile/css/Stylesheet.ts +++ b/src/compiler/compile/css/Stylesheet.ts @@ -145,6 +145,10 @@ class Declaration { ? this.node.value.children[0] : this.node.value; + // Don't minify whitespace in custom properties, since some browsers (Chromium < 99) + // treat --foo: ; and --foo:; differently + if (first.type === 'Raw' && /^\s+$/.test(first.value)) return; + let start = first.start; while (/\s/.test(code.original[start])) start += 1; diff --git a/test/css/samples/css-vars/expected.css b/test/css/samples/css-vars/expected.css index c2bd56375b1f..d9ff79a2a9c8 100644 --- a/test/css/samples/css-vars/expected.css +++ b/test/css/samples/css-vars/expected.css @@ -1 +1 @@ -:root{--root-test:20}div.svelte-xyz{--test:10} \ No newline at end of file +:root{--root-test:20}div.svelte-xyz{--test:10}div.svelte-xyz{--foo: ;--bar: !important} \ No newline at end of file diff --git a/test/css/samples/css-vars/input.svelte b/test/css/samples/css-vars/input.svelte index 7aa48617a48b..4a969428ab6d 100644 --- a/test/css/samples/css-vars/input.svelte +++ b/test/css/samples/css-vars/input.svelte @@ -7,4 +7,9 @@ div { --test: 10; } + + div { + --foo: ; + --bar: !important; + }