diff --git a/.changeset/cool-comics-marry.md b/.changeset/cool-comics-marry.md new file mode 100644 index 0000000000..1b9d814596 --- /dev/null +++ b/.changeset/cool-comics-marry.md @@ -0,0 +1,5 @@ +--- +"svelte": patch +--- + +fix missing classes after dynamic expressions in class attribute diff --git a/packages/svelte/src/compiler/phases/3-transform/server/transform-server.js b/packages/svelte/src/compiler/phases/3-transform/server/transform-server.js index 8f35d8897a..8c502449d4 100644 --- a/packages/svelte/src/compiler/phases/3-transform/server/transform-server.js +++ b/packages/svelte/src/compiler/phases/3-transform/server/transform-server.js @@ -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)); } } diff --git a/packages/svelte/tests/server-side-rendering/samples/head-raw-elements-content/_config.js b/packages/svelte/tests/server-side-rendering/samples/head-raw-elements-content/_config.js new file mode 100644 index 0000000000..f47bee71df --- /dev/null +++ b/packages/svelte/tests/server-side-rendering/samples/head-raw-elements-content/_config.js @@ -0,0 +1,3 @@ +import { test } from '../../test'; + +export default test({}); diff --git a/packages/svelte/tests/server-side-rendering/samples/head-raw-elements-content/_expected.html b/packages/svelte/tests/server-side-rendering/samples/head-raw-elements-content/_expected.html new file mode 100644 index 0000000000..25abe04124 --- /dev/null +++ b/packages/svelte/tests/server-side-rendering/samples/head-raw-elements-content/_expected.html @@ -0,0 +1,2 @@ +
bar
+
bar
\ No newline at end of file diff --git a/packages/svelte/tests/server-side-rendering/samples/head-raw-elements-content/main.svelte b/packages/svelte/tests/server-side-rendering/samples/head-raw-elements-content/main.svelte new file mode 100644 index 0000000000..767b1d4beb --- /dev/null +++ b/packages/svelte/tests/server-side-rendering/samples/head-raw-elements-content/main.svelte @@ -0,0 +1,11 @@ + +
bar
+
bar
+ +