UNPKG

@qwik.dev/core

Version:

An open source framework for building instant loading web apps at any scale, without the extra effort.

40 lines (39 loc) 1.33 kB
const BACKPATCH_DATA_SELECTOR = 'script[type="qwik/backpatch"]'; const executorScript = document.currentScript; if (executorScript) { const container = executorScript.closest( "[q\\:container]:not([q\\:container=html]):not([q\\:container=text])" ); if (container) { const script = container.querySelector(BACKPATCH_DATA_SELECTOR); if (script) { const data = JSON.parse(script.textContent || "[]"); const walker = document.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); } } } } }