-
Notifications
You must be signed in to change notification settings - Fork 0
/
fallback-dom.js
2 lines (2 loc) · 17.7 KB
/
fallback-dom.js
1
2
import{querySel as e}from"./query-selector.js";var t,r,n,l,s,o;const i=Symbol("customElements"),u=Symbol("document"),a=Symbol("reg"),c=Symbol("con"),h=Symbol("when"),m=Symbol("localName"),b=Symbol("prefix"),d=Symbol("namespaceURI"),p=Symbol("isCustomElement"),f=Symbol("customElementState");function g(e){const t=e[f];if(!t)return!1;{const[e,r,n]=t;if(n||!e&&!r)return n}return e[f][2]=!0,Promise.resolve().then((()=>{const t=e[f],[r,n]=t;if(t[0]=!1,t[1]=!1,t[2]=!1,n){const t=e.disconnectedCallback;t&&t.call(e)}if(r){const t=e.connectedCallback;t&&t.call(e)}})),!0}class CustomElementRegistry{constructor(){this[t]=new Map,this[r]=new Map,this[n]=new Map}define(e,t){e+="";const r=this[a];if(r.has(e))throw Error("an element with the name "+JSON.stringify(e)+" has already been defined");const n=t.prototype;let l=n;for(;l&&l!==Element.prototype;)l=Object.getPrototypeOf(l);if(!l)throw Error("the constructor of element "+JSON.stringify(e)+" is not a descendant of the right kind of Element class");const s=this[c];if(Element==t||s.has(t))throw Error("the prototype of element "+JSON.stringify(e)+" must be unique");if(n[p])throw Error("the prototype of element "+JSON.stringify(e)+" has already been registered in another registry");r.set(e,t),s.set(t,e),Object.defineProperties(n,{[m]:{value:e,configurable:!1,writable:!1,enumerable:!1},[b]:{value:null,configurable:!1,writable:!1,enumerable:!1},[d]:{value:null,configurable:!1,writable:!1,enumerable:!1},[p]:{value:!0,configurable:!1,writable:!1,enumerable:!1}});const o=this[h].get(e);if(o){this[h].delete(e);for(const e of o)e(t)}}upgrade(e){const t=this[a],r=e.tagName,n=t.get(r);if(!n)return;const l=Object.getPrototypeOf(e);if(l===Element.prototype)try{Object.setPrototypeOf(e,n.prototype),n.call(e),e.isConnected&&e.connectedCallback&&e[f]&&!e[f][2]&&(e[f][0]=!0,g(e))}catch(t){throw Object.setPrototypeOf(e,l),t}}whenDefined(e){const t=this[a].get(e);if(t)return Promise.resolve(t);let r=this[h].get(e);r||(r=[],this[h].set(e,r));const n=r;return new Promise((e=>n.push(e)))}get(e){return this[a].get(e)}}t=a,r=c,n=h;class DOMTokenList{constructor(e,t,r,n){this._d=new Set,this._split=e,this._accessor=r,this._setter=n,this._comb=t}_upd(){const e=this._accessor(),t=this._d;if(e==this._lastRead)return t;if(this._lastRead=e,t.clear(),!e)return t;for(const r of e.split(this._split))r&&t.add(r);return t}get length(){return this._upd().size}get value(){return this._upd(),this._lastRead||""}set value(e){this._upd(),this._setter(e||"")}toString(){return this._upd(),this._lastRead||""}add(...e){const t=this._upd(),r=this._split;for(const n of e)if(n){if(r.test(n))throw Error("invalid token");t.add(n)}this._setter(Array.from(t).join(this._comb))}contains(e){return this._upd().has(e)}remove(...e){const t=this._upd(),r=this._split;for(const n of e)if(n){if(r.test(n))throw Error("invalid token");t.delete(n)}this._setter(Array.from(t).join(this._comb))}replace(e,t){const r=this._upd();if(!r.delete(e))return!1;if(this._split.test(t))throw Error("invalid token");return r.add(t),this._setter(Array.from(r).join(this._comb)),!0}toggle(e,t){const r=this._upd();if(!t&&(r.delete(e)||!1===t))return!1;if(this._split.test(e))throw Error("invalid token");return r.add(e),this._setter(Array.from(r).join(this._comb)),!0}forEach(e,t){this._upd().forEach((r=>e.call(t,r,-1,this)))}}const w=Symbol("parent"),y=Symbol("previousSibling"),v=Symbol("nextSibling");class Node{get parentNode(){return this[w]}get previousSibling(){return this[y]}get nextSibling(){return this[v]}get ownerDocument(){return this[u]}get isConnected(){return this instanceof Document||!!this[w]?.isConnected}constructor(){if(this[l]=null,this[s]=null,this[o]=null,void 0===this[u])throw Error("a node must be created by a document")}lookupNamespaceURI(e){const t=(e=e||null)?"xmlns:"+e:"xmlns";let r=this;for(;r;){const n=r.nodeType;if(9==n)return r[P].get(e||"")||null;if(10==n||11==n)return this.ownerDocument?.[P].get(e||"")||null;if(1==n){const n=r;if(n.prefix==e)return n.namespaceURI;const l=n.getAttribute(t);if(l)return l}r=r[w]}return this.ownerDocument?.[P].get(e||"")||null}lookupPrefix(e){if(!e)return null;if("http://www.w3.org/2000/xmlns/"==e)return"xmlns";if("http://www.w3.org/XML/1998/namespace"==e)return"xml";let t=this;for(;t;){const r=this.nodeType;if(10==r||11==r)return null;if(1==r){const r=t.attributes;for(const t of r)if("http://www.w3.org/2000/xmlns/"==t.namespaceURI&&t.value==e)return t.localName}t=t[w]}return null}cloneNode(e=!1){const t=9==this.nodeType&&this.constructor!=this.ownerDocument?.Element?new this.constructor:Object.create(Object.getPrototypeOf(this),{[u]:{value:this[u],writable:!0,enumerable:!1},[w]:{value:null,writable:!0,enumerable:!1},[y]:{value:null,writable:!0,enumerable:!1},[v]:{value:null,writable:!0,enumerable:!1}});{const e=this[N];void 0!==e&&Object.defineProperty(t,N,{value:e,writable:!0,enumerable:!1})}{const e=this[E];if(void 0!==e){const r=new Map;for(const[t,n]of e){if(!n.size)continue;const e=new Map;for(const[t,[r,l]]of n)e.set(t,[r,l]);r.set(t,e)}Object.defineProperty(t,E,{value:r,writable:!0,enumerable:!1})}}{const e=this[m];t[m]||void 0===e||Object.defineProperty(t,m,{value:e,writable:!0,enumerable:!1})}{const e=this[b];t[b]||void 0===e||Object.defineProperty(t,b,{value:e,writable:!0,enumerable:!1})}{const e=this[d];t[d]||void 0===e||Object.defineProperty(t,d,{value:e,writable:!0,enumerable:!1})}{const e=this[C];t[C]||void 0===e||Object.defineProperty(t,C,{value:e,writable:!1,enumerable:!1})}if(10==this.nodeType){const e=this;Object.defineProperties(t,{name:{value:e.name,writable:!1,enumerable:!0},publicId:{value:e.publicId,writable:!1,enumerable:!0},systemId:{value:e.systemId,writable:!1,enumerable:!0}})}if(!e||!this[S])return t;const r=this;for(const e of r[S])t.insertBefore(e.cloneNode(!0),null);return t}unsafeSetParent(e){const t=this[w];this[w]=e;const r=this[f];e&&e.appendChild(this),r&&(t&&(r[0]||(r[1]=!0),r[0]=!1),e&&e.isConnected?r[0]=!0:r[0]=!1,g(this))}unsafeReplaceParent(e){const t=this[w];t&&t.removeChild(this),this[w]=e}unsafeSetPrev(e){this[y]=e,e&&(e[v]=this)}unsafeSetNext(e){this[v]=e,e&&(e[y]=this)}}l=w,s=y,o=v;const S=Symbol("childNodes"),x=Symbol("children");class ParentNode extends Node{get textContent(){const e=this[S];return e?e.reduce(((e,t)=>{const r=t.nodeType;return 1==r||3==r||4==r?e+t.textContent:e}),""):""}set textContent(e){e=e.toString();const t=this[S]=[];this[x]=[],e&&t.push(this.ownerDocument.createTextNode(e))}hasChildNodes(){const e=this[S];return!!e&&0!=e.length}get childNodes(){const e=this[S];return e?e.slice():[]}get childElementCount(){const e=this[x];return e?e.length:0}get children(){const e=this[x];return e?e.slice():[]}get firstElementChild(){const e=this[x];return e?.[0]||null}get lastElementChild(){const e=this[x];if(!e)return null;const t=e?e.length:0;return t?e[t-1]:null}get firstChild(){const e=this[S];return e?.[0]||null}get lastChild(){const e=this[S];if(!e)return null;const t=e.length;return t?e[t-1]:null}constructor(){super()}append(...e){for(const t of e){const e="string"==typeof t?this.ownerDocument.createTextNode(t):"object"==typeof t&&t instanceof Node?t:void 0;if(void 0===e)throw new TypeError("unsupported input type");this.insertBefore(e,null)}}prepend(...e){let t=this[S];t||(t=[],this[S]=t);const r=t[0]||null;for(const t of e){const e="string"==typeof t?this.ownerDocument.createTextNode(t):"object"==typeof t&&t instanceof Node?t:void 0;if(void 0===e)throw new TypeError("unsupported input type");this.insertBefore(e,r)}}querySelector(t){return e(this,t).next().value||null}querySelectorAll(t){return Array.from(e(this,t))}replaceChildren(...e){let t=this[S];if(t){if(t.length){let e=this[x];e?e.length=0:(e=[],this[x]=e);const r=t.slice();t.length=0;for(const e of r)e.unsafeSetParent(null),e.unsafeSetPrev(null),e.unsafeSetNext(null)}}else t=[],this[S]=t;e.length&&this.append(...e)}replaceChild(e,t){return this.insertBefore(e,t),this.removeChild(t)}insertBefore(e,t){if(e.ownerDocument!=this.ownerDocument){const t=this.nodeType;if(9!=t&&11!=t)throw Error("owner document mismatch");if(e.ownerDocument!=this){if(10!=e.nodeType||null!=e.ownerDocument)throw Error("owner document mismatch while inserting to the root");e[u]=this}}{let t=this;for(;t&&t!=e;)t=t.parentNode;if(t)throw Error("node insertion would lead to circularity")}if(e.parentNode==this){if(t==e||t==e.nextSibling)return e;this.removeChild(e)}let r=this[S];r||(r=[],this[S]=r);let n=this[x];n||(n=[],this[x]=n);let l=r.length,s=n.length;if(t){if(l=r.indexOf(t),-1==l)throw Error("child does not exist");s=0;for(let e=l;e>=0;e--)if(1==r[e].nodeType){s=n.indexOf(r[e]);break}}if(e instanceof DocumentFragment){const t=e[S]?.slice();if(t&&t.length){for(const e of t)e.unsafeSetParent(this),1==e.nodeType&&n.splice(s++,0,e);t[0].unsafeSetPrev(r[l-1]||null),t[t.length-1].unsafeSetNext(r[l]||null),r.splice(l,0,...t),e[S].length=0,e[x]=[]}}else e.unsafeSetPrev(r[l-1]||null),e.unsafeSetNext(r[l]||null),r.splice(l,0,e),1==e.nodeType&&n.splice(s++,0,e),e.unsafeReplaceParent(this);return e}appendChild(e){return this.insertBefore(e,null)}removeChild(e){{const t=this[S],r=t?t.indexOf(e):-1;if(!t||-1==r)return e;if(t.splice(r,1),1!=e.nodeType)return e}{const t=this[x],r=t?t.indexOf(e):-1;return t&&-1!=r&&t.splice(r,1),e}}getElementsByClassName(e){return this.querySelectorAll("."+e)}getElementsByTagName(e){return this.querySelectorAll(e)}getElementsByTagNameNS(e,t){return Array.from(this.querySelectorAll(t)).filter((t=>t.namespaceURI==e))}}Object.defineProperties(ParentNode.prototype,{[S]:{enumerable:!1,writable:!0},[x]:{enumerable:!1,writable:!0}});const N=Symbol("textContent");class CharacterData extends Node{get data(){return this[N]}set data(e){this[N]=e.toString()}get length(){return this[N].length}constructor(e){super(),this[N]=e.toString()}appendData(e){this[N]+=e.toString()}}class Text extends CharacterData{get nodeType(){return 3}get nodeName(){return"#text"}get textContent(){return this[N]}set textContent(e){this[N]=e.toString()}constructor(e){super(e)}}class CDATASection extends CharacterData{get nodeType(){return 4}get nodeName(){return"#cdata-section"}get textContent(){return this[N]}set textContent(e){this[N]=e.toString()}constructor(e){super(e)}}const C=Symbol("target");class ProcessingInstruction extends CharacterData{get nodeType(){return 7}get nodeName(){return this[C]}get target(){return this[C]}get textContent(){return this[N]}set textContent(e){this[N]=e.toString()}constructor(e,t){super(t),this[C]=e}}class Comment extends CharacterData{get nodeType(){return 8}get nodeName(){return"#comment"}get textContent(){return this[N]}set textContent(e){this[N]=e.toString()}constructor(e){super(e)}}class DocumentFragment extends ParentNode{get nodeType(){return 11}get nodeName(){return"#document-fragment"}constructor(){super()}}class Attr extends Node{get specified(){return!0}get name(){return this.prefix?`${this.prefix}:${this.localName}`:this.localName}get nodeType(){return 2}get nodeName(){return this.name}}const E=Symbol("attributes"),O=Symbol("classList");class Element extends ParentNode{get nodeType(){return 1}get nodeName(){return this.tagName}get tagName(){return this[b]?`${this[b]}:${this[m]}`:this[m]}get localName(){return this[m]}get prefix(){return this[b]}get namespaceURI(){return this[d]}get[Symbol.toStringTag](){return this[b]?`${this[b]}:${this[m]}`:this[m]}get id(){return this.getAttribute("id")||""}set id(e){this.setAttribute("id",e)}get classList(){let e=this[O];return e||(e=new DOMTokenList(/\s+/g," ",(()=>this.getAttribute("class")),(e=>this.setAttribute("class",e))),this[O]=e),e}get attributes(){const e=[],t=this[E];if(!t)return e;for(const[r,n]of t)for(const[t,[l,s]]of n){const n=Object.create(Attr.prototype,{[u]:{value:this.ownerDocument,writable:!1,enumerable:!1},[w]:{value:this,writable:!1,enumerable:!1},namespaceURI:{value:r,writable:!1,enumerable:!0},ownerElement:{value:this,writable:!1,enumerable:!0},localName:{value:t,writable:!1,enumerable:!0},value:{value:l,writable:!1,enumerable:!0},prefix:{value:s,writable:!1,enumerable:!0}});e.push(n)}return e}constructor(){if(super(),!this[m])throw Error("unexpected element without known tag name");this[p]&&(this[f]=[!1,!1,!1])}getAttribute(e){const t=this[E];if(!t)return null;const r=t.get("")?.get(e);if(void 0!==r)return r[0];const n=e.indexOf(":"),l=-1!==n?e.substring(0,n):null,s=-1!==n?e.substring(n+1):e;for(const e of t.values()){const t=e.get(s);if(void 0!==t&&t[1]===l)return t[0]}return null}getAttributeNS(e,t){null===e&&(e="");const r=this[E],n=r?.get(e)?.get(t);return void 0===n?null:n[0]}setAttribute(e,t){let r=this[E];r||(r=new Map,this[E]=r);let n=r.get("");n||(n=new Map,r.set("",n)),n.set(e,[t,null])}setAttributeNS(e,t,r){null===e&&(e="");let n=this[E];n||(n=new Map,this[E]=n);let l=n.get(e);l||(l=new Map,n.set(e,l));const s=t.indexOf(":"),o=-1!==s?t.substring(0,s):null,i=-1!==s?t.substring(s+1):t,u=l.get(i);u?u[0]=r:l.set(i,[r,o])}removeAttribute(e){const t=this[E];if(!t)return;const r=t.get("")?.delete(e);if(r)return;const n=e.indexOf(":"),l=-1!==n?e.substring(0,n):null,s=-1!==n?e.substring(n+1):e;for(const e of t.values()){const t=e.get(s);if(void 0!==t&&t[1]===l)return void e.delete(s)}}removeAttributeNS(e,t){null===e&&(e="");const r=this[E];r?.get(e)?.delete(t)}remove(){const e=this.parentNode;e&&e.removeChild(this)}}const D=Symbol("element");function T(e){var t,r;return e[D]=(r=class DOMElement extends Element{constructor(){super(),this[t]=e}},t=u,r),Object.defineProperties(e[D].prototype,{[u]:{value:e,writable:!0,enumerable:!1},[w]:{value:null,writable:!0,enumerable:!1},[y]:{value:null,writable:!0,enumerable:!1},[v]:{value:null,writable:!0,enumerable:!1}}),e}const P=Symbol("defNs"),_=[["xmlns","http://www.w3.org/2000/xmlns/"],["xml","http://www.w3.org/XML/1998/namespace"]];class Document extends ParentNode{get Element(){return this[D]}get isConnected(){return!0}get customElements(){let e=this[i];return e||(this[i]=e=new CustomElementRegistry),e}get nodeType(){return 9}get nodeName(){return"#document"}get documentElement(){return this.firstElementChild}constructor(){super()}createElement(e){const t=this[P]?.get("")||null,r=this[i]?.get(e);return r?new r:(t&&"http://www.w3.org/1999/xhtml"===t&&(e=e.toLowerCase()),Object.create(this[D].prototype,{[m]:{value:e,writable:!1,enumerable:!1},[b]:{value:null,writable:!1,enumerable:!1},[d]:{value:t,writable:!1,enumerable:!0}}))}createElementNS(e,t){if(!e)return this.createElement(t);const r=t.indexOf(":"),n=-1!==r?t.substring(0,r):null,l=-1!==r?t.substring(r+1):t;return Object.create(this[D].prototype,{[m]:{value:l,writable:!1,enumerable:!1},[b]:{value:n,writable:!1,enumerable:!1},[d]:{value:e,writable:!1,enumerable:!0}})}createTextNode(e){return Object.create(Text.prototype,{[u]:{value:this,writable:!0,enumerable:!1},[w]:{value:null,writable:!0,enumerable:!1},[y]:{value:null,writable:!0,enumerable:!1},[v]:{value:null,writable:!0,enumerable:!1},[N]:{value:e.toString(),writable:!0,enumerable:!1}})}createComment(e){return Object.create(Comment.prototype,{[u]:{value:this,writable:!0,enumerable:!1},[w]:{value:null,writable:!0,enumerable:!1},[y]:{value:null,writable:!0,enumerable:!1},[v]:{value:null,writable:!0,enumerable:!1},[N]:{value:e.toString(),writable:!0,enumerable:!1}})}createCDATASection(e){return Object.create(CDATASection.prototype,{[u]:{value:this,writable:!0,enumerable:!1},[w]:{value:null,writable:!0,enumerable:!1},[y]:{value:null,writable:!0,enumerable:!1},[v]:{value:null,writable:!0,enumerable:!1},[N]:{value:e.toString(),writable:!0,enumerable:!1}})}createProcessingInstruction(e,t){return Object.create(ProcessingInstruction.prototype,{[u]:{value:this,writable:!0,enumerable:!1},[w]:{value:null,writable:!0,enumerable:!1},[y]:{value:null,writable:!0,enumerable:!1},[v]:{value:null,writable:!0,enumerable:!1},[C]:{value:e.toString(),writable:!0,enumerable:!1},[N]:{value:t.toString(),writable:!0,enumerable:!1}})}createDocumentFragment(){return Object.create(DocumentFragment.prototype,{[u]:{value:this,writable:!0,enumerable:!1},[w]:{value:null,writable:!1,enumerable:!1},[y]:{value:null,writable:!1,enumerable:!1},[v]:{value:null,writable:!1,enumerable:!1}})}getElementById(e){return this.querySelector("#"+e)}}function j(){return T(Object.create(class DOMDocument extends Document{constructor(){super()}}.prototype,{[u]:{value:null,writable:!0,enumerable:!1},[P]:{value:new Map(_),writable:!0,enumerable:!1}}))}function A(e=""){const t=Object.create(class HTMLDocument extends Document{get body(){for(const e of this.documentElement.children)if("body"==e.localName)return e;return null}get head(){for(const e of this.documentElement.children)if("head"==e.localName)return e;return null}constructor(){super()}}.prototype,{[u]:{value:null,writable:!0,enumerable:!1},[P]:{value:new Map([..._,["","http://www.w3.org/1999/xhtml"]]),writable:!0,enumerable:!1}});T(t),Object.defineProperty(t.Element.prototype,"className",{get(){return this.getAttribute("class")||""},set(e){this.setAttribute("class",e.trim())}});{t.appendChild(I("html"));const r=t.createElement("html");r.appendChild(t.createElement("head")).appendChild(t.createElement("title")).textContent=e,r.appendChild(t.createElement("body")),t.appendChild(r)}return t}class DocumentType extends Node{get nodeType(){return 10}get nodeName(){return this.name}get internalSubset(){return null}set textContent(e){}get textContent(){return null}constructor(){throw super(),Error("Constructor should never be called")}}function I(e,t="",r=""){return Object.create(DocumentType.prototype,{[u]:{value:null,writable:!0,enumerable:!1},name:{value:e,writable:!1},publicId:{value:t,writable:!1},systemId:{value:r,writable:!1},[w]:{value:null,writable:!0,enumerable:!1},[y]:{value:null,writable:!0,enumerable:!1},[v]:{value:null,writable:!0,enumerable:!1}})}export{CDATASection,CharacterData,Comment,Document,DocumentFragment,DocumentType,Element,Node,ProcessingInstruction,Text,j as createDocument,I as createDocumentType,A as createHTMLDocument};
//# sourceMappingURL=fallback-dom.js.map