UNPKG

rvx

Version:

A signal based rendering library

2 lines (1 loc) 10.3 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"),g=Symbol("appendHtmlTo");class f{#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 m{#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 f(this.#e)}values(){return new f(this.#e)}}class v{}const x=v;class y{addEventListener(){}removeEventListener(){}dispatchEvent(){throw new Error("dispatching events is not supported")}}const w=y;class b extends y{get body(){return null}get activeElement(){return null}createTextNode(t){return new E(t)}createComment(t){return new T(t)}createDocumentFragment(){return new C}createElementNS(t,e){return new j(t,e)}createElement(t){return new j(n,t)}}class S extends y{#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 m(this)),this.#o}[d](){return this.#a}[g](t){let e=this.firstChild;for(;null!==e;)t=e[g](t),e=e.nextSibling;return t}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(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 E(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[g]("")}}class C extends S{static{this.prototype.nodeType=11,this.prototype.nodeName="#document-fragment"}}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)}[g](t){return this.#u?t+"\x3c!--"+this.#h+"--\x3e":t}}const A=T;class E 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)}[g](t){return p(t,this.#h)}}const L=Symbol("attrChanged");class k{#c;#p=null;#d=null;constructor(t){this.#c=t}get length(){return this.#g().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}#g(){let t=this.#d;if(null===t){const e=this.#p;t=null===e||e.stale?[]:e.value.split(" "),this.#d=t}return t}#f(){const t=this.#p;null===t?this.#c.push(this.#p={name:"class",value:"",stale:!0}):t.stale=!0}[L](t){this.#p=t,this.#d=null}add(...t){const e=this.#g();for(let s=0;s<t.length;s++){const n=t[s];e.includes(n)||e.push(n)}this.#f()}contains(t){return this.#g().includes(String(t))}remove(...t){const e=this.#g();for(let s=0;s<t.length;s++){const n=String(t[s]),r=e.indexOf(n);r>=0&&e.splice(r,1)}this.#f()}replace(t,e){const s=this.#g(),n=s.indexOf(String(t));return n>=0&&(s[n]=String(e),this.#f(),!0)}toggle(t,e){t=String(t);const s=this.#g(),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.#f(),r}values(){return this.#g()[Symbol.iterator]()}[Symbol.iterator](){return this.#g()[Symbol.iterator]()}}class H{#c;#p=null;#m=null;constructor(t){this.#c=t}get cssText(){const t=this.#p;if(null===t||t.stale){const e=this.#m;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}#g(){let t=this.#m;if(null===t){const e=this.#p;if(null!==e&&!e.stale&&""!==e.value)throw new Error("style attribute parsing is not supported");this.#m=t=[]}return t}#f(){const t=this.#p;null===t?this.#c.push(this.#p={name:"style",value:"",stale:!0}):t.stale=!0}[L](t){this.#p=t,this.#m=null}setProperty(t,e,s){const n=this.#g();for(let r=0;r<n.length;r++){const l=n[r];if(l.name===t)return l.value=String(e),l.important="important"===s,void this.#f()}n.push({name:t,value:String(e),important:"important"===s}),this.#f()}removeProperty(t){const e=this.#g();for(let s=0;s<e.length;s++){const n=e[s];if(n.name===t)return e.splice(s,1),this.#f(),n.value}return""}getPropertyValue(t){const e=this.#g();for(let s=0;s<e.length;s++){const n=e[s];if(n.name===t)return n.value}return""}}class j extends S{static{this.prototype.nodeType=1}#v;#x;#y;#w;#c=[];#b=new k(this.#c);#S=new H(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[g](t),e=e.nextSibling;return t}set innerHTML(t){""===t?this.replaceChildren():this.replaceChildren(new M(t))}get classList(){return null===this.#b&&(this.#b=new k(this.#c)),this.#b}get style(){return null===this.#S&&(this.#S=new H(this.#c)),this.#S}focus(){}blur(){}#C(t,e){switch(t){case"class":this.#b[L](e);break;case"style":this.#S[L](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))}[g](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[g](t+">")+"</"+this.#w+">":t+="/>",t}}class M extends S{static{this.prototype.nodeType=0,this.prototype.nodeName="#rvx-dom-raw-html"}#A;constructor(t){super(),this.#A=t}[g](t){return t+this.#A}}class I extends y{static{this.prototype[h]=!0,this.prototype.Comment=T,this.prototype.CustomEvent=v,this.prototype.Document=b,this.prototype.DocumentFragment=C,this.prototype.Element=j,this.prototype.Event=v,this.prototype.Node=S,this.prototype.Text=E}window=this;document=new b}const R=new I;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 D(e,s){let n;return l(()=>{t.inject(R,()=>{const t=i(e(s));n=U(t)})})(),n}async function O(e,s){const n=new a;let h;const u=l(()=>{r.inject([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,j as Element,k as ElementClassList,H as ElementStyles,v as Event,y as EventTarget,S as Node,m as NodeList,A as NoopComment,x as NoopEvent,w as NoopEventTarget,M as RawHTML,E as Text,N as VISIBLE_COMMENTS,R as WINDOW,I as Window,u as isRvxDom,D as renderToString,O as renderToStringAsync};