UNPKG

2.31 kBJavaScriptView Raw
1"use strict"
2Object.defineProperty(exports,"__esModule",{value:!0})
3exports.fn=exports.params=exports.description=exports.active=exports.type=exports.name=void 0
4var e=require("./_collections")
5const t=new Set(e.referencesProps)
6exports.name="cleanupIDs"
7exports.type="full"
8exports.active=!0
9exports.description="removes unused IDs and minifies used"
10exports.params={remove:!0,minify:!0,prefix:"",preserve:[],preservePrefixes:[],force:!1}
11const r=/\burl\(("|')?#(.+?)\1\)/
12const n=/^#(.+?)$/
13const s=/(\w+)\./
14const o=["style","script"]
15const i=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
16const c=i.length-1
17exports.fn=function(e,i){let c
18let l
19const p=new Map
20const u=new Map
21let v=!1
22const m=new Set(Array.isArray(i.preserve)?i.preserve:i.preserve?[i.preserve]:[])
23const x=new Set(Array.isArray(i.preservePrefixes)?i.preservePrefixes:i.preservePrefixes?[i.preservePrefixes]:[])
24e=function e(c){for(let f=0;f<c.content.length&&!v;f++){const a=c.content[f]
25if(!i.force){const e=Boolean(a.content)
26if(a.isElem(o)&&e){v=!0
27continue}if(a.isElem("svg")){let e=!0
28for(let t=0;t<a.content.length;t++)if(!a.content[t].isElem("defs")){e=!1
29break}if(e)break}}a.isElem()&&a.eachAttr((function(e){let o
30let i
31if("id"!==e.name){t.has(e.name)&&(i=e.value.match(r))?o=i[2]:("href"===e.local&&(i=e.value.match(n))||"begin"===e.name&&(i=e.value.match(s)))&&(o=i[1])
32if(o){const t=u.get(o)||[]
33t.push(e)
34u.set(o,t)}}else{o=e.value
35p.has(o)?a.removeAttr("id"):p.set(o,a)}}))
36a.content&&e(a)}return c}(e)
37if(v)return e
38const h=e=>m.has(e)||function(e,t){if(!t)return!1
39for(const r of e)if(t.startsWith(r))return!0
40return!1}(x,e)
41for(const e of u){const t=e[0]
42if(p.has(t)){if(i.minify&&!h(t)){do{l=a(c=f(c),i)}while(h(l))
43p.get(t).attr("id").value=l
44for(const r of e[1])r.value=r.value.includes("#")?r.value.replace("#"+t,"#"+l):r.value.replace(t+".",l+".")}p.delete(t)}}if(i.remove)for(const e of p)h(e[0])||e[1].removeAttr("id")
45return e}
46function f(e){if(!e)return[0]
47e[e.length-1]++
48for(let t=e.length-1;t>0;t--)if(e[t]>c){e[t]=0
49void 0!==e[t-1]&&e[t-1]++}if(e[0]>c){e[0]=0
50e.unshift(0)}return e}function a(e,t){return t.prefix+e.map((e=>i[e])).join("")}
\No newline at end of file