This repository has been archived by the owner on Jan 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplize.min.js
1 lines (1 loc) · 9.81 KB
/
templize.min.js
1
let e,t,r=r=>(e=0,t=r,r=n(),t[e]?s():r||""),s=(r="Bad syntax",s=t[e],a=t.slice(0,e).split("\n"),n=a.pop())=>{throw SyntaxError(`${r} \`${s}\` at ${a.length}:${n.length}`)},a=(r=1,s=e,a)=>{if("number"==typeof r)e+=r;else for(;a=r(t.charCodeAt(e));)e+=a;return t.slice(s,e)},n=(t=0,a,n,l,o,c)=>{for(;(n=r.space())&&(o=((c=i[n])&&c(l,t))??(!l&&r.id()));)l=o;return a&&(n==a?e++:s()),l},l=e=>e>=48&&e<=57||e>=65&&e<=90||e>=97&&e<=122||36==e||95==e||e>=192&&215!=e&&247!=e;r.space=r=>{for(;(r=t.charCodeAt(e))<=32;)e++;return r},r.id=e=>a(l);let i=[],o=(r,s=32,a,n=r.charCodeAt(0),o=r.length,c=i[n],p=r.toUpperCase()!==r)=>i[n]=(n,i,u=e)=>i<s&&(o<2||t.substr(e,o)==r)&&(!p||!l(t.charCodeAt(e+o)))&&(e+=o,a(n,i))||(e=u,c?.(n,i));const c=e=>Array.isArray(e)?p[e[0]](...e.slice(1)):t=>t?.[e],p={},u=(e,t,r=p[e])=>p[e]=(...e)=>t(...e)||r&&r(...e),h=(e,t,r)=>r[0]||r[1]?(t?o(e,t,r[0]):i[e.charCodeAt(0)||1]=r[0],u(e,r[1])):r.length?r.length>1?(((e,t,r)=>{o(e,t,((s,a)=>s&&(a=n(t-!!r))&&[e,s,a]))})(e,Math.abs(t),t<0),u(e,((e,t)=>t&&(e=c(e),t=c(t),e.length||t.length?s=>r(e(s),t(s)):(e=r(e(),t()),()=>e))))):(((e,t,r)=>{o(e,t,(r=>!r&&(r=n(t-1))&&[e,r]))})(e,t),u(e,((e,t)=>!t&&((e=c(e)).length?t=>r(e(t)):(e=r(e()),()=>e))))):(((e,t,r)=>{o(e,t,((r,s)=>r&&((s=n(t))||void 0)&&(r[0]===e&&r[2]?(r.push(s||null),r):[e,r,s])))})(e,t),u(e,((...e)=>(e=e.map(c),t=>r(...e.map((e=>e(t)))))))),f=e=>e?s():["",(e=+a((e=>46===e||e>=48&&e<=57||(69===e||101===e?2:0))))!=e?s():e],d=(e,t,r,s)=>[e,t,[r=>r?["++"===e?"-":"+",[e,r],["",1]]:[e,n(t-1)],s=(e,t)=>"("===e[0]?s(e[1]):"."===e[0]?(t=e[2],e=c(e[1]),s=>r(e(s),t)):"["===e[0]?([,e,t]=e,e=c(e),t=c(t),s=>r(e(s),t(s))):t=>r(t,e)]],b=["",,[,e=>()=>e],'"',,[e=>e?s():["",(a()+a((e=>e-34?1:0))+(a()||s("Bad string"))).slice(1,-1)]],".",,[e=>!e&&f()],...Array(10).fill(0).flatMap(((e,t)=>[""+t,0,[f]])),",",1,(...e)=>e[e.length-1],"||",4,(...e)=>{let t,r=0;for(;!t&&r<e.length;)t=e[r++];return t},"&&",5,(...e)=>{let t=0,r=!0;for(;r&&t<e.length;)r=e[t++];return r},"+",12,(e,t)=>e+t,"-",12,(e,t)=>e-t,"*",13,(e,t)=>e*t,"/",13,(e,t)=>e/t,"%",13,(e,t)=>e%t,"|",6,(e,t)=>e|t,"&",8,(e,t)=>e&t,"^",7,(e,t)=>e^t,"==",9,(e,t)=>e==t,"!=",9,(e,t)=>e!=t,">",10,(e,t)=>e>t,">=",10,(e,t)=>e>=t,"<",10,(e,t)=>e<t,"<=",10,(e,t)=>e<=t,">>",11,(e,t)=>e>>t,">>>",11,(e,t)=>e>>>t,"<<",11,(e,t)=>e<<t,"+",15,e=>+e,"-",15,e=>-e,"!",15,e=>!e,...d("++",15,((e,t)=>++e[t])),...d("--",15,((e,t)=>--e[t])),"[",18,[e=>e&&["[",e,n(0,93)||s()],(e,t)=>t&&(e=c(e),t=c(t),r=>e(r)[t(r)])],".",18,[(e,t)=>e&&(t=n(18))&&[".",e,t],(e,t)=>(e=c(e),t=t[0]?t:t[1],r=>e(r)[t])],"(",18,[e=>!e&&["(",n(0,41)||s()],c],"(",18,[e=>e&&["(",e,n(0,41)||""],(e,t,r,s)=>null!=t&&(s=""==t?()=>[]:","===t[0]?(t=t.slice(1).map(c),e=>t.map((t=>t(e)))):(t=c(t),e=>[t(e)]),"."===e[0]?(r=e[2],e=c(e[1]),t=>e(t)[r](...s(t))):"["===e[0]?(r=c(e[2]),e=c(e[1]),t=>e(t)[r(t)](...s(t))):(e=c(e),t=>e(t)(...s(t))))]];for(;b[2];)h(...b.splice(0,3));Symbol.observable||=Symbol("observable");const m=e=>e&&!!(e[Symbol.observable]||e[Symbol.asyncIterator]||e.call&&e.set||e.subscribe||e.then),g=new FinalizationRegistry((e=>e.call?.()));var v=(e,t,r,s,a,n)=>{return e&&(l=(e[Symbol.observable]?.()||e).subscribe?.(t,r,s),n=l&&(()=>l.unsubscribe?.())||e.set&&e.call?.(a,t)||(e.then?.((e=>(!a&&t(e),s?.())),r)||(async n=>{try{for await(n of e){if(a)return;t(n)}s?.()}catch(e){r?.(e)}})())&&(e=>a=1),g.register(e,n),n);var l};const y=(e,t,r,s)=>(e[t="on"===t.slice(0,2)?t.toLowerCase():t]!==r&&(!(t in e.constructor.prototype)||!(s=Object.getOwnPropertyDescriptor(e.constructor.prototype,t))||s.set)&&(e[t]=r),!1===r||null==r?e.removeAttribute(t):"function"!=typeof r&&e.setAttribute(t,!0===r?"":"number"==typeof r||"string"==typeof r?r:"class"===t&&Array.isArray(r)?r.filter(Boolean).join(" "):"style"===t&&r.constructor===Object?(t=r,r=Object.values(r),Object.keys(t).map(((e,t)=>`${e}: ${r[t]};`)).join(" ")):"")),x=(e,t,r,s=null)=>{let a,n,l,i=0,o=r.length,c=t.length,{remove:p,same:u,insert:h,replace:f}=x;for(;i<o&&i<c&&u(t[i],r[i]);)i++;for(;i<o&&i<c&&u(r[o-1],t[c-1]);)s=r[(--c,--o)];if(i==c)for(;i<o;)h(s,r[i++],e);else{for(a=t[i];i<o;)l=r[i++],n=a?a.nextSibling:s,u(a,l)?a=n:i<o&&u(r[i],n)?(f(a,l,e),a=n):h(a,l,e);for(;!u(a,s);)n=a.nextSibling,p(a,e),a=n}return r};x.same=(e,t)=>e==t,x.replace=(e,t,r)=>r.replaceChild(t,e),x.insert=(e,t,r)=>r.insertBefore(t,e),x.remove=(e,t)=>t.removeChild(e);const A={processCallback(e,t,r){if(r)for(const e of t)if(e.expression in r){const t=r[e.expression];"boolean"==typeof t&&e instanceof N&&"boolean"==typeof e.element[e.attributeName]?e.booleanValue=t:e.value=t}}};class C extends DocumentFragment{#e;#t;constructor(e,t,r=A){super(),this.appendChild(e.content.cloneNode(!0)),this.#e=k(this),this.#t=r,t||={},r.createCallback?.(this,this.#e,t),r.processCallback(this,this.#e,t)}update(e){this.#t.processCallback(this,this.#e,e)}}class w{constructor(e,t){this.setter=e,this.expression=t}toString(){return this.value}}class N extends w{#r="";get attributeName(){return this.setter.attr.name}get attributeNamespace(){return this.setter.attr.namespaceURI}get element(){return this.setter.element}get value(){return this.#r}set value(e){if(this.#r===e)return;this.#r=e;const{attr:t,element:r,parts:s}=this.setter;1===s.length?null==e?r.removeAttributeNS(t.namespaceURI,t.name):r.setAttributeNS(t.namespaceURI,t.name,e):r.setAttributeNS(t.namespaceURI,t.name,s.join(""))}get booleanValue(){return this.setter.element.hasAttributeNS(this.attributeNamespace,this.setter.attr.name)}set booleanValue(e){if(1!==this.setter.parts.length)throw new DOMException("Value is not fully templatized");this.value=e?"":null}}class S extends w{#s=[new Text];get replacementNodes(){return this.#s}get parentNode(){return this.setter.parentNode}get nextSibling(){return this.#s[this.#s.length-1].nextSibling}get previousSibling(){return this.#s[0].previousSibling}get value(){return this.#s.map((e=>e.textContent)).join("")}set value(e){this.replace(e)}replace(...e){(e=e.flat().flatMap((e=>null==e?[new Text]:e.forEach?[...e]:11===e.nodeType?[...e.childNodes]:e.nodeType?[e]:[new Text(e)]))).length||e.push(new Text),this.#s=x(this.#s[0].parentNode,this.#s,e,this.nextSibling)}replaceHTML(e){const t=this.parentNode.cloneNode();t.innerHTML=e,this.replace(t.childNodes)}}class T extends S{directive;constructor(e,t){let r=t.getAttribute("directive")||t.getAttribute("type"),s=t.getAttribute("expression")||t.getAttribute(r)||"";s.startsWith("{{")&&(s=s.trim().slice(2,-2).trim()),super(e,s),this.template=t,this.directive=r}}const k=(e,t=[])=>{let r,s,a,n,l;for(r of e.attributes||[])if(r.value.includes("{{")){for([n,l]of(a={element:e,attr:r,parts:[]},j(r.value)))n?(l=new N(a,l),a.parts.push(l),t.push(l)):a.parts.push(l);r.value=a.parts.join("")}for(s of e.childNodes)if(1!==s.nodeType||s instanceof HTMLTemplateElement){if(1===s.nodeType||s.data.includes("{{")){const r={parentNode:e,parts:[]};if(s.data)for([n,l]of j(s.data))n?(l=new S(r,l),r.parts.push(l),t.push(l)):r.parts.push(new Text(l));else l=new T(r,s),r.parts.push(l),t.push(l);s.replaceWith(...r.parts.flatMap((e=>e.replacementNodes||[e])))}}else k(s,t);return t},j=e=>{let t,r="",s=0,a=M[e],n=0;if(a)return a;for(a=[];t=e[n];n++)"{"===t&&"{"===e[n+1]&&"\\"!==e[n-1]&&e[n+2]&&1==++s?(r&&a.push([0,r]),r="",n++):"}"!==t||"}"!==e[n+1]||"\\"===e[n-1]||--s?r+=t||"":(a.push([1,r.trim()]),r="",n++);return r&&a.push([0,(s>0?"{{":"")+r]),M[e]=a},M={};var O=(e,t,r=U)=>{for(let t in B)B[t].prepare(e);let s,a=k(e),n=t=>r.processCallback(e,a,t);return t||={},r.createCallback?.(e,a,t),r.processCallback(e,a,t),t[Symbol.iterator]=function*(){yield s,yield n,yield a},s=new Proxy(t,{set:(e,r,s)=>(t[r]=s,n(t),1),deleteProperty:(e,r)=>(delete t[r],n(),1)})};let P={n:"\n",r:"\r",t:"\t",b:"\b",f:"\f",v:"\v"},$=r=>(n,l,i="")=>{for(n&&s("Unexpected string"),a();(l=t.charCodeAt(e))-r;)92===l?(a(),l=a(),i+=P[l]||l):i+=a();return a(),["",i]},I=(e,...t)=>t.flatMap((e=>Array.isArray(e)?I(...e):e?[e]:[]));h('"',null,[$(34)]),h("'",null,[$(39)]),h("?",3,[(e,t,r)=>e&&(t=n(2,58))&&["?",e,t,n(3)],(e,t,r)=>(e=c(e),t=c(t),r=c(r),s=>e(s)?t(s):r(s))]),h("??",6,((e,t)=>e??t)),h("?.",18,[e=>e&&["?.",e],e=>(e=c(e),t=>e(t)||(()=>{}))]),h("?.",18,[(e,t)=>e&&!(t=n(18))?.map&&["?.",e,t],(e,t)=>t&&(e=c(e),r=>e(r)?.[t])]),h("null",20,[e=>e?s():["",null]]),h("true",20,[e=>e?s():["",!0]]),h("false",20,[e=>e?s():["",!1]]),h("undefined",20,[e=>e?s():["",void 0]]),h("|",6,((e,t)=>t(e))),h("in",10,[(e,t)=>["in",e,n(10)],(e,t)=>r=>[e,r[t]]]);const R=new WeakMap,U={createCallback(e,t,s){if(R.get(e))return;let a,n,l={},i={},o=(t,r,s)=>{for(let i in t)m(a=t[i])?E.register(a,v(a,(t=>(r[i]=t,n&&this.processCallback(e,l[s||i]))))):a?.constructor===Object?o(a,r[i]={},s||i):r[i]=a};for(const a of t){const t=r(a.expression);I(null,t).map((e=>(l[e]||=[]).push(a))),a.eval=c(t),B[a.directive]?.create(e,a,s)}t.length&&o(s,i),R.set(e,[i]),n=!0},processCallback(e,t,r){let s,a,[n]=R.get(e);for(s of(W(n,r),t))(a=s.eval(n))!==s.value&&(s.replace?s.replace(a):1===s.setter.parts.length?y(s.element,s.attributeName,s.value=a):s.value=a)}},E=new FinalizationRegistry((e=>e?.call?.())),W=(e,t)=>{for(let r in t)r in e&&(t[r]?.constructor==Object?W(e[r],t[r]):!m(t[r])&&(e[r]=t[r]))},B={},L=(e,t)=>B[e]={prepare(t,r=t.querySelectorAll(`[\\:${e}]`),s,a,n){for(t of r)t.replaceWith(s=new Text),(a=document.createElement("template")).content.appendChild(t),a.setAttribute("directive",e),n=t.getAttribute(":"+e),a.setAttribute("expression",n.slice(n.indexOf("{{")+2,n.lastIndexOf("}}"))),t.removeAttribute(":"+e),s.replaceWith(a)},create:t};L("if",((e,t)=>{(t.addCase=(e,r=e.eval)=>e.eval=s=>t.match?"":r(s)?(t.match=e,new C(e.template,s,U)):"")(e.ifPart=t);const r=t.eval;t.eval=e=>(t.match=null,r(e))})),L("else-if",((e,t)=>e.ifPart?.addCase(t))),L("else",((e,t)=>(t.eval=()=>!0,e.ifPart?.addCase(t),e.ifPart=null))),L("each",((e,t)=>{let r=t.eval;t.eval=e=>{let[s,a]=r(e),n=[];for(let r of a)n.push(new C(t.template,{...e,[s]:r},U));return n}}));export{O as default,L as directive,B as directives,W as patch,U as processor,E as registry,R as states};