diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ebb458476..9650d9e0d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* Fix placement of `{@html}` when used at the root of a slot or the root of a component ([#5012](https://github.com/sveltejs/svelte/issues/5012)) * Fix handling of `import`ed value that is used as a store and is also mutated ([#5019](https://github.com/sveltejs/svelte/issues/5019)) * Do not display `a11y-missing-content` warning on elements with `contenteditable` bindings ([#5020](https://github.com/sveltejs/svelte/issues/5020)) * Fix handling of `this` in inline function expressions in the template ([#5033](https://github.com/sveltejs/svelte/issues/5033)) diff --git a/src/compiler/compile/render_dom/wrappers/RawMustacheTag.ts b/src/compiler/compile/render_dom/wrappers/RawMustacheTag.ts index 1e6213cf02..a5367b207d 100644 --- a/src/compiler/compile/render_dom/wrappers/RawMustacheTag.ts +++ b/src/compiler/compile/render_dom/wrappers/RawMustacheTag.ts @@ -39,7 +39,7 @@ export default class RawMustacheTagWrapper extends Tag { } else { - const needs_anchor = in_head || (this.next && !this.next.is_dom_node()); + const needs_anchor = in_head || (this.next ? !this.next.is_dom_node() : (!this.parent || !this.parent.is_dom_node())); const html_tag = block.get_unique_name('html_tag'); const html_anchor = needs_anchor && block.get_unique_name('html_anchor'); diff --git a/test/runtime/samples/raw-mustache-as-root/RawMustache.svelte b/test/runtime/samples/raw-mustache-as-root/RawMustache.svelte new file mode 100644 index 0000000000..d94954f49b --- /dev/null +++ b/test/runtime/samples/raw-mustache-as-root/RawMustache.svelte @@ -0,0 +1,5 @@ + + +{@html content} \ No newline at end of file diff --git a/test/runtime/samples/raw-mustache-as-root/_config.js b/test/runtime/samples/raw-mustache-as-root/_config.js new file mode 100644 index 0000000000..c87971a7f0 --- /dev/null +++ b/test/runtime/samples/raw-mustache-as-root/_config.js @@ -0,0 +1,33 @@ +export default { + html: ` + +

Another first line

+

This line should be last.

+ `, + async test({ assert, target, window }) { + const btn = target.querySelector("button"); + const clickEvent = new window.MouseEvent("click"); + + await btn.dispatchEvent(clickEvent); + + assert.htmlEqual( + target.innerHTML, + ` + +

First line

+

This line should be last.

+ ` + ); + + await btn.dispatchEvent(clickEvent); + + assert.htmlEqual( + target.innerHTML, + ` + +

Another first line

+

This line should be last.

+ ` + ); + }, +}; diff --git a/test/runtime/samples/raw-mustache-as-root/main.svelte b/test/runtime/samples/raw-mustache-as-root/main.svelte new file mode 100644 index 0000000000..7ccce0cd9b --- /dev/null +++ b/test/runtime/samples/raw-mustache-as-root/main.svelte @@ -0,0 +1,17 @@ + + + + + + +

This line should be last.

\ No newline at end of file diff --git a/test/runtime/samples/raw-mustache-before-element/Component.svelte b/test/runtime/samples/raw-mustache-before-element/Component.svelte new file mode 100644 index 0000000000..fcabccae48 --- /dev/null +++ b/test/runtime/samples/raw-mustache-before-element/Component.svelte @@ -0,0 +1,2 @@ + +

This line should be last.

\ No newline at end of file diff --git a/test/runtime/samples/raw-mustache-before-element/_config.js b/test/runtime/samples/raw-mustache-before-element/_config.js index 61288cbb52..c87971a7f0 100644 --- a/test/runtime/samples/raw-mustache-before-element/_config.js +++ b/test/runtime/samples/raw-mustache-before-element/_config.js @@ -1,3 +1,33 @@ export default { - html: `

xbaz

` + html: ` + +

Another first line

+

This line should be last.

+ `, + async test({ assert, target, window }) { + const btn = target.querySelector("button"); + const clickEvent = new window.MouseEvent("click"); + + await btn.dispatchEvent(clickEvent); + + assert.htmlEqual( + target.innerHTML, + ` + +

First line

+

This line should be last.

+ ` + ); + + await btn.dispatchEvent(clickEvent); + + assert.htmlEqual( + target.innerHTML, + ` + +

Another first line

+

This line should be last.

+ ` + ); + }, }; diff --git a/test/runtime/samples/raw-mustache-before-element/main.svelte b/test/runtime/samples/raw-mustache-before-element/main.svelte index 69c1d0107d..a022a68e95 100644 --- a/test/runtime/samples/raw-mustache-before-element/main.svelte +++ b/test/runtime/samples/raw-mustache-before-element/main.svelte @@ -1 +1,17 @@ -

{@html 'x'}baz

\ No newline at end of file + + + + + + {@html content} + \ No newline at end of file diff --git a/test/runtime/samples/raw-mustache-inside-slot/_config.js b/test/runtime/samples/raw-mustache-inside-slot/_config.js new file mode 100644 index 0000000000..61288cbb52 --- /dev/null +++ b/test/runtime/samples/raw-mustache-inside-slot/_config.js @@ -0,0 +1,3 @@ +export default { + html: `

xbaz

` +}; diff --git a/test/runtime/samples/raw-mustache-inside-slot/main.svelte b/test/runtime/samples/raw-mustache-inside-slot/main.svelte new file mode 100644 index 0000000000..69c1d0107d --- /dev/null +++ b/test/runtime/samples/raw-mustache-inside-slot/main.svelte @@ -0,0 +1 @@ +

{@html 'x'}baz

\ No newline at end of file