Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
let windowObject = globalThis.window;

export function setWindowObject(newWindowObject) {
windowObject = newWindowObject;
}

function renderHtml(string) {
const template = document.createElement("template");
const template = windowObject.document.createElement("template");
template.innerHTML = string;
return document.importNode(template.content, true);
return windowObject.document.importNode(template.content, true);
}

function renderSvg(string) {
const g = document.createElementNS("http://www.w3.org/2000/svg", "g");
const g = windowObject.document.createElementNS("http://www.w3.org/2000/svg", "g");
g.innerHTML = string;
return g;
}

export const html = Object.assign(hypertext(renderHtml, fragment => {
if (fragment.firstChild === null) return null;
if (fragment.firstChild === fragment.lastChild) return fragment.removeChild(fragment.firstChild);
const span = document.createElement("span");
const span = windowObject.document.createElement("span");
span.appendChild(fragment);
return span;
}), {fragment: hypertext(renderHtml, fragment => fragment)});
Expand All @@ -23,7 +29,7 @@ export const svg = Object.assign(hypertext(renderSvg, g => {
if (g.firstChild === g.lastChild) return g.removeChild(g.firstChild);
return g;
}), {fragment: hypertext(renderSvg, g => {
const fragment = document.createDocumentFragment();
const fragment = windowObject.document.createDocumentFragment();
while (g.firstChild) fragment.appendChild(g.firstChild);
return fragment;
})});
Expand Down Expand Up @@ -192,7 +198,7 @@ function hypertext(render, postprocess) {
case STATE_DATA: {
if (value == null) {
// ignore
} else if (value instanceof Node
} else if (value instanceof windowObject.Node
|| (typeof value !== "string" && value[Symbol.iterator])
|| (/(?:^|>)$/.test(strings[j - 1]) && /^(?:<|$)/.test(input))) {
string += "<!--::" + j + "-->";
Expand Down Expand Up @@ -536,7 +542,7 @@ function hypertext(render, postprocess) {

const root = render(string);

const walker = document.createTreeWalker(root, nodeFilter, null, false);
const walker = windowObject.document.createTreeWalker(root, nodeFilter, null, false);
const removeNodes = [];
while (walker.nextNode()) {
const node = walker.currentNode;
Expand Down Expand Up @@ -576,21 +582,21 @@ function hypertext(render, postprocess) {
if (/^::/.test(node.data)) {
const parent = node.parentNode;
const value = arguments[+node.data.slice(2)];
if (value instanceof Node) {
if (value instanceof windowObject.Node) {
parent.insertBefore(value, node);
} else if (typeof value !== "string" && value[Symbol.iterator]) {
if (value instanceof NodeList || value instanceof HTMLCollection) {
if (value instanceof windowObject.NodeList || value instanceof windowObject.HTMLCollection) {
for (let i = value.length - 1, r = node; i >= 0; --i) {
r = parent.insertBefore(value[i], r);
}
} else {
for (const subvalue of value) {
if (subvalue == null) continue;
parent.insertBefore(subvalue instanceof Node ? subvalue : document.createTextNode(subvalue), node);
parent.insertBefore(subvalue instanceof windowObject.Node ? subvalue : windowObject.document.createTextNode(subvalue), node);
}
}
} else {
parent.insertBefore(document.createTextNode(value), node);
parent.insertBefore(windowObject.document.createTextNode(value), node);
}
removeNodes.push(node);
}
Expand Down