UNPKG

rvx

Version:

A signal based rendering library

2 lines (1 loc) 10.2 kB
import{ENV as t,MATHML as e,SVG as s,HTML as n,capture as r,render as l,Context 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=Symbol("length"),p=Symbol("appendHtmlTo");class d{#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 g{#e;constructor(t){this.#e=t}get length(){return this.#e[c]()}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 d(this.#e)}values(){return new d(this.#e)}}const f=/["'<>&]/;function m(t,e){const s=f.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}class v{}class x{addEventListener(){}removeEventListener(){}dispatchEvent(){throw new Error("dispatching events is not supported")}}class y extends x{get body(){return null}get activeElement(){return null}createTextNode(t){return new C(t)}createComment(t){return new b(t)}createDocumentFragment(){return new S}createElementNS(t,e){return new M(t,e)}createElement(t){return new M(n,t)}}class w extends x{#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 g(this)),this.#o}[c](){return this.#a}[p](t){let e=this.firstChild;for(;null!==e;)t=e[p](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 C(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[p]("")}}class S extends w{static{this.prototype.nodeType=11,this.prototype.nodeName="#document-fragment"}}class b extends w{static{this.prototype.nodeType=8,this.prototype.nodeName="#comment"}#h;constructor(t){super(),this.#h=String(t)}get textContent(){return this.#h}set textContent(t){this.#h=String(t)}[p](t){return t}}class C extends w{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)}[p](t){return m(t,this.#h)}}const N=0,T=1,A=2;function E(t){switch(t){case n:return 0;case s:return 1;case e:return 2;default:throw new Error("unsupported namespace uri")}}function L(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}}const k=Symbol("attrChanged");class H{#u;#c=null;#p=null;constructor(t){this.#u=t}get length(){return this.#d().length}get value(){const t=this.#c;if(null===t||t.stale){const e=this.#p;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}#d(){let t=this.#p;if(null===t){const e=this.#c;t=null===e||e.stale?[]:e.value.split(" "),this.#p=t}return t}#g(){const t=this.#c;null===t?this.#u.push(this.#c={name:"class",value:"",stale:!0}):t.stale=!0}[k](t){this.#c=t,this.#p=null}add(...t){const e=this.#d();for(let s=0;s<t.length;s++){const n=t[s];e.includes(n)||e.push(n)}this.#g()}contains(t){return this.#d().includes(String(t))}remove(...t){const e=this.#d();for(let s=0;s<t.length;s++){const n=String(t[s]),r=e.indexOf(n);r>=0&&e.splice(r,1)}this.#g()}replace(t,e){const s=this.#d(),n=s.indexOf(String(t));return n>=0&&(s[n]=String(e),this.#g(),!0)}toggle(t,e){t=String(t);const s=this.#d(),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.#g(),r}values(){return this.#d()[Symbol.iterator]()}[Symbol.iterator](){return this.#d()[Symbol.iterator]()}}class j{#u;#c=null;#f=null;constructor(t){this.#u=t}get cssText(){const t=this.#c;if(null===t||t.stale){const e=this.#f;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}#d(){let t=this.#f;if(null===t){const e=this.#c;if(null!==e&&!e.stale&&""!==e.value)throw new Error("style attribute parsing is not supported");this.#f=t=[]}return t}#g(){const t=this.#c;null===t?this.#u.push(this.#c={name:"style",value:"",stale:!0}):t.stale=!0}[k](t){this.#c=t,this.#f=null}setProperty(t,e,s){const n=this.#d();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.#g()}n.push({name:t,value:String(e),important:"important"===s}),this.#g()}removeProperty(t){const e=this.#d();for(let s=0;s<e.length;s++){const n=e[s];if(n.name===t)return e.splice(s,1),this.#g(),n.value}return""}getPropertyValue(t){const e=this.#d();for(let s=0;s<e.length;s++){const n=e[s];if(n.name===t)return n.value}return""}}class M extends w{static{this.prototype.nodeType=1}#m;#v;#x;#y;#u=[];#w=new H(this.#u);#S=new j(this.#u);constructor(t,e){super(),this.#m=E(t),this.#v=t,this.#y=e}get tagName(){return this.#y}get nodeName(){return this.#y}get namespaceURI(){return this.#v}get innerHTML(){let t="",e=this.firstChild;for(;null!==e;)t=e[p](t),e=e.nextSibling;return t}set innerHTML(t){this.replaceChildren(new I(t))}get classList(){return null===this.#w&&(this.#w=new H(this.#u)),this.#w}get style(){return null===this.#S&&(this.#S=new j(this.#u)),this.#S}focus(){}blur(){}#b(t,e){switch(t){case"class":this.#w[k](e);break;case"style":this.#S[k](e)}}setAttribute(t,e){const s=this.#u;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.#b(t,r)}const n={name:t,value:String(e),stale:!1};s.push(n),this.#b(t,n)}removeAttribute(t){const e=this.#u;for(let s=0;s<e.length;s++){if(e[s].name===t)return e.splice(s,1),void this.#b(t,null)}}toggleAttribute(t,e){const s=this.#u;for(let n=0;n<s.length;n++){if(s[n].name===t)return void(void 0!==e&&e||(s.splice(n,1),this.#b(t,null)))}if(void 0===e||e){const e={name:t,value:"",stale:!1};s.push(e),this.#b(t,e)}}getAttribute(t){const e=this.#u;for(let s=0;s<e.length;s++){const n=e[s];if(n.name===t)return this.#C(n)}return null}hasAttribute(t){const e=this.#u;for(let s=0;s<e.length;s++)if(e[s].name===t)return!0;return!1}#C(t){if(t.stale)switch(t.name){case"class":return this.#w.value;case"style":return this.#S.cssText;default:throw new Error("invalid internal state")}return t.value}#N(){return this.#x??(this.#x=L(this.#m,this.#y))}[p](t){t=t+"<"+this.#y;const e=this.#u;for(let s=0;s<e.length;s++){const n=e[s];t=m(t+" "+n.name+'="',this.#C(n))+'"'}return this.#N()?t+=">":this.hasChildNodes()||0===this.#m?t=super[p](t+">")+"</"+this.#y+">":t+="/>",t}}class I extends w{#T;constructor(t){super(),this.#T=t}[p](t){return t+this.#T}}class R extends x{static{this.prototype[h]=!0,this.prototype.Comment=b,this.prototype.CustomEvent=v,this.prototype.Document=y,this.prototype.DocumentFragment=S,this.prototype.Element=M,this.prototype.Event=v,this.prototype.Node=w,this.prototype.Text=C}window=this;document=new y}const U=new R;function D(t){const{first:e,last:s}=t;if(!(e instanceof w))throw new Error("root is not an rvx dom node");return e===s?e.outerHTML:e.parentNode.outerHTML}function O(e,s){let n;return r((()=>{t.inject(U,(()=>{const t=l(e(s));n=D(t)}))}))(),n}async function P(e,s){const n=new a;let h;const u=r((()=>{i.inject([t.with(U),o.with(n)],(()=>{h=l(e(s))}))}));try{return await n.complete(),D(h)}finally{u()}}export{y as Document,S as DocumentFragment,M as Element,H as ElementClassList,j as ElementStyles,w as Node,g as NodeList,b as NoopComment,v as NoopEvent,x as NoopEventTarget,I as RawHTML,C as Text,U as WINDOW,R as Window,N as XMLNS_HTML,A as XMLNS_MATHML,T as XMLNS_SVG,m as htmlEscapeAppendTo,u as isRvxDom,L as isVoidTag,O as renderToString,P as renderToStringAsync,E as resolveNamespaceURI};