{"version":3,"file":"imm_tmpl_core.mjs","sources":["../code/imm_tmpl_core.mjs"],"sourcesContent":["// _tkey prefix from valid custom element name of [4.13.3: Core concepts][WHATWG ]\n//\n// [WHATWG]: https://html.spec.whatwg.org/multipage/custom-elements.html#custom-elements-core-concepts\nconst _tkey = 'I\\u2133-'+Date.now(), _tqsel = '['+_tkey+']', _ttype = 'text/x-'+_tkey\n\nexport function imm_tmpl_link(invoke_arg) {\n let wm_cache = new WeakMap()\n return (parts, ...args) => {\n // Return cloned template content with args expanded into keyed nodes\n\n let el = wm_cache.get(parts)\n if (!el)\n wm_cache.set(parts,\n el = _imm_tmpl_c(parts))\n\n el = el.content.cloneNode(true)\n for (let tgt_elem of el.querySelectorAll(_tqsel)) {\n // remove the tag key -- intentionally ugly!\n let idx = + tgt_elem.getAttribute(_tkey)\n tgt_elem.removeAttribute(_tkey)\n\n let a = args[+idx]\n\n // update/replace target node with arg\n invoke_arg(\n tgt_elem, _ttype === tgt_elem.type,\n null == a ? a : a.valueOf(), idx)\n }\n return el\n }\n}\n\nexport function _imm_tmpl_c(parts) {\n // Compile into template element with keyed attributes and nodes\n\n // render all as attributes\n let kinds = Array(parts.length - 1).fill(2)\n let el0 = _imm_tmpl_r(parts, kinds)\n\n // reset mark node as default rendering\n kinds.fill(1)\n\n // mark found attributes\n for (let each of el0.content.querySelectorAll(_tqsel))\n kinds[+each.getAttribute(_tkey)] = 2\n\n // render correctly as node or attribute\n return _imm_tmpl_r(parts, kinds)\n}\n\nexport function _imm_tmpl_r(parts, kinds) {\n // Render into template element with keyed attributes.\n\n // Concatenates `parts` with attribute or node\n // driven by corresponding `kind`\n\n let s='', i=-1\n for (let p of parts) {\n s += p\n\n let pk = kinds[++i] | 0\n if (pk) {\n // as attribute\n let pi = ` ${_tkey}=${i}`\n\n if (1 & pk) // as node\n pi = `