UNPKG

@horojs/core

Version:

A micro library for a reactive UI application.

3 lines (2 loc) 2.91 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).horo={})}(this,(function(t){"use strict";const e="insert:",n=e.length,r="@".length,o="attrs:",s=o.length;function i(t){return t.startsWith(e)}function u(t){return t.startsWith("@")}function c(t){return t.startsWith(o)}const f=/(<[a-z\=\"\s\-]* )([a-z\-\@]*)="?$/im;function a(t){return t.replace(/^(["])/m,"")}function l(t,e){let n=()=>{},r={end:t,start:t};const o=document.createRange(),s=e((t=>{o.setStartBefore(r.start),o.setEndAfter(r.end),"string"==typeof t?r=function(t,e){const n=document.createTextNode(e);return t.deleteContents(),t.insertNode(n),t.selectNode(n),{end:n,start:n}}(o,t):(r=function(t,e){t.deleteContents();const n={start:e.fragment.firstChild,end:e.fragment.lastChild};return t.insertNode(e.fragment),n}(o,t),n(),n=t.unsubscribe)}));return()=>{n(),s?.()}}function d(t){return"string"==typeof t||"fragment"in t}function p(t){if(d(t)||"function"==typeof t)return t;throw new Error("The passed value is not Instertable.")}function m(t){if("function"==typeof t)return t;throw new Error("The passed value is not a Subscription.")}function h(t,e){return d(e)?function(t,e){return"string"==typeof e?(t.replaceWith(e),()=>{}):(t.replaceWith(e.fragment),e.unsubscribe)}(t,e):l(t,e)}function g(t,e,n){const r=t=>{n(t)};return t.addEventListener(e,r),()=>t.removeEventListener(e,r)}function b(t,e,n){const r=document.createAttribute(e);if(t.setAttributeNode(r),"string"==typeof n)return r.value=n,()=>{};return n((t=>{r.value=t}))}function x(t,e){const o=function(t){const e=document.createTreeWalker(t,NodeFilter.SHOW_COMMENT),n=[];for(;e.nextNode();)n.push(e.currentNode);return n}(t),f=[];for(let t=0;t<o.length;t++){const a=o[t],l=a.textContent;if(i(l)){const t=p(e[l.substring(n)]);f.push(h(a,t))}else if(u(l)){const[t,n]=l.substring(r).split(":"),o=m(e[n]);f.push(g(a.nextElementSibling,t,o))}else if(c(l)){const[t,n]=l.substring(s).split(":"),r=p(e[n]),o=b(a.nextElementSibling,t,r);o&&f.push(o)}}return()=>{f.forEach((t=>t()))}}function $(t,e){const n=document.createElement("template");n.innerHTML=t;const r=x(n.content,e);return{fragment:n.content,unsubscribe:r}}t.attrsPrefix=o,t.horo=function(t,...e){return $(function(t){let e=t[0];for(let n=1;n<t.length;n++){const r=t[n],o=e.match(f);e=null===o?`${e}\x3c!--insert:${n-1}--\x3e${r}`:o[2].startsWith("@")?`${e.replace(f,`\x3c!--$2:${n-1}--\x3e$1`)}${a(r)}`:`${e.replace(f,`\x3c!--attrs:$2:${n-1}--\x3e$1`)}${a(r)}`}return e}(t),e)},t.insertionPrefix=e,t.makeComponent=$,t.mergeComponents=function(t){const e=document.createDocumentFragment();return e.append(...t.map((t=>t.fragment))),{fragment:e,unsubscribe:()=>t.forEach((t=>t.unsubscribe()))}},Object.defineProperty(t,"__esModule",{value:!0})})); //# sourceMappingURL=index.umd.js.map