site/node_modules/micromorph/dist/nav.js
2024-10-14 08:09:33 +02:00

2 lines
5 KiB
JavaScript

var R=(e,t,n)=>{e.setAttribute(t,new URL(e.getAttribute(t),n).pathname)};function x(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}`],D=N("Name"),M=N("Type"),L=N("Value");function I(e,t){if(e.attributes.length===0&&t.attributes.length===0)return[];let n=[],r=new Map,i=new Map;for(let o of e.attributes)r.set(o.name,o.value);for(let o of t.attributes){let s=r.get(o.name);o.value===s?r.delete(o.name):(typeof s<"u"&&r.delete(o.name),i.set(o.name,o.value))}for(let o of r.keys())n.push({type:5,name:o});for(let[o,s]of i.entries())n.push({type:4,name:o,value:s});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 g(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 u(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,o=new Map;for(let s of e.children)r.set(g(s),s);for(let s of t.children){let c=g(s),a=r.get(c);a?y(s,!1)!==y(a,!1)&&i.set(c,u(s)):o.set(c,u(s)),r.delete(c)}for(let s of e.childNodes){if(s.nodeType===1){let c=g(s);if(r.has(c)){n.push({type:1});continue}else if(i.has(c)){let a=i.get(c);n.push({type:3,attributes:I(s,a),children:P(s,a)});continue}}n.push(void 0)}for(let s of o.values())n.push({type:0,node:u(s)});return n}function P(e,t){let n=[],r=Math.max(e.childNodes.length,t.childNodes.length);for(let i=0;i<r;i++){let o=e.childNodes.item(i),s=t.childNodes.item(i);n[i]=A(o,s)}return n}function A(e,t){if(!e)return{type:0,node:u(t)};if(!t)return{type:1};if(M(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(D(e,t)){let n=e.tagName==="HEAD"?V:P;return{type:3,attributes:I(e,t),children:n(e,t)}}return{type:2,node:u(t)}}else return e.nodeType===9?A(e.documentElement,t.documentElement):L(e,t)?void 0:{type:2,value:t.nodeValue}}return{type:2,node:u(t)}}function Y(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 v(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:o}=t;if(typeof o=="string"){r.nodeValue=o;return}r.replaceWith(i);return}case 3:{if(!r)return;let{attributes:i,children:o}=t;Y(r,i);let s=Array.from(r.childNodes);await Promise.all(o.map((c,a)=>v(r,c,s[a])));return}}}function w(e,t){let n=A(e,t);return v(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 S=()=>{},C,d=document.createElement("route-announcer");function q(e={}){return typeof window<"u"&&"navigation"in window&&navigation.addEventListener("navigate",t=>{if(!t.canTransition||t.hashChange||t.downloadRequest!==null)return;let n=new URL(location.toString()),r=new URL(t.destination.url);if(n.toString()===r.toString()){(e.scrollToTop??!0)&&window.scrollTo({top:0});return}let{beforeDiff:i=S,afterDiff:o=S}=e;C=C||new DOMParser;async function s(){let c=await fetch(r.toString()).then(m=>m.text()),a=C.parseFromString(c,"text/html");x(a,r),await i(a);let l=a.querySelector("title")?.textContent;if(l)document.title=l;else{let m=document.querySelector("h1");l=m?.innerText??m?.textContent??r.pathname}d.textContent!==l&&(d.textContent=l),d.dataset.persist="",a.body.appendChild(d),await w(document,a),(e.scrollToTop??!0)&&(window.scrollTo({top:0}),document.activeElement?.closest("[data-persist]")||document.body.focus()),await o(),delete d.dataset.persist}t.intercept(document.startViewTransition(()=>s()))}),new class{go(n){let r=new URL(n,window.location.toString());return navigation.navigate(r)}back(){return navigation.back()}forward(){return navigation.forward()}}}export{q as default};