Skip to content

Commit 53d5115

Browse files
committed
[fix] add logic to select and claim raw html tags in head (sveltejs#6463)
1 parent a4e4027 commit 53d5115

File tree

2 files changed

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

2 files changed

+26
-1
lines changed

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ 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+
];
44+
`);
4045
}
4146

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

src/runtime/internal/dom.ts

+20
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,26 @@ export function query_selector_all(selector: string, parent: HTMLElement = docum
644644
return Array.from(parent.querySelectorAll(selector)) as ChildNodeArray;
645645
}
646646

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

0 commit comments

Comments
 (0)