diff --git a/src/compile/render-dom/Block.ts b/src/compile/render-dom/Block.ts index e40b3ceb55f3..8baf6f18d477 100644 --- a/src/compile/render-dom/Block.ts +++ b/src/compile/render-dom/Block.ts @@ -164,7 +164,7 @@ export default class Block { if (parent_node) { this.builders.mount.add_line(`@append(${parent_node}, ${name});`); - if (parent_node === 'document.head') this.builders.destroy.add_line(`@detach(${name});`); + if (parent_node === 'document.head' && !no_detach) this.builders.destroy.add_line(`@detach(${name});`); } else { this.builders.mount.add_line(`@insert(#target, ${name}, anchor);`); if (!no_detach) this.builders.destroy.add_conditional('detaching', `@detach(${name});`); diff --git a/src/compile/render-dom/wrappers/RawMustacheTag.ts b/src/compile/render-dom/wrappers/RawMustacheTag.ts index eb71e16de9b5..8111e821a20b 100644 --- a/src/compile/render-dom/wrappers/RawMustacheTag.ts +++ b/src/compile/render-dom/wrappers/RawMustacheTag.ts @@ -21,9 +21,18 @@ export default class RawMustacheTagWrapper extends Tag { render(block: Block, parent_node: string, parent_nodes: string) { const name = this.var; + const in_head = parent_node === 'document.head'; + const needs_anchors = !parent_node || in_head; + + // if in head always needs anchors + if (in_head) { + this.prev = null; + this.next = null; + } + // TODO use is_dom_node instead of type === 'Element'? - const needs_anchor_before = this.prev ? this.prev.node.type !== 'Element' : !parent_node; - const needs_anchor_after = this.next ? this.next.node.type !== 'Element' : !parent_node; + const needs_anchor_before = this.prev ? this.prev.node.type !== 'Element' : needs_anchors; + const needs_anchor_after = this.next ? this.next.node.type !== 'Element' : needs_anchors; const anchor_before = needs_anchor_before ? block.get_unique_name(`${name}_before`) @@ -89,7 +98,7 @@ export default class RawMustacheTagWrapper extends Tag { block.builders.mount.add_line(insert(init)); - if (!parent_node) { + if (needs_anchors) { block.builders.destroy.add_conditional('detaching', needs_anchor_before ? `${detach}\n@detach(${anchor_before});` : detach); @@ -100,4 +109,4 @@ export default class RawMustacheTagWrapper extends Tag { add_anchor_after(); } } -} \ No newline at end of file +}