uhtml
Version:
A minimalistic library to create fast and reactive Web pages
2 lines (1 loc) • 15.9 kB
JavaScript
var e;!function(e){e[e.None=0]="None",e[e.Mutable=1]="Mutable",e[e.Watching=2]="Watching",e[e.RecursedCheck=4]="RecursedCheck",e[e.Recursed=8]="Recursed",e[e.Dirty=16]="Dirty",e[e.Pending=32]="Pending"}(e||(e={}));const t=[],n=[],{link:s,unlink:i,propagate:r,checkDirty:o,endTracking:l,startTracking:a,shallowPropagate:c}=function({update:e,notify:t,unwatched:n}){let s=0;return{link:function(e,t){const n=t.depsTail;if(void 0!==n&&n.dep===e)return;let i;if(4&t.flags&&(i=void 0!==n?n.nextDep:t.deps,void 0!==i&&i.dep===e))return i.version=s,void(t.depsTail=i);const r=e.subsTail;if(void 0!==r&&r.version===s&&r.sub===t)return;const o=t.depsTail=e.subsTail={version:s,dep:e,sub:t,prevDep:n,nextDep:i,prevSub:r,nextSub:void 0};void 0!==i&&(i.prevDep=o);void 0!==n?n.nextDep=o:t.deps=o;void 0!==r?r.nextSub=o:e.subs=o},unlink:i,propagate:function(e){let n,s=e.nextSub;e:for(;;){const i=e.sub;let r=i.flags;if(3&r&&(60&r?12&r?4&r?48&r||!o(e,i)?r=0:(i.flags=40|r,r&=1):i.flags=-9&r|32:r=0:i.flags=32|r,2&r&&t(i),1&r)){const t=i.subs;if(void 0!==t){e=t,void 0!==t.nextSub&&(n={value:s,prev:n},s=e.nextSub);continue}}if(void 0===(e=s)){for(;void 0!==n;)if(e=n.value,n=n.prev,void 0!==e){s=e.nextSub;continue e}break}s=e.nextSub}},checkDirty:function(t,n){let s,i=0;e:for(;;){const o=t.dep,l=o.flags;let a=!1;if(16&n.flags)a=!0;else if(17&~l){if(!(33&~l)){void 0===t.nextSub&&void 0===t.prevSub||(s={value:t,prev:s}),t=o.deps,n=o,++i;continue}}else if(e(o)){const e=o.subs;void 0!==e.nextSub&&r(e),a=!0}if(a||void 0===t.nextDep){for(;i;){--i;const o=n.subs,l=void 0!==o.nextSub;if(l?(t=s.value,s=s.prev):t=o,a){if(e(n)){l&&r(o),n=t.sub;continue}}else n.flags&=-33;if(n=t.sub,void 0!==t.nextDep){t=t.nextDep;continue e}a=!1}return a}t=t.nextDep}},endTracking:function(e){const t=e.depsTail;let n=void 0!==t?t.nextDep:e.deps;for(;void 0!==n;)n=i(n,e);e.flags&=-5},startTracking:function(e){++s,e.depsTail=void 0,e.flags=-57&e.flags|4},shallowPropagate:r};function i(e,t=e.sub){const s=e.dep,i=e.prevDep,r=e.nextDep,o=e.nextSub,l=e.prevSub;return void 0!==r?r.prevDep=i:t.depsTail=i,void 0!==i?i.nextDep=r:t.deps=r,void 0!==o?o.prevSub=l:s.subsTail=l,void 0!==l?l.nextSub=o:void 0===(s.subs=o)&&n(s),r}function r(e){do{const n=e.sub,s=e.nextSub,i=n.flags;32==(48&i)&&(n.flags=16|i,2&i&&t(n)),e=s}while(void 0!==e)}function o(e,t){const n=t.depsTail;if(void 0!==n){let s=t.deps;do{if(s===e)return!0;if(s===n)break;s=s.nextDep}while(void 0!==s)}return!1}}({update:e=>"getter"in e?y(e):w(e,e.value),notify:function e(t){const s=t.flags;if(!(64&s)){t.flags=64|s;const i=t.subs;void 0!==i?e(i.sub):n[h++]=t}},unwatched(e){if("getter"in e){let t=e.deps;if(void 0!==t){e.flags=17;do{t=i(t,e)}while(void 0!==t)}}else"previousValue"in e||D.call(e)}});let u,d,f=0,p=0,h=0;function v(e){const t=u;return u=e,t}function g(e){const t=d;return d=e,t}function b(e){return T.bind({previousValue:e,value:e,subs:void 0,subsTail:void 0,flags:1})}function m(e){return C.bind({value:void 0,subs:void 0,subsTail:void 0,deps:void 0,depsTail:void 0,flags:17,getter:e})}function x(e){const t={fn:e,subs:void 0,subsTail:void 0,deps:void 0,depsTail:void 0,flags:2};void 0!==u?s(t,u):void 0!==d&&s(t,d);const n=v(t);try{t.fn()}finally{v(n)}return D.bind(t)}function y(e){const t=v(e);a(e);try{const t=e.value;return t!==(e.value=e.getter(t))}finally{v(t),l(e)}}function w(e,t){return e.flags=1,e.previousValue!==(e.previousValue=t)}function S(e,t){if(16&t||32&t&&o(e.deps,e)){const t=v(e);a(e);try{e.fn()}finally{v(t),l(e)}return}32&t&&(e.flags=-33&t);let n=e.deps;for(;void 0!==n;){const e=n.dep,t=e.flags;64&t&&S(e,e.flags=-65&t),n=n.nextDep}}function k(){for(;p<h;){const e=n[p];n[p++]=void 0,S(e,e.flags&=-65)}p=0,h=0}function C(){const e=this.flags;if(16&e||32&e&&o(this.deps,this)){if(y(this)){const e=this.subs;void 0!==e&&c(e)}}else 32&e&&(this.flags=-33&e);return void 0!==u?s(this,u):void 0!==d&&s(this,d),this.value}function T(...e){if(!e.length){const e=this.value;if(16&this.flags&&w(this,e)){const e=this.subs;void 0!==e&&c(e)}return void 0!==u&&s(this,u),e}{const t=e[0];if(this.value!==(this.value=t)){this.flags=17;const e=this.subs;void 0!==e&&(r(e),f||k())}}}function D(){let e=this.deps;for(;void 0!==e;)e=i(e,this);const t=this.subs;void 0!==t&&i(t),this.flags=0}const O={greedy:!1},N=e=>new A(e),$=e=>{t.push(v(void 0));try{return e()}finally{v(t.pop())}};class W{constructor(e,t){this._=e(t)}get value(){return this._()}set value(e){this._(e)}peek(){return $(this._)}valueOf(){return this.value}}class A extends W{constructor(e){super(m,e)}get value(){return this._()}set value(e){throw new Error("Computed values are read-only")}}class E extends W{constructor(e){super(b,[e])}get value(){return super.value[0]}set value(e){super.value=[e]}peek(){return super.peek()[0]}}const M=e=>{++f;try{return e()}finally{--f||k()}};let R=(e,{greedy:t=!1}=O)=>t?new E(e):new W(b,e);function L(){return R.apply(null,arguments)}const _=e=>{R=e},{isArray:j}=Array,{assign:F,defineProperties:P,entries:B,freeze:J}=Object;class V{#e;constructor(e){this.#e=e}valueOf(){return this.#e}toString(){return String(this.#e)}}const z=e=>new V(e),H=e=>document.createComment(e),q=42,G=new Set(["plaintext","script","style","textarea","title","xmp"]),I=new Set(["area","base","br","col","embed","hr","img","input","keygen","link","menuitem","meta","param","source","track","wbr"]),K=J({}),Q=J([]),U=(e,t)=>(e.children===Q&&(e.children=[]),e.children.push(t),t.parent=e,t),X=(e,t,n)=>{e.props===K&&(e.props={}),e.props[t]=n},Y=(e,t,n)=>{e!==t&&n.push(e)};class Z{constructor(e){this.type=e,this.parent=null}toJSON(){return[this.type,this.data]}}class ee extends Z{constructor(e){super(8),this.data=e}toString(){return`\x3c!--${this.data}--\x3e`}}class te extends Z{constructor(e){super(10),this.data=e}toString(){return`<!${this.data}>`}}class ne extends Z{constructor(e){super(3),this.data=e}toString(){return this.data}}class se extends Z{constructor(){super(q),this.name="template",this.props=K,this.children=Q}toJSON(){const e=[q];return Y(this.props,K,e),Y(this.children,Q,e),e}toString(){let e="";for(const t in this.props){const n=this.props[t];null!=n&&("boolean"==typeof n?n&&(e+=` ${t}`):e+=` ${t}="${n}"`)}return`<template${e}>${this.children.join("")}</template>`}}class ie extends Z{constructor(e,t=!1){super(1),this.name=e,this.xml=t,this.props=K,this.children=Q}toJSON(){const e=[1,this.name,+this.xml];return Y(this.props,K,e),Y(this.children,Q,e),e}toString(){const{xml:e,name:t,props:n,children:s}=this,{length:i}=s;let r=`<${t}`;for(const t in n){const s=n[t];null!=s&&("boolean"==typeof s?s&&(r+=e?` ${t}=""`:` ${t}`):r+=` ${t}="${s}"`)}if(i){r+=">";for(let n=!e&&G.has(t),o=0;o<i;o++)r+=n?s[o].data:s[o];r+=`</${t}>`}else r+=e?" />":I.has(t)?">":`></${t}>`;return r}}class re extends Z{constructor(){super(11),this.name="#fragment",this.children=Q}toJSON(){const e=[11];return Y(this.children,Q,e),e}toString(){return this.children.join("")}}const oe="\0",le=`"${oe}"`,ae=`'${oe}'`,ce=/\x00|<[^><\s]+/g,ue=/([^\s/>=]+)(?:=(\x00|(?:(['"])[\s\S]*?\3)))?/g,de=(e,t,n,s,i)=>[t,n,s],fe=e=>{const t=[];for(;e.parent;){switch(e.type){case q:case 1:"template"===e.name&&t.push(-1)}t.push(e.parent.children.indexOf(e)),e=e.parent}return t},pe=(e,t)=>{do{e=e.parent}while(t.has(e));return e};const he=(e,t)=>t<0?e.content:e.childNodes[t];var ve=(e,t)=>t.reduceRight(he,e);let ge,be=!1;const me=({firstChild:e,lastChild:t})=>{const n=ge||(ge=document.createRange());return n.setStartAfter(e),n.setEndAfter(t),n.deleteContents(),e},xe=(e,t)=>be&&11===e.nodeType?1/t<0?t?me(e):e.lastChild:t?e.valueOf():e.firstChild:e,ye=Symbol("nodes"),we={get(){return this.firstChild.parentNode}},Se={value(e){me(this).replaceWith(e)}},ke={value(){me(this).remove()}},Ce={value(){const{parentNode:e}=this;if(e===this)this[ye]===Q&&(this[ye]=[...this.childNodes]);else{if(e){let{firstChild:e,lastChild:t}=this;for(this[ye]=[e];e!==t;)this[ye].push(e=e.nextSibling)}this.replaceChildren(...this[ye])}return this}};function Te(e){const t=H("<>"),n=H("</>");return e.replaceChildren(t,...e.childNodes,n),be=!0,P(e,{[ye]:{writable:!0,value:Q},firstChild:{value:t},lastChild:{value:n},parentNode:we,valueOf:Ce,replaceWith:Se,remove:ke})}Te.prototype=DocumentFragment.prototype;const De=16,Oe=32768,Ne=((e=globalThis.document)=>{let t,n=e.createElement("template");return(s,i=!1)=>{if(i)return t||(t=e.createRange(),t.selectNodeContents(e.createElementNS("http://www.w3.org/2000/svg","svg"))),t.createContextualFragment(s);n.innerHTML=s;const r=n.content;return n=n.cloneNode(!1),r}})(document),$e=Symbol("ref"),We=(e,t)=>{for(const[n,s]of B(t)){const t="role"===n?n:`aria-${n.toLowerCase()}`;null==s?e.removeAttribute(t):e.setAttribute(t,s)}},Ae=e=>(t,n)=>{null==n?t.removeAttribute(e):t.setAttribute(e,n)},Ee=(e,t)=>{e[ye]=((e,t,n,s)=>{const i=s.parentNode,r=t.length;let o=e.length,l=r,a=0,c=0,u=null;for(;a<o||c<l;)if(o===a){const e=l<r?c?n(t[c-1],-0).nextSibling:n(t[l],0):s;for(;c<l;)i.insertBefore(n(t[c++],1),e)}else if(l===c)for(;a<o;)u&&u.has(e[a])||n(e[a],-1).remove(),a++;else if(e[a]===t[c])a++,c++;else if(e[o-1]===t[l-1])o--,l--;else if(e[a]===t[l-1]&&t[c]===e[o-1]){const s=n(e[--o],-0).nextSibling;i.insertBefore(n(t[c++],1),n(e[a++],-0).nextSibling),i.insertBefore(n(t[--l],1),s),e[o]=t[l]}else{if(!u){u=new Map;let e=c;for(;e<l;)u.set(t[e],e++)}const s=u.get(e[a])??-1;if(s<0)n(e[a++],-1).remove();else if(c<s&&s<l){let r=a,d=1;for(;++r<o&&r<l&&u.get(e[r])===s+d;)d++;if(d>s-c){const r=n(e[a],0);for(;c<s;)i.insertBefore(n(t[c++],1),r)}else i.replaceChild(n(t[c++],1),n(e[a++],-1))}else a++}return t})(e[ye]||Q,t,xe,e)},Me=new WeakMap,Re=(e,t)=>{const n="object"==typeof t?t??e:((e,t)=>{let n=Me.get(e);return n?n.data=t:Me.set(e,n=document.createTextNode(t)),n})(e,t),s=e[ye]??e;n!==s&&s.replaceWith(xe(e[ye]=n,1))},Le=(e,t)=>{Re(e,t instanceof W?t.value:t)},_e=({dataset:e},t)=>{for(const[n,s]of B(t))null==s?delete e[n]:e[n]=s},je=new Map,Fe=e=>{let t=je.get(e);return t||je.set(e,t=Pe(e)),t},Pe=e=>(t,n)=>{t[e]=n},Be=(e,t)=>{for(const[n,s]of B(t))Ae(n)(e,s)},Je=(e,t,n)=>n?(n,s)=>{const i=n[t];i?.length&&n.removeEventListener(e,...i),s&&n.addEventListener(e,...s),n[t]=s}:(n,s)=>{const i=n[t];i&&n.removeEventListener(e,i),s&&n.addEventListener(e,s),n[t]=s},Ve=e=>(t,n)=>{t.toggleAttribute(e,!!n)};let ze=!1;let He=!0;const qe=e=>{He=e},Ge=()=>He,Ie=e=>xe(e.n?e.update(e):e.valueOf(!1),1),Ke=(e,t)=>{const n=[],s=e.length,i=t.length;for(let r,o,l=0,a=0;a<i;a++)r=t[a],n[a]=l<s&&(o=e[l++]).t===r.t?(t[a]=o).update(r):r.valueOf(!1);return n},Qe=(e,t,n)=>{const s=R,i=n.length;let r=0;_(e=>r<i?n[r++]:n[r++]=e instanceof W?e:s(e));const o=Ge();o&&qe(!o);try{return e(t,Ze)}finally{o&&qe(o),_(s)}},Ue=(e,t)=>(e.t===t.t?e.update(t):(e.n.replaceWith(Ie(t)),e=t),e),Xe=(e,t,n)=>{let s,i=[],r=[De,null,n],o=!0;return x(()=>{if(o)o=!1,s=Qe(t,n,i),i.length||(i=Q),s?(e.replaceWith(Ie(s)),r[1]=s):e.remove();else{const e=Qe(t,n,i);s&&Ue(s,e)===e&&(r[2]=s=e)}}),r},Ye=Symbol(),Ze={};class et{constructor(e,t){this.t=e,this.v=t,this.n=null,this.k=-1}valueOf(e=Ge()){const[t,n,s]=this.t,i=document.importNode(t,!0),r=this.v;let o,l,a,c=r.length,u=Q;if(0<c){for(u=n.slice(0);c--;){const[t,s,d]=n[c],f=r[c];if(l!==t&&(o=ve(i,t),l=t),d&De){const e=o[Ye]||(o[Ye]={});if(d===De){for(const{name:t,value:n}of o.attributes)e[t]??=n;e.children??=[...o.content.childNodes],u[c]=Xe(o,f,e)}else s(e,f),u[c]=[d,s,e]}else{let t=!0;e||!(8&d)||d&Oe||(1&d?(t=!1,f.length&&s(o,f[0]instanceof et?Ke(Q,f):f)):f instanceof et&&(t=!1,s(o,Ie(f)))),t&&(512===d?this.k=c:(16384===d&&(a??=new Set).add(o),s(o,f))),u[c]=[d,s,f,o],e&&8&d&&o.remove()}}a&&(e=>{for(const t of e){const e=t[$e];"function"==typeof e?e(t):e instanceof W?e.value=t:e&&(e.current=t)}})(a)}const{childNodes:d}=i,f=d.length,p=1===f?d[0]:f?Te(i):i;return this.v=u,this.n=p,-1<this.k&&s.set(u[this.k][2],p,this),p}update(e){const t=this.k,n=this.v,s=e.v;if(-1<t&&n[t][2]!==s[t])return((e,t)=>e.t[2].get(t)?.update(e)??e.valueOf(!1))(e,s[t]);let{length:i}=n;for(;i--;){const e=n[i],[t,r,o]=e;if(512===t)continue;let l=s[i];if(t&De)if(t===De){const t=l(o,Ze);r&&Ue(r,t)===t&&(e[2]=t)}else r(o,l);else{let n=l;if(1&t){if(8&t)l.length&&l[0]instanceof et&&(n=Ke(o,l));else if(256&t&&l[0]===o[0])continue}else if(8&t)if(t&Oe){if(l===o){r(e[3],n);continue}}else o instanceof et&&(l=Ue(o,l),n=l.n);l!==o&&(e[2]=l,r(e[3],n))}}return this.n}}const tt=new WeakMap;class nt extends Map{constructor(){super()._=new FinalizationRegistry(e=>this.delete(e))}get(e){const t=super.get(e)?.deref();return t&&tt.get(t)}set(e,t,n){tt.set(t,n),this._.register(t,e),super.set(e,new WeakRef(t))}}const st=(({Comment:e=ee,DocumentType:t=te,Text:n=ne,Fragment:s=re,Element:i=ie,Component:r=se,update:o=de})=>(l,a,c)=>{const u=l.join(oe).trim(),d=new Set,f=[];let p=new s,h=0,v=0,g=0,b=Q;for(const s of u.matchAll(ce)){if(0<v){v--;continue}const l=s[0],m=s.index;if(h<m&&U(p,new n(u.slice(h,m))),l===oe){"table"===p.name&&(p=U(p,new i("tbody",c)),d.add(p));const t=U(p,new e("◦"));f.push(o(t,8,fe(t),"",a[g++])),h=m+1}else if(l.startsWith("<!")){const n=u.indexOf(">",m+2);if("--\x3e"===u.slice(n-2,n+1)){const t=u.slice(m+4,n-2);"!"===t[0]&&U(p,new e(t.slice(1).replace(/!$/,"")))}else U(p,new t(u.slice(m+2,n)));h=n+1}else if(l.startsWith("</")){const e=u.indexOf(">",m+2);c&&"svg"===p.name&&(c=!1),p=pe(p,d),h=e+1}else{const e=m+l.length,t=u.indexOf(">",e),s=l.slice(1);let x=s;if(s===oe?(x="template",p=U(p,new r),b=fe(p).slice(1),f.push(o(p,q,b,"",a[g++]))):(c||(x=x.toLowerCase(),"table"!==p.name||"tr"!==x&&"td"!==x||(p=U(p,new i("tbody",c)),d.add(p)),"tbody"===p.name&&"td"===x&&(p=U(p,new i("tr",c)),d.add(p))),p=U(p,new i(x,!!c&&"svg"!==x)),b=Q),e<t){let n=!1;for(const[s,i,r]of u.slice(e,t).matchAll(ue))if(r===oe||r===le||r===ae||(n=i.endsWith(oe))){const e=b===Q?b=fe(p):b;f.push(o(p,2,e,n?i.slice(0,-1):i,a[g++])),n=!1,v++}else X(p,i,!r||r.slice(1,-1));b=Q}h=t+1;const y=0<t&&"/"===u[t-1];if(c)y&&(p=p.parent);else if(y||I.has(x))p=y?pe(p,d):p.parent;else if("svg"===x)c=!0;else if(G.has(x)){const e=u.indexOf(`</${s}>`,h),t=u.slice(h,e);t.trim()===oe?(v++,f.push(o(p,3,fe(p),"",a[g++]))):U(p,new n(t)),p=p.parent,h=e+s.length+3,v++;continue}}}return h<u.length&&U(p,new n(u.slice(h))),[p,f]})({Comment:ee,DocumentType:te,Text:ne,Fragment:re,Element:ie,Component:se,update:(e,t,n,s,i)=>{switch(t){case q:return[n,i,De];case 8:return j(i)?[n,Ee,9]:i instanceof V?[n,(r=e.xml,(e,t)=>{const n=e[$e]??(e[$e]={});n.v!==t&&(n.f=Te(Ne(t,r)),n.v=t),Re(e,n.f)}),8192]:i instanceof W?[n,Le,32776]:[n,Re,8];case 3:return[n,Fe("textContent"),2048];case 2:{const t=e.type===q;switch(s.at(0)){case"@":{const e=j(i);return[n,Je(s.slice(1),Symbol(s),e),e?257:256]}case"?":return[n,Ve(s.slice(1)),4096];case".":return"..."===s?[n,t?F:Be,t?144:128]:[n,Pe(s.slice(1)),t?80:64];default:return t?[n,Pe(s),1040]:"aria"===s?[n,We,2]:"data"!==s||/^object$/i.test(e.name)?"key"===s?[n,ze=!0,512]:"ref"===s?[n,Fe($e),16384]:s.startsWith("on")?[n,Fe(s.toLowerCase()),64]:[n,Ae(s),4]:[n,_e,32]}}}var r}}),it=(e,t=new WeakMap)=>(n,...s)=>{let i=t.get(n);return i||(i=st(n,s,e),i.push((()=>{const e=ze;return ze=!1,e})()?new nt:null),i[0]=Ne(i[0].toString(),e),t.set(n,i)),new et(i,s)},rt=it(!1),ot=it(!0),lt=new WeakMap;function at(e,...t){const n=rt.apply(null,arguments);return Ge()?n.valueOf(!0):n}function ct(e,...t){const n=ot.apply(null,arguments);return Ge()?n.valueOf(!0):n}const ut=(e,t)=>{const n=lt.get(e);if(n&&n[0](),"function"==typeof t){let i;qe(!1);const r=function(e){const t={deps:void 0,depsTail:void 0,subs:void 0,subsTail:void 0,flags:0};void 0!==d&&s(t,d);const n=v(void 0),i=g(t);try{e()}finally{g(i),v(n)}return D.bind(t)}(()=>{i=t()});if(n&&n[1].t===i.t)n[1].update(i);else{const t=i.valueOf(!1);e.replaceChildren(t)}lt.set(e,[r,i])}else qe(!0),lt.delete(e),e.replaceChildren(t instanceof et?Ie(t):xe(t,1));return e};export{et as Hole,M as batch,N as computed,x as effect,Ne as fragment,at as html,ut as render,L as signal,ct as svg,z as unsafe,$ as untracked};