site/node_modules/micromorph/dist/spa.js

2 lines
5.6 KiB
JavaScript
Raw Permalink Normal View History

2024-10-14 06:09:33 +00:00
var R=(e,t,n)=>{e.setAttribute(t,new URL(e.getAttribute(t),n).pathname)};function C(e,t){e.querySelectorAll('[href^="./"], [href^="../"]').forEach(n=>R(n,"href",t)),e.querySelectorAll('[src^="./"], [src^="../"]').forEach(n=>R(n,"src",t))}var N=e=>(t,n)=>t[`node${e}`]===n[`node${e}`],x=N("Name"),D=N("Type"),M=N("Value");function L(e,t){if(e.attributes.length===0&&t.attributes.length===0)return[];let n=[],r=new Map,i=new Map;for(let s of e.attributes)r.set(s.name,s.value);for(let s of t.attributes){let o=r.get(s.name);s.value===o?r.delete(s.name):(typeof o<"u"&&r.delete(s.name),i.set(s.name,s.value))}for(let s of r.keys())n.push({type:5,name:s});for(let[s,o]of i.entries())n.push({type:4,name:s,value:o});return n}function y(e,t=!0){let n=`${e.localName}`;for(let{name:r,value:i}of e.attributes)t&&r.startsWith("data-")||(n+=`[${r}=${i}]`);return n+=e.innerHTML,n}function w(e){switch(e.tagName){case"BASE":case"TITLE":return e.localName;case"META":{if(e.hasAttribute("name"))return`meta[name="${e.getAttribute("name")}"]`;if(e.hasAttribute("property"))return`meta[name="${e.getAttribute("property")}"]`;break}case"LINK":{if(e.hasAttribute("rel")&&e.hasAttribute("href"))return`link[rel="${e.getAttribute("rel")}"][href="${e.getAttribute("href")}"]`;if(e.hasAttribute("href"))return`link[href="${e.getAttribute("href")}"]`;break}}return y(e)}function k(e){let[t,n=""]=e.split("?");return`${t}?t=${Date.now()}&${n.replace(/t=\d+/g,"")}`}function l(e){if(e.nodeType===1&&e.hasAttribute("data-persist"))return e;if(e.nodeType===1&&e.localName==="script"){let t=document.createElement("script");for(let{name:n,value:r}of e.attributes)n==="src"&&(r=k(r)),t.setAttribute(n,r);return t.innerHTML=e.innerHTML,t}return e.cloneNode(!0)}function V(e,t){if(e.children.length===0&&t.children.length===0)return[];let n=[],r=new Map,i=new Map,s=new Map;for(let o of e.children)r.set(w(o),o);for(let o of t.children){let a=w(o),c=r.get(a);c?y(o,!1)!==y(c,!1)&&i.set(a,l(o)):s.set(a,l(o)),r.delete(a)}for(let o of e.childNodes){if(o.nodeType===1){let a=w(o);if(r.has(a)){n.push({type:1});continue}else if(i.has(a)){let c=i.get(a);n.push({type:3,attributes:L(o,c),children:I(o,c)});continue}}n.push(void 0)}for(let o of s.values())n.push({type:0,node:l(o)});return n}function I(e,t){let n=[],r=Math.max(e.childNodes.length,t.childNodes.length);for(let i=0;i<r;i++){let s=e.childNodes.item(i),o=t.childNodes.item(i);n[i]=h(s,o)}return n}function h(e,t){if(!e)return{type:0,node:l(t)};if(!t)return{type:1};if(D(e,t)){if(e.nodeType===3){let n=e.nodeValue,r=t.nodeValue;if(n.trim().length===0&&r.trim().length===0)return}if(e.nodeType===1){if(x(e,t)){let n=e.tagName==="HEAD"?V:I;return{type:3,attributes:L(e,t),children:n(e,t)}}return{type:2,node:l(t)}}else return e.nodeType===9?h(e.documentElement,t.documentElement):M(e,t)?void 0:{type:2,value:t.nodeValue}}return{type:2,node:l(t)}}function S(e,t){if(t.length!==0)for(let{type:n,name:r,value:i}of t)n===5?e.removeAttribute(r):n===4&&e.setAttribute(r,i)}async function O(e,t,n){if(!t)return;let r;switch(e.nodeType===9?(e=e.documentElement,r=e):n?r=n:r=e,t.type){case 0:{let{node:i}=t;e.appendChild(i);return}case 1:{if(!r)return;e.removeChild(r);return}case 2:{if(!r)return;let{node:i,value:s}=t;if(typeof s=="string"){r.nodeValue=s;return}r.replaceWith(i);return}case 3:{if(!r)return;let{attributes:i,children:s}=t;S(r,i);let o=Array.from(r.childNodes);await Promise.all(s.map((a,c)=>O(r,a,o[c])));return}}}function T(e,t){let n=h(e,t);return O(e,n)}if(!customElements.get("route-announcer")){let e={"aria-live":"assertive","aria-atomic":"true",style:"position: absolute; left: 0; top: 0; clip: rect(0 0 0 0); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px"};customElements.define("route-announcer",class extends HTMLElement{constructor(){super()}connectedCallback(){for(let[n,r]of Object.entries(e))this.setAttribute(n,r)}})}var d=document.createElement("route-announcer"),Y=e=>e?.nodeType===1,q=e=>{try{let t=new URL(e);if(window.location.origin===t.origin)return t.pathname===window.location.pathname?!t.hash:!0}ca