@qwik.dev/core
Version:
An open source framework for building instant loading web apps at any scale, without the extra effort.
47 lines (46 loc) • 1.62 kB
JavaScript
const BACKPATCH_DATA_SELECTOR = 'script[type="qwik/backpatch"]';
function executeBackpatch(doc, containerElement) {
const container = containerElement || doc.querySelector("[q\\:container]:not([q\\:container=html]):not([q\\:container=text])");
if (container) {
const scripts = container.querySelectorAll(BACKPATCH_DATA_SELECTOR);
const script = scripts[scripts.length - 1];
if (script) {
const data = JSON.parse(script.textContent || "[]");
const walker = doc.createTreeWalker(container, NodeFilter.SHOW_ELEMENT);
let currentNode = walker.currentNode;
let currentNodeIdx = currentNode.hasAttribute(":") ? 0 : -1;
for (let i = 0; i < data.length; i += 3) {
const elementIdx = data[i];
const attrName = data[i + 1];
let value = data[i + 2];
while (currentNodeIdx < elementIdx) {
currentNode = walker.nextNode();
if (!currentNode) {
break;
}
if (currentNode.hasAttribute(":")) {
currentNodeIdx++;
}
}
const element = currentNode;
if (value == null || value === false) {
element.removeAttribute(attrName);
} else {
if (typeof value === "boolean") {
value = "";
}
element.setAttribute(attrName, value);
}
}
}
}
}
const executorScript = document.currentScript;
if (executorScript) {
const container = executorScript.closest(
"[q\\:container]:not([q\\:container=html]):not([q\\:container=text])"
);
if (container) {
executeBackpatch(document, container);
}
}