Optimise repeat processing

This commit is contained in:
cpq
2026-05-02 07:14:40 +01:00
parent c937144b65
commit 0c296a17f8
+9 -6
View File
@@ -102,6 +102,7 @@
try { try {
result = Function("x", `"use strict"; const {${k}} = x; return ${expr};`)(c); result = Function("x", `"use strict"; const {${k}} = x; return ${expr};`)(c);
} catch (err) { } catch (err) {
//console.log('EVAL', expr, context, err);
} }
return result; return result;
} }
@@ -194,19 +195,19 @@
function handle_repeat(el, key, context) { function handle_repeat(el, key, context) {
if (!has(context, key)) { console.error('EVAL', key, context); return; } if (!has(context, key)) { console.error('EVAL', key, context); return; }
if (!el.orig) el.orig = el.children[0]; if (!el.orig) el.orig = el.children[0].cloneNode(true);
const v = get(context, key), v2 = JSON.stringify(v); const v = get(context, key), v2 = JSON.stringify(v);
// if (el.v2 === v2) return; if (el.v2 === v2) return;
const frag = document.createDocumentFragment(); const frag = document.createDocumentFragment();
const it = el.dataset.iterator ?? '__obj'; const it = el.dataset.iterator ?? '__obj';
v.forEach(function (item, index) { v.forEach(function (item, index) {
const child = el.orig.cloneNode(true); const child = el.orig.cloneNode(true);
const ctx = { ...context, [it]: item }; const ctx = { ...context, [it]: item };
process(child, ctx); process(child, ctx, true);
frag.appendChild(child); frag.appendChild(child);
}); });
el.replaceChildren(frag); el.replaceChildren(frag);
// el.v2 = v2; el.v2 = v2;
}; };
function handle_upload(el) { function handle_upload(el) {
@@ -242,8 +243,10 @@
return typeof text == "string" ? text.replace(/\${(.+?)}/g, f) : text; return typeof text == "string" ? text.replace(/\${(.+?)}/g, f) : text;
} }
function process(el, context) { function process(root, context, x) {
el.querySelectorAll("*").forEach(function (el) { const all = root.querySelectorAll("*:not([data-repeat] *)");
const els = root.nodeType === Node.DOCUMENT_NODE ? all : [root, ...all];
els.forEach(function (el) {
if (el.tagName === "SCRIPT" || el.tagName === "STYLE") return; if (el.tagName === "SCRIPT" || el.tagName === "STYLE") return;
for (const node of el.childNodes) { for (const node of el.childNodes) {
if (node.nodeType === Node.TEXT_NODE && if (node.nodeType === Node.TEXT_NODE &&