fix: missing classes after dynamic expressions in class attributes (#11134)

pull/11133/head
Tan Li Hau 9 months ago committed by GitHub
parent d5776c3ec3
commit 15eb5b5864
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix missing classes after dynamic expressions in class attribute

@ -853,9 +853,7 @@ function serialize_attribute_value(
/** @type {import('estree').Expression[]} */
const expressions = [];
if (attribute_value[0].type !== 'Text') {
quasis.push(b.quasi('', false));
}
quasis.push(b.quasi('', false));
let i = 0;
for (const node of attribute_value) {
@ -866,7 +864,8 @@ function serialize_attribute_value(
// don't trim, space could be important to separate from expression tag
data = data.replace(regex_whitespaces_strict, ' ');
}
quasis.push(b.quasi(data, i === attribute_value.length));
const last = /** @type {import('estree').TemplateElement} */ (quasis.at(-1));
last.value.raw += data;
} else {
expressions.push(
b.call(
@ -874,9 +873,7 @@ function serialize_attribute_value(
/** @type {import('estree').Expression} */ (context.visit(node.expression))
)
);
if (i === attribute_value.length || attribute_value[i]?.type !== 'Text') {
quasis.push(b.quasi('', true));
}
quasis.push(b.quasi('', i + 1 === attribute_value.length));
}
}

@ -0,0 +1,3 @@
import { test } from '../../test';
export default test({});

@ -0,0 +1,2 @@
<div class="bar baz svelte-m782ot">bar</div>
<div class="foo bar baz svelte-m782ot">bar</div>

@ -0,0 +1,11 @@
<script>
const dynamic_value = 'bar';
</script>
<div class="{dynamic_value} baz">bar</div>
<div class="foo {dynamic_value} baz">bar</div>
<style>
.baz {
color: red;
}
</style>
Loading…
Cancel
Save