Files
fuwari/_astro/SwupHeadPlugin.d6nb3Z__.js
2026-01-08 18:42:34 +00:00

2 lines
2.5 KiB
JavaScript

import{e as $}from"./index.modern.BC8Oj8jT.js";function w(){return w=Object.assign?Object.assign.bind():function(r){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var i in t)({}).hasOwnProperty.call(t,i)&&(r[i]=t[i])}return r},w.apply(null,arguments)}function A(r){return r.localName!=="title"&&!r.matches("[data-swup-theme]")}function P(r,n){return r.outerHTML===n.outerHTML}function x(r,n=[]){const t=Array.from(r.attributes);return n.length?t.filter(({name:i})=>n.some(f=>f instanceof RegExp?f.test(i):i===f)):t}function j(r){return r.matches("link[rel=stylesheet][href]")}class B extends ${constructor(n={}){var t;super(),t=this,this.name="SwupHeadPlugin",this.requires={swup:">=4.6"},this.defaults={persistTags:!1,persistAssets:!1,awaitAssets:!1,attributes:["lang","dir"],timeout:3e3},this.options=void 0,this.updateHead=async function(i,{page:{}}){const{awaitAssets:f,attributes:y,timeout:H}=t.options,T=i.to.document,{removed:E,added:b}=function(s,m,{shouldPersist:c=()=>!1}={}){const a=Array.from(s.children),o=Array.from(m.children),d=(h=a,o.reduce((e,u,g)=>(h.some(v=>P(u,v))||e.push({el:u,index:g}),e),[]));var h;const p=function(e,u){return e.reduce((g,v)=>(u.some(O=>P(v,O))||g.push({el:v}),g),[])}(a,o);p.reverse().filter(({el:e})=>A(e)).filter(({el:e})=>!c(e)).forEach(({el:e})=>s.removeChild(e));const l=d.filter(({el:e})=>A(e)).map(e=>{let u=e.el.cloneNode(!0);return s.insertBefore(u,s.children[(e.index||0)+1]||null),w({},e,{el:u})});return{removed:p.map(({el:e})=>e),added:l.map(({el:e})=>e)}}(document.head,T.head,{shouldPersist:s=>t.isPersistentTag(s)});if(t.swup.log(`Removed ${E.length} / added ${b.length} tags in head`),y!=null&&y.length&&function(s,m,c=[]){const a=new Set;for(const{name:o,value:d}of x(m,c))s.setAttribute(o,d),a.add(o);for(const{name:o}of x(s,c))a.has(o)||s.removeAttribute(o)}(document.documentElement,T.documentElement,y),f){const s=function(m,c=0){return m.filter(j).map(a=>function(o,d=0){let h;const p=l=>{o.sheet?l():h=setTimeout(()=>p(l),10)};return new Promise(l=>{p(()=>l(o)),d>0&&setTimeout(()=>{h&&clearTimeout(h),l(o)},d)})}(a,c))}(b,H);s.length&&(t.swup.log(`Waiting for ${s.length} assets to load`),await Promise.all(s))}},this.options=w({},this.defaults,n),this.options.persistAssets&&!this.options.persistTags&&(this.options.persistTags="link[rel=stylesheet], script[src], style")}mount(){this.before("content:replace",this.updateHead)}isPersistentTag(n){const{persistTags:t}=this.options;return typeof t=="function"?t(n):typeof t=="string"&&t.length>0?n.matches(t):!!t}}export{B as default};