(() => {
"use strict";
const ATTR = "data-vars";
const set = new WeakMap();
const pairs = (text) => {
const found = new Map();
for (const part of text.split(";")) {
const at = part.indexOf(":");
if (at < 0) continue;
const name = part.slice(0, at).trim();
if (!name.startsWith("--")) continue;
found.set(name, part.slice(at + 1).trim());
}
return found;
};
const apply = (el) => {
const next = pairs(el.getAttribute(ATTR) ?? "");
for (const name of set.get(el) ?? []) {
if (!next.has(name)) el.style.removeProperty(name);
}
for (const [name, value] of next) el.style.setProperty(name, value);
set.set(el, [...next.keys()]);
};
const within = (node) => {
if (node.nodeType !== Node.ELEMENT_NODE) return;
if (node.hasAttribute(ATTR)) apply(node);
for (const el of node.querySelectorAll(`[${ATTR}]`)) apply(el);
};
new MutationObserver((records) => {
for (const record of records) {
if (record.type === "attributes") apply(record.target);
else record.addedNodes.forEach(within);
}
}).observe(document.documentElement, {
childList: true,
subtree: true,
attributes: true,
attributeFilter: [ATTR],
});
within(document.documentElement);
})();