UNPKG

mettle

Version:

A approachable, fast, flexible and lightweight JavaScript library for building user interfaces.

7 lines (6 loc) 8.13 kB
/*! * Mettle.js v1.1.2 * (c) 2021-2025 maomincoding * Released under the MIT License. */ const e=(t,n,o,l)=>{let r;n[0]=0;for(let i=1;i<n.length;i++){const s=n[i++],c=n[i]?(n[0]|=s?1:2,o[n[i++]]):n[++i];3===s?l[0]=c:4===s?l[1]=Object.assign(l[1]||{},c):5===s?(l[1]=l[1]||{})[n[++i]]=c:6===s?l[1][n[++i]]+=c+"":s?(r=t.apply(c,e(t,c,o,["",null])),l.push(r),c[0]?n[0]|=2:(n[i-2]=0,n[i]=r)):l.push(c)}return l},t=new Map,n=function(n){let o=t.get(this);return o||(o=new Map,t.set(this,o)),o=e(this,o.get(n)||(o.set(n,o=function(e){let t,n,o=1,l="",r="",i=[0];const s=e=>{1===o&&(e||(l=l.replace(/^\s*\n\s*|\s*\n\s*$/g,"")))?i.push(0,e,l):3===o&&(e||l)?(i.push(3,e,l),o=2):2===o&&"..."===l&&e?i.push(4,e,0):2===o&&l&&!e?i.push(5,0,!0,l):o>=5&&((l||!e&&5===o)&&(i.push(o,0,l,n),o=6),e&&(i.push(o,e,0,n),o=6)),l=""};for(let c=0;c<e.length;c++){c&&(1===o&&s(),s(c));for(let f=0;f<e[c].length;f++)t=e[c][f],1===o?"<"===t?(s(),i=[i],o=3):l+=t:4===o?"--"===l&&">"===t?(o=1,l=""):l=t+l[0]:r?t===r?r="":l+=t:'"'===t||"'"===t?r=t:">"===t?(s(),o=1):o&&("="===t?(o=5,n=l,l=""):"/"===t&&(o<5||">"===e[c][f+1])?(s(),3===o&&(i=i[0]),o=i,(i=i[0]).push(2,0,o),o=0):" "===t||"\t"===t||"\n"===t||"\r"===t?(s(),o=2):l+=t),3===o&&"!--"===l&&(o=4,i=i[0])}return s(),i}(n)),o),arguments,[]),o.length>1?o:o[0]}.bind((function(e,t,n){let o=null,l=null,r=null;for(l in t)"key"===l&&(o=t[l]);return arguments.length>2&&(r=arguments.length>3?Array.prototype.slice.call(arguments,2):n),{tag:e,props:t,children:r,key:o,el:null}}));function o(e){const t=Object.create(null),n=e.split(",");for(let e=0;e<n.length;e++)t[n[e]]=!0;return function(e){return t[e]}}const l=o("svg,animate,circle,clippath,cursor,image,defs,desc,ellipse,filter,font-face,foreignobject,g,glyph,line,marker,mask,missing-glyph,path,pattern,polygon,polyline,rect,switch,symbol,text,textpath,tspan,use,view,feBlend,feColorMatrix,feComponentTransfer,feComposite,feConvolveMatrix,feDiffuseLighting,feDisplacementMap,feFlood,feGaussianBlur,feImage,feMerge,feMorphology,feOffset,feSpecularLighting,feTile,feTurbulence,feDistantLight,fePointLight,feSpotLight,linearGradient,stop,radialGradient,animateTransform,animateMotion"),r={svg:"http://www.w3.org/2000/svg",math:"http://www.w3.org/1998/Math/MathML"};const i=Object.prototype.hasOwnProperty,s=(e,t)=>i.call(e,t),c=e=>null!==e&&"object"==typeof e,f=e=>null==e,a=(e,t)=>e.tag===t.tag&&e.key===t.key,u=e=>null!=e&&"object"==typeof e&&"tag"in e,p=e=>!u(e)&&!Array.isArray(e);function h(e){console.warn(`[Mettle.js warn]: ${e}`)}function g(e,t){Object.assign(e.style,t)}function d(e,t,n){if("function"!=typeof n)return;const o=t.slice(2).toLowerCase();e.addEventListener(o,n)}function m(e,t,n){if("function"!=typeof n)return;const o=t.slice(2).toLowerCase();e.removeEventListener(o,n)}const y="http://www.w3.org/1999/xlink",b=new Set(["checked","disabled","readonly","selected","multiple","hidden"]);function w(e,t,n){b.has(t)?n?e.setAttribute(t,""):e.removeAttribute(t):t.startsWith("xlink:")?e.setAttributeNS(y,t,n):e.setAttribute(t,n)}function k(e,t){t.startsWith("xlink:")?e.removeAttributeNS(y,t):e.removeAttribute(t)}const v=document.createElement.bind(document),A=document.createDocumentFragment.bind(document),M=document.createComment.bind(document);function j(e){return"fragment"===e?A():"comment"===e||"null"===e?M(""):l(e)?(t=function(e){return l(e)?"svg":"math"===e?"math":void 0}(e),n=e,document.createElementNS(r[t],n)):v(e);var t,n}function S(e,t){const n=e.children,o=t.children;if(Array.isArray(o))for(let e=0;e<o.length;e++){const t=o[e],l=n[e];u(t)&&(t.el=l.el,S(l,t))}else c(o)&&(o.el=n.el)}const C="1.1.2",L=o("$ref,$once,$memo");let O=new WeakMap;const x=new WeakMap;function $(e,t){t.textContent=e}function E(e,t,n){const{tag:o,props:l,children:r}=e;if(!f(o))if("string"==typeof o){const i=j(o);if(e.el=i,!f(l)){const e=Object.keys(l);for(let t=0;t<e.length;t++){const n=e[t],o=l[n],r=c(o);n.startsWith("on")&&d(i,n,o),"function"==typeof o||"key"===n||L(n)||w(i,n,o),"style"===n&&r&&g(i,o),"$ref"===n&&r&&x.set(o,i)}}if(!f(r))if(p(r))i&&$(r,i);else{const e=c(r);if(Array.isArray(r))for(let e=0;e<r.length;e++){const t=r[e];u(t)&&E(t,i)}else e&&E(r,i)}if(n)t.insertBefore(i,n);else{if(!t)return i;t.appendChild(i)}}else if("function"==typeof o){const e={content:o,setData:W.bind(o),props:l},n=o.call(o,e);o.template=n;const r=n();E(r,t),O.set(o,r)}}function T(e,t,n){const o=e.props;if((!o||!s(o,"$once"))&&((e,t)=>"function"!=typeof e.tag&&"function"!=typeof t.tag)(e,t))if(a(e,t)){const l=t.el=e.el,r=e.props||{},i=t.props||{},h={},y=Object.keys(i),b=Object.keys(r),v=new Set(y);for(let e=0;e<y.length;e++)h[y[e]]=!0;for(let e=0;e<b.length;e++)h[b[e]]=!0;const A=Object.keys(h);for(let e=0;e<A.length;e++){const t=A[e],n=i[t],o=r[t];if(n===o)continue;if(f(n)){k(l,t);continue}const s=c(n),a="function"==typeof n,u="style"===t;if(a){n.toString()!==o.toString()&&(m(l,t,o),d(l,t,n));continue}if(u&&s){g(l,n);continue}"key"!==t&&!L(t)&&!a&&w(l,t,n)}for(let e=0;e<b.length;e++){const t=b[e];v.has(t)||k(l,t)}if(null!=o&&s(o,"$memo")){const l=o.$memo;if(l[1]===n&&!l[0])return void(l[2]&&S(e,t))}const M=e.children,j=t.children;Array.isArray(M)&&Array.isArray(j)?function(e,t,n,o){const l=t.length;let r=0,i=e.length-1,s=l-1;for(;r<=i&&r<=s&&a(e[r],t[r]);)T(e[r],t[r],o),r++;for(;r<=i&&r<=s&&a(e[i],t[s]);)T(e[i],t[s],o),i--,s--;if(r>i){if(r<=s){const e=s+1,o=e<l?t[e].el:null;for(;r<=s;)n.insertBefore(E(t[r]),o),r++}}else if(r>s)for(;r<=i;)n.removeChild(e[r].el),r++;else{const c=r,f=r,u=new Map;for(r=f;r<=s;r++){const e=t[r];null!=e.key&&u.set(e.key,r)}let p,h=0;const g=s-f+1;let d=!1,m=0;const y=new Array(g);for(r=0;r<g;r++)y[r]=0;for(let l=c;l<=i;l++){if(h>=g){n.removeChild(e[l].el);continue}let r;if(null!==e[l].key)r=u.get(e[l].key);else for(p=f;p<=s;p++)if(0===y[p-f]&&a(e[l],t[p])){r=p;break}void 0===r?n.removeChild(e[l].el):(y[r-f]=l+1,r>m?m=r:d=!0,T(e[l],t[r],o),h++)}const b=d?function(e){const t=e.slice(),n=[0];let o,l,r,i,s;const c=e.length;for(o=0;o<c;o++){const c=e[o];if(0!==c){if(l=n[n.length-1],e[l]<c){t[o]=l,n.push(o);continue}for(r=0,i=n.length-1;r<i;)s=(r+i)/2|0,e[n[s]]<c?r=s+1:i=s;c<e[n[r]]&&(r>0&&(t[o]=n[r-1]),n[r]=o)}}for(r=n.length,i=n[r-1];r-- >0;)n[r]=i,i=t[i];return n}(y):[];p=b.length-1;for(let e=g-1;e>=0;e--){const o=e+f,r=o+1<l?t[o+1].el:null;0===y[e]?n.insertBefore(E(t[o]),r):d&&(p<0||e!==b[p]?n.insertBefore(t[o].el,r):p--)}}}(M,j,l,n):u(M)&&u(j)?T(M,j,n):p(M)&&p(j)&&M!==j&&$(j,l)}else{const n=e.el.parentNode,o=e.el.nextSibling;n.removeChild(e.el),E(t,n,o)}}async function W(e,t){try{await Promise.resolve();const n=e||this,o=O.get(n),l=n.template();T(o,l,t),O.set(n,l)}catch(e){h(e)}}function D(e,t){const n=e.tag,o={content:n,setData:W.bind(n),props:e.props},l=n.call(n,o);n.template=l;const r=l(),i=function(e){if("string"==typeof e){const t=document.querySelector(e);if(!t){let t=null;return e.startsWith("#")?(t=document.createElement("div"),t.setAttribute("id",e.substring(1,e.length))):e.startsWith(".")?(t=document.createElement("div"),t.setAttribute("class",e.substring(1,e.length))):h(`Failed to mount app: mount target selector "${e}" returned null.`),document.body.insertAdjacentElement("afterbegin",t),t}return t}return e instanceof HTMLElement?e:window.ShadowRoot&&e instanceof window.ShadowRoot&&"closed"===e.mode?(h('mounting on a ShadowRoot with `{mode: "closed"}` may lead to unpredictable bugs.'),null):null}(t);E(r,i),O.set(n,r),q=i,P()}let B=[],N=0,F=0;function G(e=null){null!==e&&("function"==typeof e?B.push(e):h("The parameter of onMounted is not a function!"))}function P(){if(B.length>0)for(let e=0,t=B.length;e<t;e++)B[e]&&B[e]();F=N,N=0,B=[]}let R=[];function H(e=null){null!==e&&("function"==typeof e?(N+=1,R.push(e)):h("The parameter of onUnmounted is not a function!"))}let q=Object.create(null);function I(e){!function(){if(R.length>0){for(let e=0;e<F;e++)R[e]&&R[e]();R.splice(0,F)}F=N}(),q.innerHTML="",O=null,O=new WeakMap;const t={content:e,setData:W.bind(e)},n=e.call(e,t);e.template=n;const o=n();E(o,q),O.set(e,o),P()}export{D as createApp,x as domInfo,n as html,G as onMounted,H as onUnmounted,I as resetView,W as setData,C as version};