Skip to content

Commit

Permalink
[fix] add logic to select and claim raw html tags in head (sveltejs#6463
Browse files Browse the repository at this point in the history
)
  • Loading branch information
myisaak committed Dec 2, 2021
1 parent 5145987 commit e6aaa79
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/compiler/compile/render_dom/wrappers/Head.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ export default class HeadWrapper extends Wrapper {
let nodes;
if (this.renderer.options.hydratable && this.fragment.nodes.length) {
nodes = block.get_unique_name('head_nodes');
block.chunks.claim.push(b`const ${nodes} = @query_selector_all('[data-svelte="${this.node.id}"]', @_document.head);`);
block.chunks.claim.push(b`
const ${nodes} = [
...@query_selector_all('[data-svelte="${this.node.id}"]', @_document.head),
...@get_hydratable_elements(@_document.head)
];`);
}

this.fragment.render(block, x`@_document.head` as unknown as Identifier, nodes);
Expand Down
21 changes: 21 additions & 0 deletions src/runtime/internal/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,27 @@ export function query_selector_all(selector: string, parent: HTMLElement = docum
return Array.from(parent.querySelectorAll(selector)) as ChildNodeArray;
}

export function get_hydratable_elements(parent: HTMLElement = document.body) {
const elements = Array.from(parent.childNodes);
let cursor = 0;
while (cursor < elements.length) {
const start_index = find_comment(elements, 'HTML_TAG_START', cursor);
const end_index = find_comment(elements, 'HTML_TAG_END', start_index);
if (start_index === end_index) {
elements.splice(cursor, elements.length - cursor);
break;
}

detach(elements[start_index]);
detach(elements[end_index]);

elements.splice(cursor, 1 + start_index - cursor);
cursor += end_index - start_index - 1;
}
console.log(elements);
return elements;
}

export class HtmlTag {
// parent for creating node
e: HTMLElement;
Expand Down

0 comments on commit e6aaa79

Please sign in to comment.