UNPKG

rvx

Version:

A signal based rendering library

2 lines (1 loc) 11.1 kB
import{ENV as t,MATHML as e,SVG as s,HTML as n,Context as r,capture as l,render as i}from"./rvx.min.js";import{AsyncContext as a,ASYNC as o}from"./rvx.async.min.js";const h=Symbol.for("rvx:rvx-dom-window");function u(){const e=t.current;return null!==e&&"object"==typeof e&&!0===e[h]}const c=/["'<>&]/;function p(t,e){const s=c.exec(e);if(null===s)return t+e;let n,r=0,l=s.index;t:for(;l<e.length;){switch(e.charCodeAt(l)){case 34:n="&#34;";break;case 38:n="&amp;";break;case 39:n="&#39;";break;case 60:n="&lt;";break;case 62:n="&gt;";break;default:l++;continue t}l!==r&&(t+=e.slice(r,l)),t+=n,l++,r=l}return l!==r&&(t+=e.slice(r,l)),t}const d=Symbol("length"),f=Symbol("appendHtmlTo"),m=Symbol("cloneChildren"),g=Symbol("clone");class v{#t;constructor(t){this.#t=t.firstChild}next(){const t=this.#t;return null===t?{value:null,done:!0}:(this.#t=t.nextSibling,{value:t,done:!1})}}class x{#e;constructor(t){this.#e=t}get length(){return this.#e[d]()}forEach(t,e){let s=0,n=this.#e.firstChild;for(;null!==n;)t.call(e,n,s,this),n=n.nextSibling,s++}[Symbol.iterator](){return new v(this.#e)}values(){return new v(this.#e)}}class y{}class w{addEventListener(){}removeEventListener(){}dispatchEvent(){throw new Error("dispatching events is not supported")}}class b extends w{get body(){return null}get activeElement(){return null}createTextNode(t){return new A(t)}createComment(t){return new T(t)}createDocumentFragment(){return new C}createElementNS(t,e){return new H(t,e)}createElement(t){return new H(n,t)}}class S extends w{#s=null;#n=null;#r=null;#l=null;#i=null;#a=0;#o=null;get parentNode(){return this.#s}get firstChild(){return this.#n}get lastChild(){return this.#r}get previousSibling(){return this.#l}get nextSibling(){return this.#i}get childNodes(){return null===this.#o&&(this.#o=new x(this)),this.#o}[d](){return this.#a}[f](t){let e=this.#n;for(;null!==e;)t=e[f](t),e=e.#i;return t}[m](t){let e=this.#n;for(;null!==e;){const s=e.cloneNode(!0),n=t.#r;null===n?t.#n=s:n.#i=s,s.#l=n,s.#s=t,t.#r=s,t.#a++,e=e.#i}}cloneNode(t){throw new Error("not supported")}contains(t){if(null===t)return!1;do{if(t===this)return!0;t=t.#s}while(null!==t);return!1}hasChildNodes(){return this.#a>0}removeChild(t){if(t.#s!==this)throw new Error("node is not a child of this node");const e=t.#l,s=t.#i;return null===e?this.#n=s:e.#i=s,null===s?this.#r=e:s.#l=e,t.#l=null,t.#i=null,t.#s=null,this.#a--,t}appendChild(t){if(11===t.nodeType){if(0===t.#a)return t;const e=this.#r,s=t.#n;null===e?this.#n=s:e.#i=s,s.#l=e,this.#r=t.#r,this.#a+=t.#a;let n=s;for(;null!==n;)n.#s=this,n=n.#i;return t.#n=null,t.#r=null,t.#a=0,t}t.#s?.removeChild(t);const e=this.#r;return null===e?this.#n=t:e.#i=t,t.#l=e,t.#s=this,this.#r=t,this.#a++,t}insertBefore(t,e){if(e.#s!==this)throw new Error("ref must be a child of this node");if(t===e)return t;if(11===t.nodeType){if(0===t.#a)return t;const s=e.#l,n=t.#n,r=t.#r;null===s?this.#n=n:s.#i=n,n.#l=s,r.#i=e,e.#l=r,this.#a+=t.#a;let l=n;for(;null!==l;)l.#s=this,l=l.#i;return t.#n=null,t.#r=null,t.#a=0,t}t.#s?.removeChild(t);const s=e.#l;return null===s?this.#n=t:s.#i=t,e.#l=t,t.#s=this,t.#l=s,t.#i=e,this.#a++,t}replaceChild(t,e){if(e.#s!==this)throw new Error("ref must be a child of this node");if(11===t.nodeType){if(0===t.#a){const t=e.#l,s=e.#i;null===t?this.#n=s:t.#i=s,null===s?this.#r=t:s.#l=t,e.#s=null,e.#l=null,e.#i=null,this.#a--}else{const s=t.#n,n=t.#r,r=e.#l,l=e.#i;null===r?this.#n=s:(r.#i=s,s.#l=r),null===l?this.#r=n:(l.#l=n,n.#i=l),e.#s=null,e.#l=null,e.#i=null,this.#a=this.#a-1+t.#a,t.#n=null,t.#r=null,t.#a=0;let i=s;for(;null!==i;)i.#s=this,i=i.#i}return e}const s=e.#l,n=e.#i;return null===s?this.#n=t:s.#i=t,null===n?this.#r=t:n.#l=t,t.#s=this,t.#l=s,t.#i=n,e.#s=null,e.#l=null,e.#i=null,e}remove(){this.#s?.removeChild(this)}append(...t){for(let e=0;e<t.length;e++){const s=t[e];"string"==typeof s?this.appendChild(new A(s)):this.appendChild(s)}}replaceChildren(...t){let e=this.#n;for(;null!==e;){const t=e.#i;e.#s=null,e.#l=null,e.#i=null,e=t}this.#a=0,this.#n=null,this.#r=null,this.append(...t)}get textContent(){let t="",e=this.#n;for(;null!==e;)8!==e.nodeType&&(t+=e.textContent),e=e.#i;return t}get outerHTML(){return this[f]("")}}class C extends S{static{this.prototype.nodeType=11,this.prototype.nodeName="#document-fragment"}cloneNode(t){const e=new C;return t&&this[m](e),e}}const N=new r(!1);class T extends S{static{this.prototype.nodeType=8,this.prototype.nodeName="#comment"}#h;#u=N.current;constructor(t){super(),this.#h=String(t)}get textContent(){return this.#h}set textContent(t){this.#h=String(t)}cloneNode(t){const e=new T(this.#h);return e.#u=this.#u,e}[f](t){return this.#u?t+"\x3c!--"+this.#h+"--\x3e":t}}class A extends S{static{this.prototype.nodeType=3,this.prototype.nodeName="#text"}#h;constructor(t){super(),this.#h=String(t)}get textContent(){return this.#h}set textContent(t){this.#h=String(t)}cloneNode(t){return new A(this.#h)}[f](t){return p(t,this.#h)}}const E=Symbol("attrChanged");class k{#c;#p=null;#d=null;constructor(t){this.#c=t}[g](t){null!==t.#p&&(this.#p=this.#c.find(t=>"class"===t.name)),null!==t.#d&&(this.#d=Array.from(t.#d))}get length(){return this.#f().length}get value(){const t=this.#p;if(null===t||t.stale){const e=this.#d;if(null===e)return"";let s="";for(let t=0;t<e.length;t++)t>0&&(s+=" "),s+=e[t];return t.value=s,t.stale=!1,s}return t.value}#f(){let t=this.#d;if(null===t){const e=this.#p;t=null===e||e.stale?[]:e.value.split(" "),this.#d=t}return t}#m(){const t=this.#p;null===t?this.#c.push(this.#p={name:"class",value:"",stale:!0}):t.stale=!0}[E](t){this.#p=t,this.#d=null}add(...t){const e=this.#f();for(let s=0;s<t.length;s++){const n=t[s];e.includes(n)||e.push(n)}this.#m()}contains(t){return this.#f().includes(String(t))}remove(...t){const e=this.#f();for(let s=0;s<t.length;s++){const n=String(t[s]),r=e.indexOf(n);r>=0&&e.splice(r,1)}this.#m()}replace(t,e){const s=this.#f(),n=s.indexOf(String(t));return n>=0&&(s[n]=String(e),this.#m(),!0)}toggle(t,e){t=String(t);const s=this.#f(),n=s.indexOf(t);let r=!1;return void 0===e?n<0?(s.push(t),r=!0):s.splice(n,1):e?(n<0&&s.push(t),r=!0):n>=0&&s.splice(n,1),this.#m(),r}values(){return this.#f()[Symbol.iterator]()}[Symbol.iterator](){return this.#f()[Symbol.iterator]()}}class L{#c;#p=null;#g=null;constructor(t){this.#c=t}[g](t){null!==t.#p&&(this.#p=this.#c.find(t=>"style"===t.name)),null!==t.#g&&(this.#g=t.#g.map(t=>({name:t.name,value:t.value,important:t.important})))}get cssText(){const t=this.#p;if(null===t||t.stale){const e=this.#g;if(null===e)return"";let s="";for(let t=0;t<e.length;t++){const n=e[t];t>0&&(s+="; "),s=s+n.name+": "+n.value,n.important&&(s+=" !important")}return t.stale=!1,t.value=s,s}return t.value}#f(){let t=this.#g;if(null===t){const e=this.#p;if(null!==e&&!e.stale&&""!==e.value)throw new Error("style attribute parsing is not supported");this.#g=t=[]}return t}#m(){const t=this.#p;null===t?this.#c.push(this.#p={name:"style",value:"",stale:!0}):t.stale=!0}[E](t){this.#p=t,this.#g=null}setProperty(t,e,s){const n=this.#f();for(let r=0;r<n.length;r++){const l=n[r];if(l.name===t)return l.value=null===e?"":String(e),l.important="important"===s,void this.#m()}n.push({name:t,value:null===e?"":String(e),important:"important"===s}),this.#m()}removeProperty(t){const e=this.#f();for(let s=0;s<e.length;s++){const n=e[s];if(n.name===t)return e.splice(s,1),this.#m(),n.value}return""}getPropertyValue(t){const e=this.#f();for(let s=0;s<e.length;s++){const n=e[s];if(n.name===t)return n.value}return""}}class H extends S{static{this.prototype.nodeType=1}#v;#x;#y;#w;#c=[];#b=new k(this.#c);#S=new L(this.#c);constructor(t,r){super(),this.#v=function(t){switch(t){case n:return 0;case s:return 1;case e:return 2;default:throw new Error("unsupported namespace uri")}}(t),this.#x=t,this.#w=r}get tagName(){return this.#w}get nodeName(){return this.#w}get namespaceURI(){return this.#x}get innerHTML(){let t="",e=this.firstChild;for(;null!==e;)t=e[f](t),e=e.nextSibling;return t}set innerHTML(t){""===t?this.replaceChildren():this.replaceChildren(new I(t))}get classList(){return null===this.#b&&(this.#b=new k(this.#c)),this.#b}get style(){return null===this.#S&&(this.#S=new L(this.#c)),this.#S}cloneNode(t){const e=new H(this.#x,this.#w);e.#y=this.#y;const s=this.#c,n=e.#c;for(let t=0;t<s.length;t++){const e=s[t];n.push({name:e.name,value:e.value,stale:e.stale})}return e.#b[g](this.#b),e.#S[g](this.#S),t&&this[m](e),e}focus(){}blur(){}#C(t,e){switch(t){case"class":this.#b[E](e);break;case"style":this.#S[E](e)}}setAttribute(t,e){const s=this.#c;for(let n=0;n<s.length;n++){const r=s[n];if(r.name===t)return r.value=String(e),r.stale=!1,void this.#C(t,r)}const n={name:t,value:String(e),stale:!1};s.push(n),this.#C(t,n)}removeAttribute(t){const e=this.#c;for(let s=0;s<e.length;s++){if(e[s].name===t)return e.splice(s,1),void this.#C(t,null)}}toggleAttribute(t,e){const s=this.#c;for(let n=0;n<s.length;n++){if(s[n].name===t)return void(void 0!==e&&e||(s.splice(n,1),this.#C(t,null)))}if(void 0===e||e){const e={name:t,value:"",stale:!1};s.push(e),this.#C(t,e)}}getAttribute(t){const e=this.#c;for(let s=0;s<e.length;s++){const n=e[s];if(n.name===t)return this.#N(n)}return null}hasAttribute(t){const e=this.#c;for(let s=0;s<e.length;s++)if(e[s].name===t)return!0;return!1}#N(t){if(t.stale)switch(t.name){case"class":return this.#b.value;case"style":return this.#S.cssText;default:throw new Error("invalid internal state")}return t.value}#T(){return this.#y??(this.#y=function(t,e){if(0!==t)return!1;switch(e){case"area":case"base":case"br":case"col":case"embed":case"hr":case"img":case"input":case"link":case"meta":case"param":case"source":case"track":case"wbr":return!0;default:return!1}}(this.#v,this.#w))}[f](t){t=t+"<"+this.#w;const e=this.#c;for(let s=0;s<e.length;s++){const n=e[s];t=p(t+" "+n.name+'="',this.#N(n))+'"'}return this.#T()?t+=">":this.hasChildNodes()||0===this.#v?t=super[f](t+">")+"</"+this.#w+">":t+="/>",t}}class I extends S{static{this.prototype.nodeType=0,this.prototype.nodeName="#rvx-dom-raw-html"}#A;constructor(t){super(),this.#A=t}cloneNode(t){return new I(this.#A)}[f](t){return t+this.#A}}class M extends w{static{this.prototype[h]=!0,this.prototype.Comment=T,this.prototype.CustomEvent=y,this.prototype.Document=b,this.prototype.DocumentFragment=C,this.prototype.Element=H,this.prototype.Event=y,this.prototype.Node=S,this.prototype.Text=A}window=this;document=new b}const R=new M;function U(t){const{first:e,last:s}=t;if(!(e instanceof S))throw new Error("root is not an rvx dom node");return e===s?e.outerHTML:e.parentNode.outerHTML}function j(e,s){let n;return l(()=>{t.provide(R,()=>{const t=i(e(s));n=U(t)})})(),n}async function D(e,s){const n=new a;let h;const u=l(()=>{r.provide([t.with(R),o.with(n)],()=>{h=i(e(s))})});try{return await n.complete(),U(h)}finally{u()}}export{T as Comment,b as Document,C as DocumentFragment,H as Element,k as ElementClassList,L as ElementStyles,y as Event,w as EventTarget,S as Node,x as NodeList,I as RawHTML,A as Text,N as VISIBLE_COMMENTS,R as WINDOW,M as Window,u as isRvxDom,j as renderToString,D as renderToStringAsync};