-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
spect.min.js
1 lines (1 loc) · 4.39 KB
/
spect.min.js
1
let e=0,t={},s={},i={},a={},n={},l=new WeakMap,h=document,r="undefined"!=typeof AnimationEvent,o=h.head.appendChild(h.createElement("style")),d=Symbol();function c(e,t,s){if(e&&e.raw)return new m(null,String.raw.apply(null,arguments));if("string"==typeof e)return new m(null,e,t);if(!t||"function"==typeof t){s=t;let i=e;i||(i=[]),i.nodeType&&(i=[i]);const a=new m(null,null,s);return i.forEach((e=>a.add(e))),a}return new m(e,t,s)}class m extends Array{#e;#t;#s;#i;#a;#n;#l;#h;constructor(l,c,m){if("number"==typeof l)return Array(l);if(super(),this.#e=new WeakMap,this.#t=new WeakSet,this.#s=new WeakMap,this.#i=l,this.#a=m,this[d]=Object.getPrototypeOf(this),!c)return;(l||h).querySelectorAll(c).forEach((e=>{this[d].add.call(this,e)}));const f=/(?:#([\w:-]+)|\[\s*name=['"]?([\w:-]+)['"]?\s*\]|\.([\w:-]+)|([\*\w:-]+))(\[[^\]]+\]|\.[\w:-]+)*$/;if(this.#n=c.split(/\s*,\s*/).map((e=>{const n=(e=new String(e)).match(f);if(e.filter=e,!n)return e;let[l,h,r,o,d,c]=n;return h?(t[e.id=h]=t[h]||[]).push(this):r?(a[e.name=r]=a[r]||[]).push(this):o?(s[e.class=o]=s[o]||[]).push(this):d&&(e.tag=d=d.toUpperCase(),i[d]=i[d]||[]).push(this),c?e.filter=e.slice(0,n.index)+e.slice(-c.length):n.index||delete e.filter,e})),this.#h=this.#n.some((e=>e.filter)),!r||!this.#n.every((e=>e.tag&&!e.filter))){let t=n[this.#n];if(!t){const{sheet:s}=o,{cssRules:i}=s;t=n[this.#n]=[],t.id=`⬡-${(e++).toString(36)}`,s.insertRule(`@keyframes ${t.id}{}`,i.length),s.insertRule(`${this.#n.map((e=>e+`:not(.${t.id})`))}{animation:${t.id}}`,i.length),s.insertRule(`.${t.id}{animation:${t.id}}`,i.length),s.insertRule(`${this.#n.map((e=>e+`.${t.id}`))}{animation:unset;animation:revert}`,i.length),t.rules=[].slice.call(i,-4),t.onanim=e=>{if(e.animationName!==t.id)return;e.stopPropagation(),e.preventDefault();let{target:s}=e;s.classList.contains(t.id)?(s.classList.remove(t.id),t.forEach((e=>e.delete(s)))):(s.classList.add(t.id),t.forEach((e=>e[d].add.call(e,s,!1))))},h.addEventListener("animationstart",t.onanim,!0)}this.#l=t.id,t.push(this)}}add(e,t=this.#h){e&&(this.#e.has(e)||t&&!e.matches(this.#n)||(!this.#i||this.#i!==e&&this.#i.contains(e))&&(this.push(e),this.#e.set(e,[e.id,e.name]),e.name&&(this[e.name]=e),e.id&&(this[e.id]=e),this.#t.has(e)&&this.#t.delete(e),l.has(e)&&l.get(e).has(this)||(l.has(e)||l.set(e,new Set),l.get(e).add(this),e.classList.add("⬡"),this.#s.set(e,this.#a?.(e)))))}delete(e,t=!1){if(!this.#e.has(e))return;this.length&&this.splice(this.indexOf(e>>>0,1),1);const[s,i]=this.#e.get(e);i&&delete this[i],s&&delete this[s],this.#e.delete(e),this.#t.add(e);const a=()=>{if(!this.#t.has(e)||!this.#e)return;if(this.#t.delete(e),!l.has(e))return;const t=this.#s.get(e);t&&(t.call?t(e):t.then&&t.then((e=>e&&e.call&&e()))),this.#s.delete(e),l.get(e).delete(this),l.get(e).size||(l.delete(e),e.classList.remove("⬡"))};t?a():requestAnimationFrame(a)}item(e){return e<0?this[this.length+e]:this[e]}namedItem(e){return this[e]}has(e){return this.#e.has(e)}dispose(){if(this.#n&&this.#n.forEach((({id:e,class:n,name:l,tag:h})=>{e&&t[e].splice(t[e].indexOf(this)>>>0,1),l&&a[l].splice(a[l].indexOf(this)>>>0,1),n&&s[n].splice(s[n].indexOf(this)>>>0,1),h&&i[h].splice(i[h].indexOf(this)>>>0,1)})),this.#l){const e=n[this.#n];e.splice(e.indexOf(this)>>>0,1),e.length||(h.removeEventListener("animationstart",e.onanim),delete n[this.#n],e.rules&&e.rules.forEach((e=>{let t=[].indexOf.call(o.sheet.cssRules,e);~t&&o.sheet.deleteRule(t)})))}let e=[...this];this.length=0,e.forEach((e=>this.delete(e,!0)))}[Symbol.dispose||=Symbol("dispose")](){return this.dispose}}const f=(e,t,s)=>{t&&e&&[].forEach.call(e,(e=>t.forEach((t=>t[d].add.call(t,e,s)))))},u=e=>[e.classList.contains("⬡")?e:null,...e.getElementsByClassName("⬡")].forEach((e=>l.has(e)&&l.get(e).forEach((t=>t.delete(e)))));new MutationObserver((e=>{for(let l of e){let{addedNodes:e,removedNodes:h,target:o}=l;if(!r){u(o);for(let e in n)f([o,...o.querySelectorAll(e)],n[e],!0)}h.forEach((e=>1===e.nodeType&&u(e))),e.forEach((e=>{if(1===e.nodeType&&(e.id&&t[e.id]&&f([e],t[e.id]),e.name&&a[e.name]&&f([e],a[e.name]),e.className&&e.classList.forEach((t=>f([e],s[t]))),i[e.tagName]&&f([e],i[e.tagName]),e.hasChildNodes())){if(e.getElementById)for(let s in t)f(e.getElementById(s),t[s]);for(let t in a)f(e.getElementsByName(t),a[t]);for(let t in s)f(e.getElementsByClassName(t),s[t]);for(let t in i)f(e.getElementsByTagName(t),i[t])}}))}})).observe(h,{childList:!0,subtree:!0,attributes:!r});export{m as SelectorCollection,c as default};