UNPKG

uhtml

Version:
5 lines (4 loc) • 6.59 kB
var e=e=>function(){const{constructor:t}=e.createDocumentFragment();const{isArray:n}=Array,{getPrototypeOf:r,getOwnPropertyDescriptor:s}=Object,l=[],i=()=>e.createRange(),o=(e,t,n)=>(e.set(t,n),n),a=(e,t)=>t.reduceRight(c,e),c=(e,t)=>e.childNodes[t],{setPrototypeOf:h}=Object;let u;var d=(e,t,n)=>(u||(u=i()),n?u.setStartAfter(e):u.setStartBefore(e),u.setEndAfter(t),u.deleteContents(),e);const f=({firstChild:e,lastChild:t},n)=>d(e,t,n);let p=!1;const g=(e,t)=>p&&11===e.nodeType?1/t<0?t?f(e,!0):e.lastChild:t?e.valueOf():e.firstChild:e,v=t=>e.createComment(t);class b extends((e=>{function t(e){return h(e,new.target.prototype)}return t.prototype=e.prototype,t})(t)){#e=v("<>");#t=v("</>");#n=l;constructor(e){super(e),this.replaceChildren(this.#e,...e.childNodes,this.#t),p=!0}get firstChild(){return this.#e}get lastChild(){return this.#t}get parentNode(){return this.#e.parentNode}remove(){f(this,!1)}replaceWith(e){f(this,!0).replaceWith(e)}valueOf(){const{parentNode:e}=this;if(e===this)this.#n===l&&(this.#n=[...this.childNodes]);else{if(e){let{firstChild:e,lastChild:t}=this;for(this.#n=[e];e!==t;)this.#n.push(e=e.nextSibling)}this.replaceChildren(...this.#n)}return this}}const m=(e,t,n)=>e.setAttribute(t,n),C=(e,t)=>e.removeAttribute(t);let w;const x=(e,t,r)=>{r=r.slice(1),w||(w=new WeakMap);const s=w.get(e)||o(w,e,{});let l=s[r];return l&&l[0]&&e.removeEventListener(r,...l),l=n(t)?t:[t,!1],s[r]=l,l[0]&&e.addEventListener(r,...l),t},$=(e,t)=>{const{t:n,n:r}=e;let s=!1;switch(typeof t){case"object":if(null!==t){(r||n).replaceWith(e.n=t.valueOf());break}case"undefined":s=!0;default:n.data=s?"":t,r&&(e.n=null,r.replaceWith(n))}return t},y=(e,t,n)=>e[n]=t,N=(e,t,n)=>y(e,t,n.slice(1)),O=(e,t,n)=>null==t?(C(e,n),t):y(e,t,n),k=(e,t)=>("function"==typeof t?t(e):t.current=e,t),A=(e,t,n)=>(null==t?C(e,n):m(e,n,t),t),M=(e,t,n)=>(e.toggleAttribute(n.slice(1),t),t),W=(e,t,n)=>{const{length:r}=t;if(e.data=`[${r}]`,r)return((e,t,n,r,s)=>{const l=n.length;let i=t.length,o=l,a=0,c=0,h=null;for(;a<i||c<o;)if(i===a){const t=o<l?c?r(n[c-1],-0).nextSibling:r(n[o],0):s;for(;c<o;)e.insertBefore(r(n[c++],1),t)}else if(o===c)for(;a<i;)h&&h.has(t[a])||e.removeChild(r(t[a],-1)),a++;else if(t[a]===n[c])a++,c++;else if(t[i-1]===n[o-1])i--,o--;else if(t[a]===n[o-1]&&n[c]===t[i-1]){const s=r(t[--i],-0).nextSibling;e.insertBefore(r(n[c++],1),r(t[a++],-0).nextSibling),e.insertBefore(r(n[--o],1),s),t[i]=n[o]}else{if(!h){h=new Map;let e=c;for(;e<o;)h.set(n[e],e++)}if(h.has(t[a])){const s=h.get(t[a]);if(c<s&&s<o){let l=a,u=1;for(;++l<i&&l<o&&h.get(t[l])===s+u;)u++;if(u>s-c){const l=r(t[a],0);for(;c<s;)e.insertBefore(r(n[c++],1),l)}else e.replaceChild(r(n[c++],1),r(t[a++],-1))}else a++}else e.removeChild(r(t[a++],-1))}return n})(e.parentNode,n,t,g,e);switch(n.length){case 1:n[0].remove();case 0:break;default:d(g(n[0],0),g(n.at(-1),-0),!1)}return l},S=new Map([["aria",(e,t)=>{for(const n in t){const r=t[n],s="role"===n?n:`aria-${n}`;null==r?C(e,s):m(e,s,r)}return t}],["class",(e,t)=>O(e,t,null==t?"class":"className")],["data",(e,t)=>{const{dataset:n}=e;for(const e in t)null==t[e]?delete n[e]:n[e]=t[e];return t}],["ref",k],["style",(e,t)=>null==t?O(e,t,"style"):y(e.style,t,"cssText")]]),E=(e,t,n)=>{switch(t[0]){case".":return N;case"?":return M;case"@":return x;default:return n||"ownerSVGElement"in e?"ref"===t?k:A:S.get(t)||(t in e?t.startsWith("on")?y:((e,t)=>{let n;do{n=s(e,t)}while(!n&&(e=r(e)));return n})(e,t)?.set?O:A:A)}},T=(e,t)=>(e.textContent=null==t?"":t,t),B=(e,t,n)=>({a:e,b:t,c:n}),D=()=>B(null,null,l);var j=t=>(n,r)=>{const{a:s,b:i,c:o}=t(n,r),c=e.importNode(s,!0);let h=l;if(i!==l){h=[];for(let e,t,n=0;n<i.length;n++){const{a:r,b:s,c:o}=i[n],g=r===t?e:e=a(c,t=r);h[n]=(u=s,d=g,f=o,p=s===W?[]:s===$?D():null,{v:l,u:u,t:d,n:f,c:p})}}var u,d,f,p;return((e,t)=>({b:e,c:t}))(o?c.firstChild:new b(c),h)};const F=/^(?:plaintext|script|style|textarea|title|xmp)$/i,L=/^(?:area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)$/i,P=/<([a-zA-Z0-9]+[a-zA-Z0-9:._-]*)([^>]*?)(\/?)>/g,z=/([^\s\\>"'=]+)\s*=\s*(['"]?)\x01/g,H=/[\x01\x02]/g;let R,Z,G=e.createElement("template");var V=(t,n)=>{if(n)return R||(R=e.createElementNS("http://www.w3.org/2000/svg","svg"),Z=i(),Z.selectNodeContents(R)),Z.createContextualFragment(t);G.innerHTML=t;const{content:r}=G;return G=G.cloneNode(!1),r};const _=e=>{const t=[];let n;for(;n=e.parentNode;)t.push(t.indexOf.call(n.childNodes,e)),e=n;return t},q=()=>e.createTextNode(""),I=(t,r,s)=>{const i=V(((e,t,n)=>{let r=0;return e.join("").trim().replace(P,((e,t,r,s)=>`<${t}${r.replace(z,"=$2$1").trimEnd()}${s?n||L.test(t)?" /":`></${t}`:""}>`)).replace(H,(e=>""===e?`\x3c!--${t+r++}--\x3e`:t+r++))})(t,K,s),s),{length:a}=t;let c=l;if(a>1){const t=[],l=e.createTreeWalker(i,129);let o=0,h=`${K}${o++}`;for(c=[];o<a;){const e=l.nextNode();if(8===e.nodeType){if(e.data===h){const s=n(r[o-1])?W:$;s===$&&t.push(e),c.push(B(_(e),s,null)),h=`${K}${o++}`}}else{let t;for(;e.hasAttribute(h);){t||(t=_(e));const n=e.getAttribute(h);c.push(B(t,E(e,n,s),n)),C(e,h),h=`${K}${o++}`}!s&&F.test(e.localName)&&e.textContent.trim()===`\x3c!--${h}--\x3e`&&(c.push(B(t||_(e),T,null)),h=`${K}${o++}`)}}for(o=0;o<t.length;o++)t[o].replaceWith(q())}const{childNodes:h}=i;let{length:u}=h;return u<1?(u=1,i.appendChild(q())):1===u&&1!==a&&1!==h[0].nodeType&&(u=0),o(J,t,B(i,c,1===u))},J=new WeakMap,K="isµ";var Q=e=>(t,n)=>J.get(t)||I(t,n,e);const U=j(Q(!1)),X=j(Q(!0)),Y=(e,{s:t,t:n,v:r})=>{if(e.a!==n){const{b:s,c:l}=(t?X:U)(n,r);e.a=n,e.b=s,e.c=l}for(let{c:t}=e,n=0;n<t.length;n++){const e=r[n],s=t[n];switch(s.u){case W:s.v=W(s.t,ee(s.c,e),s.v);break;case $:const t=e instanceof te?Y(s.c||(s.c=D()),e):(s.c=null,e);t!==s.v&&(s.v=$(s,t));break;default:e!==s.v&&(s.v=s.u(s.t,e,s.n,s.v))}}return e.b},ee=(e,t)=>{let n=0,{length:r}=t;for(r<e.length&&e.splice(r);n<r;n++){const r=t[n];r instanceof te?t[n]=Y(e[n]||(e[n]=D()),r):e[n]=null}return t};class te{constructor(e,t,n){this.s=e,this.t=t,this.v=n}toDOM(e=D()){return Y(e,this)}} /*! (c) Andrea Giammarchi - MIT */const ne=e=>(t,...n)=>new te(e,t,n),re=ne(!1),se=ne(!0),le=new WeakMap; /*! (c) Andrea Giammarchi - MIT */ const ie=new WeakMap,oe=e=>(t,n)=>{const r=ie.get(t)||o(ie,t,new Map);return r.get(n)||o(r,n,function(t,...n){return new te(e,t,n).toDOM(this)}.bind(D()))},ae=oe(!1),ce=oe(!0);return{Hole:te,attr:S,html:re,htmlFor:ae,render:(e,t)=>((e,t)=>{const n=le.get(e)||o(le,e,D()),{b:r}=n,s="function"==typeof t?t():t,l=s instanceof te?s.toDOM(n):s;return r!==l&&e.replaceChildren((n.b=l).valueOf()),e})(e,t),svg:se,svgFor:ce}}();export{e as default};