Skip to content

Commit 8dea38c

Browse files
committed
[fix] add logic to select and claim raw html tags in head (#6463)
1 parent 5145987 commit 8dea38c

File tree

2 files changed

+25
-1
lines changed
  • src
    • compiler/compile/render_dom/wrappers
    • runtime/internal

2 files changed

+25
-1
lines changed

src/compiler/compile/render_dom/wrappers/Head.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ export default class HeadWrapper extends Wrapper {
3636
let nodes;
3737
if (this.renderer.options.hydratable && this.fragment.nodes.length) {
3838
nodes = block.get_unique_name('head_nodes');
39-
block.chunks.claim.push(b`const ${nodes} = @query_selector_all('[data-svelte="${this.node.id}"]', @_document.head);`);
39+
block.chunks.claim.push(b`
40+
const ${nodes} = [
41+
...@query_selector_all('[data-svelte="${this.node.id}"]', @_document.head),
42+
...@get_hydratable_raw_elements(@_document.head)
43+
];`);
4044
}
4145

4246
this.fragment.render(block, x`@_document.head` as unknown as Identifier, nodes);

src/runtime/internal/dom.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,26 @@ export function query_selector_all(selector: string, parent: HTMLElement = docum
640640
return Array.from(parent.querySelectorAll(selector)) as ChildNodeArray;
641641
}
642642

643+
export function get_hydratable_raw_elements(parent: HTMLElement = document.body) {
644+
const elements = Array.from(parent.childNodes);
645+
let cursor = 0;
646+
while (cursor < elements.length) {
647+
const start_index = find_comment(elements, 'HTML_TAG_START', cursor);
648+
const end_index = find_comment(elements, 'HTML_TAG_END', start_index);
649+
if (start_index === end_index) {
650+
elements.splice(cursor, elements.length - cursor);
651+
break;
652+
}
653+
654+
detach(elements[start_index]);
655+
detach(elements[end_index]);
656+
657+
elements.splice(cursor, 1 + start_index - cursor);
658+
cursor += end_index - start_index - 1;
659+
}
660+
return elements;
661+
}
662+
643663
export class HtmlTag {
644664
// parent for creating node
645665
e: HTMLElement;

0 commit comments

Comments
 (0)