Skip to content

Commit e6aaa79

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

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

+5-1
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_elements(@_document.head)
43+
];`);
4044
}
4145

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

src/runtime/internal/dom.ts

+21
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,27 @@ 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_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+
console.log(elements);
661+
return elements;
662+
}
663+
643664
export class HtmlTag {
644665
// parent for creating node
645666
e: HTMLElement;

0 commit comments

Comments
 (0)