Skip to content

Commit

Permalink
feat: allow use documents other than window.document during render
Browse files Browse the repository at this point in the history
  • Loading branch information
pedro00dk committed May 25, 2023
1 parent 679bf6d commit 461ac1c
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/dom-expressions/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,19 @@ export {
voidFn as HydrationScript
};

const documents = new WeakMap()

export function render(code, element, init, options = {}) {
let disposer;
const owner = { ...options.owner }
owner.root = owner
documents.set(owner, element.ownerDocuments ?? document)
root(dispose => {
disposer = dispose;
element === document
? code()
: insert(element, code(), element.firstChild ? null : undefined, init);
}, options.owner);
}, owner);
return () => {
disposer();
element.textContent = "";
Expand All @@ -67,7 +72,7 @@ export function template(html, isCE, isSVG) {
};
// backwards compatible with older builds
const fn = isCE
? () => untrack(() => document.importNode(node || (node = create()), true))
? () => untrack(() => documents.get(getOwner().root).importNode(node || (node = create()), true))
: () => (node || (node = create())).cloneNode(true);
fn.cloneNode = fn;
return fn;
Expand Down

0 comments on commit 461ac1c

Please sign in to comment.