-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdry-html.js
2 lines (2 loc) · 3.72 KB
/
dry-html.js
1
2
/*! (c) Andrea Giammarchi - ISC */
const t=(()=>{const t="DOMContentLoaded",e=new WeakMap,n=[],s=t=>{do{if(t.nextSibling)return!0}while(t=t.parentNode);return!1},i=()=>{n.splice(0).forEach((t=>{!0!==e.get(t[0])&&(e.set(t[0],!0),t[0][t[1]]())}))};document.addEventListener(t,i);class o extends HTMLElement{static withParsedCallback(o,r="parsed"){const{prototype:c}=o,{connectedCallback:a}=c,l=r+"Callback",u=(e,n,s,i)=>{n.disconnect(),s.removeEventListener(t,i),h(e)},h=t=>{n.length||requestAnimationFrame(i),n.push([t,l])};return Object.defineProperties(c,{connectedCallback:{configurable:!0,writable:!0,value(){if(a&&a.apply(this,arguments),l in this&&!e.has(this)){const n=this,{ownerDocument:i}=n;if(e.set(n,!1),"complete"===i.readyState||s(n))h(n);else{const e=()=>u(n,o,i,e);i.addEventListener(t,e);const o=new MutationObserver((()=>{s(n)&&u(n,o,i,e)}));o.observe(n.parentNode,{childList:!0,subtree:!0})}}}},[r]:{configurable:!0,get(){return!0===e.get(this)}}}),o}}return o.withParsedCallback(o)})(),e="loaded",n="onError";class s extends t{#t;#e;#n;constructor(t){super(),this.#t=t||{},this[n]=!1,this[e]=!1}#s(t,e){const n=this.#e;if(n)return t&&(this.#e=null),e?n.textContent:n}#i(t){this[e]=!0,this[n]=!!t,this[n]&&(this.innerHTML=this.#t.onErrorHtml||""),this.hasAttribute("level-up")&&this.replaceWith(...this.children),this.dispatchEvent(new Event("load"))}originalFragment(t=!0){return this.#s(t,!1)}originalText(t=!0){return this.#s(t,!0)}parsedCallback(){const t=this,e=e=>t.#i(e);this.#n=this.querySelectorAll("slot"),this.#n.forEach((t=>t.remove())),this.#e=document.createDocumentFragment(),this.#e.append(...this.childNodes),this.innerHTML=this.#t.onLoadHtml||"",this.init?"AsyncFunction"===this.init.constructor.name?this.init().then((()=>e())).catch((t=>{e(new Error("m-element error",{cause:t}))})):(this.init(),e()):e()}getSlotByName(t){return[...this.#n].filter((e=>t&&e.name===t))[0]}}const i=("dry-"+Math.random()).replace(".","");let o=1;const r=()=>i+o++,c="#inner#",a=/\{(?<content>t-\w[\-\w]+)\}/;function l(t){const e=document.getElementById(t),n=e.getAttribute("class"),s=e.content.cloneNode(!0),i=function(t){const e=t.querySelectorAll("*"),n=new Map;for(const t of e){const e=t.attributes;for(let s=0,i=e.length;s<i;s++){const{nodeName:i,nodeValue:o}=e.item(s),r=o.match(a);if(r){const e=d(t);n.get(e)||n.set(e,[]);const s="on"===i.substring(0,2)?{type:i.substring(2)}:null;n.get(e).push([i,r.groups.content,s])}}}return n}(s);var o;o=i,f(s,(function(t){if(0===t.childElementCount){const e=t.textContent.match(a);if(e){const n=d(t);o.get(n)||o.set(n,[]),o.get(n).push([c,e.groups.content])}}}));const r=new Set;for(const[,t]of i)for(const[e,n,s]of t)r.add(n);return{refClone:s,refData:i,attributes:[...r],tClass:n}}function u(t){if(!function(t){if(!t)return!1;const e=t.indexOf("-");return 0!==e&-1!==e}(t))return;const{refClone:e,refData:n,tClass:i}=l(t);customElements.define(t,class extends s{constructor(){super()}init(){this.style.display="block",this.hasAttribute("class")||this.setAttribute("class",i),this.tRefs=structuredClone(n),this.append(e.cloneNode(!0)),function(t){const e=[];for(const n of t.tRefs){const[s,i]=n,o=t.querySelector("#"+s);for(const[e,n,s]of i){h(o,e,t.getAttribute(n))}e.push([o,i,s])}for(const[n,s,i]of e){const e=r();n.setAttribute("id",e),t.tRefs.set(e,s),t.tRefs.delete(i)}}(this)}})}function h(t,e,n){e===c?t.innerText=n:"class"===e?(t.classList.remove(...t.classList),t.classList.add(n)):t.setAttribute(e,n)}function d(t){let e=t.getAttribute("id");return e||(e=r(),t.setAttribute("id",e)),e}function f(t,e){e(t),t.firstElementChild&&f(t.firstElementChild,e),t.nextElementSibling&&f(t.nextElementSibling,e)}document.addEventListener("DOMContentLoaded",(function(){for(const t of[...document.getElementsByTagName("template")])u(t.id)}));