{"version":3,"file":"index.cjs","sources":["../src/classes.ts","../src/attributes.ts","../src/index.ts"],"sourcesContent":["export function updateClassNames(el: HTMLElement, newEl: HTMLElement, { prefix = '' } = {}): void {\n\tconst remove = [...el.classList].filter((className) => isValid(className, { prefix }));\n\tconst add = [...newEl.classList].filter((className) => isValid(className, { prefix }));\n\tel.classList.remove(...remove);\n\tel.classList.add(...add);\n}\n\nfunction isValid(className: string, { prefix = '' } = {}): boolean {\n\treturn !!className && className.startsWith(prefix);\n}\n","export function updateAttributes(\n\ttarget: Element,\n\tsource: Element,\n\tfilters: (string | RegExp)[] = []\n): void {\n\tconst keep = new Set<string>();\n\n\tfor (const { name, value } of getAttributes(source, filters)) {\n\t\ttarget.setAttribute(name, value);\n\t\tkeep.add(name);\n\t}\n\n\tfor (const { name } of getAttributes(target, filters)) {\n\t\tif (!keep.has(name)) {\n\t\t\ttarget.removeAttribute(name);\n\t\t}\n\t}\n}\n\nfunction getAttributes(el: Element, filters: (string | RegExp)[] = []): Attr[] {\n\tconst all = Array.from(el.attributes);\n\tif (!filters.length) return all;\n\n\treturn all.filter(({ name }) =>\n\t\tfilters.some((pattern) =>\n\t\t\tpattern instanceof RegExp ? pattern.test(name) : name === pattern\n\t\t)\n\t);\n}\n","import { Handler } from 'swup';\nimport Plugin from '@swup/plugin';\nimport { updateClassNames } from './classes.js';\nimport { updateAttributes } from './attributes.js';\n\ntype Options = {\n\t/** If set, only classes beginning with this string will be added/removed. */\n\tprefix: string;\n\t/** Additional body attributes to update, besides the classname. */\n\tattributes: (string | RegExp)[];\n};\n\nexport default class SwupBodyClassPlugin extends Plugin {\n\tname = 'SwupBodyClassPlugin';\n\n\trequires = { swup: '>=4.6' };\n\n\tdefaults: Options = {\n\t\tprefix: '',\n\t\tattributes: []\n\t};\n\toptions: Options;\n\n\tconstructor(options: Partial<Options> = {}) {\n\t\tsuper();\n\t\tthis.options = { ...this.defaults, ...options };\n\t}\n\n\tmount() {\n\t\tthis.on('content:replace', this.update);\n\t}\n\n\tprotected update: Handler<'content:replace'> = (visit) => {\n\t\tconst { prefix, attributes } = this.options;\n\t\tupdateClassNames(document.body, visit.to.document!.body, { prefix });\n\t\tif (attributes?.length) {\n\t\t\tupdateAttributes(document.body, visit.to.document!.body, attributes);\n\t\t}\n\t};\n}\n"],"names":["isValid","className","prefix","startsWith","getAttributes","el","filters","all","Array","from","attributes","length","filter","name","some","pattern","RegExp","test","Plugin","constructor","options","super","this","requires","swup","defaults","update","visit","newEl","remove","classList","add","updateClassNames","document","body","to","target","source","keep","Set","value","setAttribute","has","removeAttribute","updateAttributes","mount","on"],"mappings":"sHAOA,SAASA,EAAQC,GAAmBC,OAAEA,EAAS,IAAO,CAAA,GACrD,QAASD,GAAaA,EAAUE,WAAWD,EAC5C,CCUA,SAASE,EAAcC,EAAaC,EAA+B,IAClE,MAAMC,EAAMC,MAAMC,KAAKJ,EAAGK,YAC1B,OAAKJ,EAAQK,OAENJ,EAAIK,OAAO,EAAGC,UACpBP,EAAQQ,KAAMC,GACbA,aAAmBC,OAASD,EAAQE,KAAKJ,GAAQA,IAASE,IAJhCR,CAO7B,gBChBqB,cAA4BW,EAAM,QAWtDC,WAAAA,CAAYC,EAA4B,CAAA,GACvCC,QAAQC,KAXTT,KAAO,sBAAqBS,KAE5BC,SAAW,CAAEC,KAAM,cAEnBC,SAAoB,CACnBvB,OAAQ,GACRQ,WAAY,IACZY,KACDF,aAAO,EAAAE,KAWGI,OAAsCC,IAC/C,MAAMzB,OAAEA,EAAMQ,WAAEA,GAAeY,KAAKF,SFjCtB,SAAiBf,EAAiBuB,GAAoB1B,OAAEA,EAAS,IAAO,CAAE,GACzF,MAAM2B,EAAS,IAAIxB,EAAGyB,WAAWlB,OAAQX,GAAcD,EAAQC,EAAW,CAAEC,YACtE6B,EAAM,IAAIH,EAAME,WAAWlB,OAAQX,GAAcD,EAAQC,EAAW,CAAEC,YAC5EG,EAAGyB,UAAUD,UAAUA,GACvBxB,EAAGyB,UAAUC,OAAOA,EACr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