{"version":3,"file":"imm_tmpl.mjs","sources":["../code/imm_utils.mjs","../code/imm_dom_core.mjs","../code/imm_tmpl_core.mjs","../code/imm_tmpl.mjs"],"sourcesContent":["export const\n _I_ = x=>x,\n _dash_name = s => s?.replace(/_/g, '-'),\n _prop_name = s => s?.replace(/-/g, '_'),\n _pairs = o => Object.entries(o),\n _is_obj = a => 'object' === typeof a && null !== a,\n _is_iter = a => _is_obj(a) && Symbol.iterator in a,\n _is_attr_dict = a => _is_obj(a)\n && !a.nodeType && !a.toDOM\n && !_is_iter(a)\n\nexport const\n _imm_cp = (tgt, src, key) =>\n Object.assign(tgt, key ? {[key]:src} : src),\n _ce = /* #__PURE__ */ _I_(customElements),\n\n _el_on = (el, ...z) => ( el.addEventListener(...z), el ),\n _el_get = (el,k) => (k=el.getAttribute(k), ''==k || k),\n _el_has = (el,k) => el.hasAttribute(k),\n _el_set = (el,k,v) => (\n (null == v || false === v\n ? el.removeAttribute(k) // false or nullish is semantically removeAttribute()\n : _is_obj(v) == {style: 1}[k] ? _imm_cp(el[k], v) // use object assign for known keys: style; note true==1 but false!=undefined\n : el.setAttribute(k, true === v ? '' : v)\n ), 1)\n\n","import {\n _dash_name, _pairs,\n _is_attr_dict, _is_iter,\n _el_set, _el_on, _imm_cp\n} from './imm_utils.mjs'\nexport {\n _imm_cp\n} from './imm_utils.mjs'\n\n\n// complex expressions to avoid 'if', 'else', and 'return' keywords\nconst\n // .reduce(_imm_aop, el) interpretation of imm attribute dictionary semantics\n _imm_aop = (el,[k,v],k0,attrs) => (\n k0 = k[0]\n , (\n '$' === k0 // children to prepend\n ? (attrs.z ||= []).push(v)\n\n : '=' === k0 // direct property assignment\n ? _imm_cp(el, v, k.split('=')[1])\n\n : '@' === k0 // hook callback\n ? v(el, k)\n\n : v?.call // event handlers\n ? _el_on(el, k, v, v.opt)\n\n : _el_set(el, _dash_name(k), v) // attribute values\n ), el),\n\n // .flatMap(_imm_cf) interpretation of imm child elements and iterables\n _imm_cf = c => (\n // allow opt-in coersion\n c &&= c.toDOM?.(c) || c.valueOf(c),\n\n null == c ? [] // filter nullish\n\n : c.nodeType ? [c] // pass-through nodes\n\n : _is_iter(c) ? _imm_b(c) // recursively reduce arrays and iterables\n\n : ''+c ) // otherwise force toString()\n\n\n\nexport function imm(el, ...args) {\n let len=args.length, attrs=args[0]\n\n if (0 < len && el?.nodeType) {\n if (_is_attr_dict(attrs)) {\n // replace attrs with null in args\n 1 === len ? args = null : args[0] = null\n\n attrs = _pairs(attrs)\n attrs.reduce(_imm_aop, el)\n\n // prepend children found in attrs.z\n attrs.z && el.prepend(... _imm_b(attrs.z))\n }\n\n // append arguments as children\n args && el.append(... _imm_b(args))\n }\n return el\n}\n\n\nexport const\n // clear all inner content (text and html)\n _imm0 = el => (el.textContent = '', el),\n imm_set = (el, ...args) => imm(_imm0(el), ...args),\n\n _imm_b = iterable => [... iterable].flatMap(_imm_cf)\n\n","// _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 = `