Skip to content

Commit

Permalink
[fix] do not collapse whitespace-only css vars (sveltejs#7303)
Browse files Browse the repository at this point in the history
Fixes sveltejs#7152, see also sveltejs#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.
  • Loading branch information
geoffrich authored and himanshiLt committed Mar 3, 2022
1 parent 3c0bf3c commit eb76164
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/compiler/compile/css/Stylesheet.ts
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion test/css/samples/css-vars/expected.css
@@ -1 +1 @@
:root{--root-test:20}div.svelte-xyz{--test:10}
:root{--root-test:20}div.svelte-xyz{--test:10}div.svelte-xyz{--foo: ;--bar: !important}
5 changes: 5 additions & 0 deletions test/css/samples/css-vars/input.svelte
Expand Up @@ -7,4 +7,9 @@
div {
--test: 10;
}
div {
--foo: ;
--bar: !important;
}
</style>

0 comments on commit eb76164

Please sign in to comment.