horojs
Version:
A micro library for a reactive UI application.
3 lines (2 loc) • 2.94 kB
JavaScript
!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 u(t){return t.startsWith(e)}function i(t){return t.startsWith("@")}function c(t){return t.startsWith(o)}function f(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 a(t){return"string"==typeof t||"fragment"in t}function l(t){if(a(t)||"function"==typeof t)return t;throw new Error("The passed value is not Instertable.")}function d(t){if("function"==typeof t)return t;throw new Error("The passed value is not a Subscription.")}function p(t,e){return a(e)?function(t,e){return"string"==typeof e?(t.replaceWith(e),()=>{}):(t.replaceWith(e.fragment),e.unsubscribe)}(t,e):f(t,e)}function h(t,e,n){const r=t=>{n(t)};return t.addEventListener(e,r),()=>t.removeEventListener(e,r)}function m(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 g(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],g=a.textContent;if(u(g)){const t=l(e[g.substring(n)]);f.push(p(a,t))}else if(i(g)){const[t,n]=g.substring(r).split(":"),o=d(e[n]);f.push(h(a.nextElementSibling,t,o))}else if(c(g)){const[t,n]=g.substring(s).split(":"),r=l(e[n]),o=m(a.nextElementSibling,t,r);o&&f.push(o)}}return()=>{f.forEach((t=>t()))}}const b=/(<[a-z\=\"\s\-]* )([a-z\-\@]*)="?$/im;function x(t,e){const n=document.createElement("template");return n.innerHTML=function(t){let e=t[0];for(let n=1;n<t.length;n++){const r=t[n],o=e.match(b);e=null===o?`${e}\x3c!--insert:${n-1}--\x3e${r}`:o[2].startsWith("@")?`${e.replace(b,`\x3c!--$2:${n-1}--\x3e$1`)}${r.replace(/^(["])/m,"")}`:`${e.replace(b,`\x3c!--attrs:$2:${n-1}--\x3e$1`)}${r.replace(/^(["])/m,"")}`}return e}(t),n.content}t.horo=function(t,...e){const n=x(t);return{fragment:n,unsubscribe:g(n,e)}},t.mergeComponents=function(t){const e=document.createDocumentFragment();return e.append(...t.map((t=>t.fragment))),{fragment:e,unsubscribe:()=>t.forEach((t=>t.unsubscribe()))}},t.state=function(t){let e=t;const n=[];return[t=>{t(e),n.push(t)},t=>{e=t,n.forEach((t=>t(e)))}]},Object.defineProperty(t,"__esModule",{value:!0})}));
//# sourceMappingURL=index.umd.js.map