@asamuzakjp/dom-selector 4.1.3 → 4.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- var W=Object.create;var P=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var B=Object.getOwnPropertyNames;var j=Object.getPrototypeOf,G=Object.prototype.hasOwnProperty;var q=(E,i)=>{for(var e in i)P(E,e,{get:i[e],enumerable:!0})},U=(E,i,e,r)=>{if(i&&typeof i=="object"||typeof i=="function")for(let a of B(i))!G.call(E,a)&&a!==e&&P(E,a,{get:()=>i[a],enumerable:!(r=H(i,a))||r.enumerable});return E};var z=(E,i,e)=>(e=E!=null?W(j(E)):{},U(i||!E||!E.__esModule?P(e,"default",{value:E,enumerable:!0}):e,E)),V=E=>U(P({},"__esModule",{value:!0}),E);var Y={};q(Y,{Finder:()=>X});module.exports=V(Y);var I=z(require("is-potential-custom-element-name"),1),F=z(require("nwsapi"),1),L=require("./dom-util.js"),M=require("./matcher.js"),y=require("./parser.js"),u=require("./constant.js");const C="next",O="prev",$="all",A="first",R="lineal",D="self";class X{#a;#l;#t;#o;#f;#e;#h;#w;#c;#n;#m;#s;#d;#u;#r;#b;#i;#p;constructor(i){this.#p=i,this.#f=i.document,this.#l=new WeakMap,this.#m=new WeakMap,this.#c=(0,F.default)({document:i.document,DOMException:i.DOMException}),this.#c.configure({LOGERRORS:!1})}_onError(i){if(!this.#w)if(i instanceof DOMException||i instanceof this.#p.DOMException)if(i.name===u.NOT_SUPPORTED_ERR)this.#i&&console.warn(i.message);else throw new this.#p.DOMException(i.message,i.name);else throw i}_setup(i,e,r={}){const{noexcept:a,warn:s}=r;return this.#w=!!a,this.#i=!!s,this.#e=e,[this.#t,this.#s,this.#r]=(0,L.resolveContent)(e),this.#d=(0,L.isInShadowTree)(e),[this.#a,this.#h]=this._correspond(i),this.#b=new WeakMap,e}_correspond(i){const e=[];this.#o=!1;let r;if(this.#t){const a=this.#l.get(this.#t);if(a&&a.has(`${i}`)){const s=a.get(`${i}`);this.#o=s.descendant,r=s.ast}}if(r){const a=r.length;for(let s=0;s<a;s++)r[s].collected=!1,r[s].dir=null,r[s].filtered=!1,r[s].find=!1,e[s]=[]}else{let a;try{a=(0,y.parseSelector)(i)}catch(h){this._onError(h)}const s=(0,y.walkAST)(a);let o=!1,n=0;r=[];for(const[...h]of s){const f=[];let t=h.shift();if(t&&t.type!==u.COMBINATOR){const l=new Set;for(;t;){if(t.type===u.COMBINATOR){const[c]=h;if(c.type===u.COMBINATOR){const p=`Invalid selector ${i}`;throw new DOMException(p,u.SYNTAX_ERR)}const m=t.name;/^[\s>]$/.test(m)&&(o=!0),f.push({combo:t,leaves:(0,y.sortAST)(l)}),l.clear()}else if(t){let{name:c}=t;c&&typeof c=="string"&&(c=(0,y.unescapeSelector)(c),t.name=c,/[|:]/.test(c)&&(t.namespace=!0)),l.add(t)}if(h.length)t=h.shift();else{f.push({combo:null,leaves:(0,y.sortAST)(l)}),l.clear();break}}}r.push({branch:f,collected:!1,dir:null,filtered:!1,find:!1}),e[n]=[],n++}if(this.#t){let h;this.#l.has(this.#t)?h=this.#l.get(this.#t):h=new Map,h.set(`${i}`,{ast:r,descendant:o}),this.#l.set(this.#t,h)}this.#o=o}return[r,e]}_createTreeWalker(i){let e;return this.#b.has(i)?e=this.#b.get(i):(e=this.#f.createTreeWalker(i,u.WALKER_FILTER),this.#b.set(i,e)),e}_prepareQuerySelectorWalker(){return this.#n=this._createTreeWalker(this.#e),this.#u=!1,this.#n}_traverse(i,e=this.#r){let r=e.currentNode,a;if(r===i)a=r;else if(r.contains(i))for(r=e.nextNode();r;){if(r===i){a=r;break}r=e.nextNode()}else{if(r!==e.root)for(;r&&!(r===e.root||r===i);)r=e.parentNode();if(i?.nodeType===u.ELEMENT_NODE)for(;r;){if(r===i){a=r;break}r=e.nextNode()}else a=r}return a??null}_collectNthChild(i,e,r){const{a,b:s,reverse:o,selector:n}=i,{parentNode:h}=e,f=new Set;let t;if(n&&(this.#l.has(n)?t=this.#l.get(n):(t=(0,y.walkAST)(n),this.#l.set(n,t))),h){const l=this.#r;let c=this._traverse(h,l);c=l.firstChild();let m=0;for(;c;)m++,c=l.nextSibling();c=this._traverse(h,l);const p=new Set;if(t)for(c=l.firstChild();c;){let b;for(const _ of t)if(b=this._matchLeaves(_,c,r),!b)break;b&&p.add(c),c=l.nextSibling()}if(a===0){if(s>0&&s<=m){if(p.size){c=this._traverse(h,l),o?c=l.lastChild():c=l.firstChild();let b=0;for(;c;){if(p.has(c)){if(b===s-1){f.add(c);break}b++}o?c=l.previousSibling():c=l.nextSibling()}}else if(!n){c=this._traverse(h,l),o?c=l.lastChild():c=l.firstChild();let b=0;for(;c;){if(b===s-1){f.add(c);break}o?c=l.previousSibling():c=l.nextSibling(),b++}}}}else{let b=s-1;if(a>0)for(;b<0;)b+=a;if(b>=0&&b<m){c=this._traverse(h,l),o?c=l.lastChild():c=l.firstChild();let _=0,N=a>0?0:s-1;for(;c&&(c&&b>=0&&b<m);)p.size?p.has(c)&&(N===b&&(f.add(c),b+=a),a>0?N++:N--):_===b&&(n||f.add(c),b+=a),o?c=l.previousSibling():c=l.nextSibling(),_++}}if(o&&f.size>1){const b=[...f];return new Set(b.reverse())}}else if(e===this.#s&&a+s===1)if(t){let l;for(const c of t)if(l=this._matchLeaves(c,e,r),l)break;l&&f.add(e)}else f.add(e);return f}_collectNthOfType(i,e){const{a:r,b:a,reverse:s}=i,{localName:o,parentNode:n,prefix:h}=e,f=new Set;if(n){const t=this.#r;let l=this._traverse(n,t);l=t.firstChild();let c=0;for(;l;)c++,l=t.nextSibling();if(r===0){if(a>0&&a<=c){l=this._traverse(n,t),s?l=t.lastChild():l=t.firstChild();let m=0;for(;l;){const{localName:p,prefix:b}=l;if(p===o&&b===h){if(m===a-1){f.add(l);break}m++}s?l=t.previousSibling():l=t.nextSibling()}}}else{let m=a-1;if(r>0)for(;m<0;)m+=r;if(m>=0&&m<c){l=this._traverse(n,t),s?l=t.lastChild():l=t.firstChild();let p=r>0?0:a-1;for(;l;){const{localName:b,prefix:_}=l;if(b===o&&_===h){if(p===m&&(f.add(l),m+=r),m<0||m>=c)break;r>0?p++:p--}s?l=t.previousSibling():l=t.nextSibling()}}}if(s&&f.size>1){const m=[...f];return new Set(m.reverse())}}else e===this.#s&&r+a===1&&f.add(e);return f}_matchAnPlusB(i,e,r,a){const{nth:{a:s,b:o,name:n},selector:h}=i,f=new Map;if(n?(n==="even"?(f.set("a",2),f.set("b",0)):n==="odd"&&(f.set("a",2),f.set("b",1)),r.indexOf("last")>-1&&f.set("reverse",!0)):(typeof s=="string"&&/-?\d+/.test(s)?f.set("a",s*1):f.set("a",0),typeof o=="string"&&/-?\d+/.test(o)?f.set("b",o*1):f.set("b",0),r.indexOf("last")>-1&&f.set("reverse",!0)),/^nth-(?:last-)?child$/.test(r)){h&&f.set("selector",h);const t=Object.fromEntries(f);return this._collectNthChild(t,e,a)}else if(/^nth-(?:last-)?of-type$/.test(r)){const t=Object.fromEntries(f);return this._collectNthOfType(t,e)}return new Set}_matchDirectionPseudoClass(i,e){const r=(0,L.getDirectionality)(e);let a;return i.name===r&&(a=e),a??null}_matchLanguagePseudoClass(i,e){if(i.name===u.EMPTY)return null;const r=(0,y.unescapeSelector)(i.name);i.name=r;let a;if(r==="*")if(e.hasAttribute("lang"))e.getAttribute("lang")&&(a=e);else{let s=e.parentNode;for(;s&&s.nodeType===u.ELEMENT_NODE;){if(s.hasAttribute("lang")){s.getAttribute("lang")&&(a=e);break}s=s.parentNode}}else if(r){const s=`(?:-${u.ALPHA_NUM})*`;if(new RegExp(`^(?:\\*-)?${u.ALPHA_NUM}${s}$`,"i").test(r)){let n;if(r.indexOf("-")>-1){const[h,f,...t]=r.split("-");let l;h==="*"?l=`${u.ALPHA_NUM}${s}`:l=`${h}${s}`;const c=`-${f}${s}`,m=t.length;let p="";if(m)for(let b=0;b<m;b++)p+=`-${t[b]}${s}`;n=new RegExp(`^${l}${c}${p}$`,"i")}else n=new RegExp(`^${r}${s}$`,"i");if(e.hasAttribute("lang"))n.test(e.getAttribute("lang"))&&(a=e);else{let h=e.parentNode;for(;h&&h.nodeType===u.ELEMENT_NODE;){if(h.hasAttribute("lang")){const f=h.getAttribute("lang");n.test(f)&&(a=e);break}h=h.parentNode}}}}return a??null}_matchHasPseudoFunc(i,e,r={}){let a;if(Array.isArray(i)&&i.length){const[s]=i,{type:o}=s;let n;o===u.COMBINATOR?n=i.shift():n={name:" ",type:u.COMBINATOR};const h=[];for(;i.length;){const[l]=i,{type:c}=l;if(c===u.COMBINATOR)break;h.push(i.shift())}const f={combo:n,leaves:h};r.dir=C;const t=this._matchCombinator(f,e,r);if(t.size)if(i.length){for(const l of t)if(a=this._matchHasPseudoFunc(Object.assign([],i),l,r),a)break}else a=!0}return!!a}_matchLogicalPseudoFunc(i,e,r={}){const{astName:a="",branches:s=[],selector:o="",twigBranches:n=[]}=i;let h;if(a==="has")if(o.includes(":has("))h=null;else{let f;for(const t of s)if(f=this._matchHasPseudoFunc(Object.assign([],t),e,r),f)break;f&&(h=e)}else{const f=/^(?:is|where)$/.test(a);r.forgive=f;const t=n.length;let l;for(let c=0;c<t;c++){const m=n[c],p=m.length-1,{leaves:b}=m[p];if(l=this._matchLeaves(b,e,r),l&&p>0){let _=new Set([e]);for(let N=p-1;N>=0;N--){const k=m[N],d=[];r.dir=O;for(const w of _){const g=this._matchCombinator(k,w,r);g.size&&d.push(...g)}if(d.length)N===0?l=!0:_=new Set(d);else{l=!1;break}}}if(l)break}a==="not"?l||(h=e):l&&(h=e)}return h??null}_matchPseudoClassSelector(i,e,r={}){const{children:a,name:s}=i,{localName:o,parentNode:n}=e,{forgive:h,warn:f=this.#i}=r,t=new Set;if(u.REG_LOGICAL_PSEUDO.test(s)){let l;if(this.#l.has(i))l=this.#l.get(i);else{const m=(0,y.walkAST)(i),p=[],b=[];for(const[..._]of m){for(const w of _){const g=(0,y.generateCSS)(w);p.push(g)}const N=[],k=new Set;let d=_.shift();for(;d;)if(d.type===u.COMBINATOR?(N.push({combo:d,leaves:[...k]}),k.clear()):d&&k.add(d),_.length)d=_.shift();else{N.push({combo:null,leaves:[...k]}),k.clear();break}b.push(N)}l={astName:s,branches:m,twigBranches:b,selector:p.join(",")},this.#l.set(i,l)}const c=this._matchLogicalPseudoFunc(l,e,r);c&&t.add(c)}else if(Array.isArray(a)){const[l]=a;if(/^nth-(?:last-)?(?:child|of-type)$/.test(s))return this._matchAnPlusB(l,e,s,r);if(s==="dir"){const c=this._matchDirectionPseudoClass(l,e);c&&t.add(c)}else if(s==="lang"){const c=this._matchLanguagePseudoClass(l,e);c&&t.add(c)}else switch(s){case"current":case"nth-col":case"nth-last-col":{if(f){const c=`Unsupported pseudo-class :${s}()`;throw new DOMException(c,u.NOT_SUPPORTED_ERR)}break}case"host":case"host-context":break;default:if(!h){const c=`Unknown pseudo-class :${s}()`;throw new DOMException(c,u.SYNTAX_ERR)}}}else{const l=/^a(?:rea)?$/,c=/^(?:(?:fieldse|inpu|selec)t|button|opt(?:group|ion)|textarea)$/,m=/^(?:(?:inpu|selec)t|button|form|textarea)$/,p=/^d(?:etails|ialog)$/,b=/^(?:checkbox|radio)$/,_=/^(?:date(?:time-local)?|month|time|week)$/,N=/(?:(?:rang|tim)e|date(?:time-local)?|month|number|week)$/,k=/^(?:(?:emai|te|ur)l|number|password|search|text)$/;switch(s){case"any-link":case"link":{l.test(o)&&e.hasAttribute("href")&&t.add(e);break}case"local-link":{if(l.test(o)&&e.hasAttribute("href")){const{href:d,origin:w,pathname:g}=new URL(this.#t.URL),x=new URL(e.getAttribute("href"),d);x.origin===w&&x.pathname===g&&t.add(e)}break}case"visited":break;case"target":{const{hash:d}=new URL(this.#t.URL);e.id&&d===`#${e.id}`&&this.#t.contains(e)&&t.add(e);break}case"target-within":{const{hash:d}=new URL(this.#t.URL);if(d){const w=d.replace(/^#/,"");let g=this.#t.getElementById(w);for(;g;){if(g===e){t.add(e);break}g=g.parentNode}}break}case"scope":{this.#e.nodeType===u.ELEMENT_NODE?!this.#d&&e===this.#e&&t.add(e):e===this.#t.documentElement&&t.add(e);break}case"focus":{if(e===this.#t.activeElement){let d=e,w=!0;for(;d;){if(d.hasAttribute("hidden")){w=!1;break}else if(d.hasAttribute("style")){const{display:g,visibility:x}=d.style;if(w=!(g==="none"||x==="hidden"),!w)break}if(d.parentNode&&d.parentNode.nodeType===u.ELEMENT_NODE)d=d.parentNode;else break}w&&t.add(e)}break}case"focus-within":{let d=this.#t.activeElement,w;for(;d;){if(d===e){w=!0;break}d=d.parentNode}if(w){let g=e,x=!0;for(;g;){if(g.hasAttribute("hidden")){x=!1;break}else if(g.hasAttribute("style")){const{display:v,visibility:T}=g.style;if(x=!(v==="none"||T==="hidden"),!x)break}if(g.parentNode&&g.parentNode.nodeType===u.ELEMENT_NODE)g=g.parentNode;else break}x&&t.add(e)}break}case"open":{p.test(o)&&e.hasAttribute("open")&&t.add(e);break}case"closed":{p.test(o)&&!e.hasAttribute("open")&&t.add(e);break}case"disabled":{if(c.test(o)||(0,I.default)(o))if(e.disabled||e.hasAttribute("disabled"))t.add(e);else{let d=n;for(;d&&d.localName!=="fieldset";)d=d.parentNode;d&&n.localName!=="legend"&&d.hasAttribute("disabled")&&t.add(e)}break}case"enabled":{(c.test(o)||(0,I.default)(o))&&!(e.disabled&&e.hasAttribute("disabled"))&&t.add(e);break}case"read-only":{switch(o){case"textarea":{(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&t.add(e);break}case"input":{(!e.type||_.test(e.type)||k.test(e.type))&&(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&t.add(e);break}default:(0,L.isContentEditable)(e)||t.add(e)}break}case"read-write":{switch(o){case"textarea":{e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled")||t.add(e);break}case"input":{(!e.type||_.test(e.type)||k.test(e.type))&&!(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&t.add(e);break}default:(0,L.isContentEditable)(e)&&t.add(e)}break}case"placeholder-shown":{let d;o==="textarea"?d=e:o==="input"&&(e.hasAttribute("type")?k.test(e.getAttribute("type"))&&(d=e):d=e),d&&e.value===""&&e.hasAttribute("placeholder")&&e.getAttribute("placeholder").trim().length&&t.add(e);break}case"checked":{(e.checked&&o==="input"&&e.hasAttribute("type")&&b.test(e.getAttribute("type"))||e.selected&&o==="option")&&t.add(e);break}case"indeterminate":{if(e.indeterminate&&o==="input"&&e.type==="checkbox"||o==="progress"&&!e.hasAttribute("value"))t.add(e);else if(o==="input"&&e.type==="radio"&&!e.hasAttribute("checked")){const d=e.name;let w=e.parentNode;for(;w&&w.localName!=="form";)w=w.parentNode;w||(w=this.#t.documentElement);const g=w.getElementsByTagName("input"),x=g.length;let v;for(let T=0;T<x;T++){const S=g[T];if(S.getAttribute("type")==="radio"&&(d?S.getAttribute("name")===d&&(v=!!S.checked):S.hasAttribute("name")||(v=!!S.checked),v))break}v||t.add(e)}break}case"default":{const d=/^(?:button|reset)$/,w=/^(?:image|submit)$/;if(o==="button"&&!(e.hasAttribute("type")&&d.test(e.getAttribute("type")))||o==="input"&&e.hasAttribute("type")&&w.test(e.getAttribute("type"))){let g=e.parentNode;for(;g&&g.localName!=="form";)g=g.parentNode;if(g){const x=this.#r;let v=this._traverse(g,x);for(v=x.firstChild();v&&g.contains(v);){const T=v.localName;let S;if(T==="button"?S=!(v.hasAttribute("type")&&d.test(v.getAttribute("type"))):T==="input"&&(S=v.hasAttribute("type")&&w.test(v.getAttribute("type"))),S){v===e&&t.add(e);break}v=x.nextNode()}}}else if(o==="input"&&e.hasAttribute("type")&&b.test(e.getAttribute("type"))&&(e.checked||e.hasAttribute("checked")))t.add(e);else if(o==="option"){let g=n,x=!1;for(;g&&g.localName!=="datalist";){if(g.localName==="select"){(g.multiple||g.hasAttribute("multiple"))&&(x=!0);break}g=g.parentNode}if(x)(e.selected||e.hasAttribute("selected"))&&t.add(e);else{const v=new Set,T=this.#r;let S=this._traverse(n,T);for(S=T.firstChild();S;){if(S.selected||S.hasAttribute("selected")){v.add(S);break}S=T.nextSibling()}v.size&&v.has(e)&&t.add(e)}}break}case"valid":{if(m.test(o))e.checkValidity()&&t.add(e);else if(o==="fieldset"){const d=this.#r;let w=this._traverse(e,d);w=d.firstChild();let g;for(;w&&e.contains(w)&&!(m.test(w.localName)&&(g=w.checkValidity(),!g));)w=d.nextNode();g&&t.add(e)}break}case"invalid":{if(m.test(o))e.checkValidity()||t.add(e);else if(o==="fieldset"){const d=this.#r;let w=this._traverse(e,d);w=d.firstChild();let g;for(;w&&e.contains(w)&&!(m.test(w.localName)&&(g=w.checkValidity(),!g));)w=d.nextNode();g||t.add(e)}break}case"in-range":{o==="input"&&!(e.readonly||e.hasAttribute("readonly"))&&!(e.disabled||e.hasAttribute("disabled"))&&e.hasAttribute("type")&&N.test(e.getAttribute("type"))&&!(e.validity.rangeUnderflow||e.validity.rangeOverflow)&&(e.hasAttribute("min")||e.hasAttribute("max")||e.getAttribute("type")==="range")&&t.add(e);break}case"out-of-range":{o==="input"&&!(e.readonly||e.hasAttribute("readonly"))&&!(e.disabled||e.hasAttribute("disabled"))&&e.hasAttribute("type")&&N.test(e.getAttribute("type"))&&(e.validity.rangeUnderflow||e.validity.rangeOverflow)&&t.add(e);break}case"required":{let d;if(/^(?:select|textarea)$/.test(o))d=e;else if(o==="input")if(e.hasAttribute("type")){const w=e.getAttribute("type");(w==="file"||b.test(w)||_.test(w)||k.test(w))&&(d=e)}else d=e;d&&(e.required||e.hasAttribute("required"))&&t.add(e);break}case"optional":{let d;if(/^(?:select|textarea)$/.test(o))d=e;else if(o==="input")if(e.hasAttribute("type")){const w=e.getAttribute("type");(w==="file"||b.test(w)||_.test(w)||k.test(w))&&(d=e)}else d=e;d&&!(e.required||e.hasAttribute("required"))&&t.add(e);break}case"root":{e===this.#t.documentElement&&t.add(e);break}case"empty":{if(e.hasChildNodes()){const d=this.#f.createTreeWalker(e,u.SHOW_ALL);let w=d.firstChild(),g;for(;w&&(g=w.nodeType!==u.ELEMENT_NODE&&w.nodeType!==u.TEXT_NODE,!!g);)w=d.nextSibling();g&&t.add(e)}else t.add(e);break}case"first-child":{(n&&e===n.firstElementChild||e===this.#s)&&t.add(e);break}case"last-child":{(n&&e===n.lastElementChild||e===this.#s)&&t.add(e);break}case"only-child":{(n&&e===n.firstElementChild&&e===n.lastElementChild||e===this.#s)&&t.add(e);break}case"first-of-type":{if(n){const[d]=this._collectNthOfType({a:0,b:1},e);d&&t.add(d)}else e===this.#s&&t.add(e);break}case"last-of-type":{if(n){const[d]=this._collectNthOfType({a:0,b:1,reverse:!0},e);d&&t.add(d)}else e===this.#s&&t.add(e);break}case"only-of-type":{if(n){const[d]=this._collectNthOfType({a:0,b:1},e);if(d===e){const[w]=this._collectNthOfType({a:0,b:1,reverse:!0},e);w===e&&t.add(e)}}else e===this.#s&&t.add(e);break}case"host":case"host-context":break;case"after":case"before":case"first-letter":case"first-line":{if(f){const d=`Unsupported pseudo-element ::${s}`;throw new DOMException(d,u.NOT_SUPPORTED_ERR)}break}case"active":case"autofill":case"blank":case"buffering":case"current":case"defined":case"focus-visible":case"fullscreen":case"future":case"hover":case"modal":case"muted":case"past":case"paused":case"picture-in-picture":case"playing":case"seeking":case"stalled":case"user-invalid":case"user-valid":case"volume-locked":case"-webkit-autofill":{if(f){const d=`Unsupported pseudo-class :${s}`;throw new DOMException(d,u.NOT_SUPPORTED_ERR)}break}default:if(s.startsWith("-webkit-")){if(f){const d=`Unsupported pseudo-class :${s}`;throw new DOMException(d,u.NOT_SUPPORTED_ERR)}}else if(!h){const d=`Unknown pseudo-class :${s}`;throw new DOMException(d,u.SYNTAX_ERR)}}}return t}_matchShadowHostPseudoClass(i,e){const{children:r,name:a}=i;let s;if(Array.isArray(r)){const[o]=(0,y.walkAST)(r[0]),[...n]=o,{host:h}=e;if(a==="host"){let f;for(const t of n){const{type:l}=t;if(l===u.COMBINATOR){const m=`Invalid selector ${(0,y.generateCSS)(i)}`;throw new DOMException(m,u.SYNTAX_ERR)}if(f=this._matchSelector(t,h).has(h),!f)break}f&&(s=e)}else if(a==="host-context"){let f=h,t;for(;f;){for(const l of n){const{type:c}=l;if(c===u.COMBINATOR){const p=`Invalid selector ${(0,y.generateCSS)(i)}`;throw new DOMException(p,u.SYNTAX_ERR)}if(t=this._matchSelector(l,f).has(f),!t)break}if(t)break;f=f.parentNode}t&&(s=e)}}else if(a==="host")s=e;else{const o=`Invalid selector :${a}`;throw new DOMException(o,u.SYNTAX_ERR)}return s??null}_matchSelector(i,e,r){const{type:a}=i,s=new Set;if(i.name===u.EMPTY)return s;const o=(0,y.unescapeSelector)(i.name);if(i.name=o,e.nodeType===u.ELEMENT_NODE)switch(a){case u.SELECTOR_PSEUDO_ELEMENT:{(0,M.matchPseudoElementSelector)(o,r);break}case u.SELECTOR_ID:{e.id===o&&s.add(e);break}case u.SELECTOR_CLASS:{e.classList.contains(o)&&s.add(e);break}case u.SELECTOR_PSEUDO_CLASS:return this._matchPseudoClassSelector(i,e,r);default:{const n=(0,M.matchSelector)(i,e,r);n&&s.add(n)}}else if(this.#d&&a===u.SELECTOR_PSEUDO_CLASS&&e.nodeType===u.DOCUMENT_FRAGMENT_NODE){if(o!=="has"&&u.REG_LOGICAL_PSEUDO.test(o))return this._matchPseudoClassSelector(i,e,r);if(u.REG_SHADOW_HOST.test(o)){const n=this._matchShadowHostPseudoClass(i,e,r);n&&s.add(n)}}return s}_matchLeaves(i,e,r){const{attributes:a,localName:s,nodeType:o}=e;let n=this.#m.get(i),h;if(n&&n.has(e)){const{attr:f,matched:t}=n.get(e);a?.length===f&&(h=t)}if(typeof h!="boolean"){const f=/^(?:(?:fieldse|inpu|selec)t|button|form|textarea)$/;let t;o===u.ELEMENT_NODE&&f.test(s)?t=!1:t=!0;for(const l of i){const{name:c,type:m}=l,p=(0,y.unescapeSelector)(c);if(l.name=p,m===u.SELECTOR_PSEUDO_CLASS&&p==="dir"&&(t=!1),h=this._matchSelector(l,e,r).has(e),!h)break}t&&(n||(n=new WeakMap),n.set(e,{attr:a?.length,matched:h}),this.#m.set(i,n))}return!!h}_matchHTMLCollection(i,e={}){const{compound:r,filterLeaves:a}=e,s=new Set,o=i.length;if(o)if(r)for(let n=0;n<o;n++){const h=i[n];this._matchLeaves(a,h,e)&&s.add(h)}else{const n=[].slice.call(i);return new Set(n)}return s}_findDescendantNodes(i,e,r){const[a,...s]=i,o=s.length>0,{type:n}=a,h=(0,y.unescapeSelector)(a.name);a.name=h;let f=new Set,t=!1;if(this.#d)t=!0;else switch(n){case u.SELECTOR_PSEUDO_ELEMENT:{(0,M.matchPseudoElementSelector)(h,r);break}case u.SELECTOR_ID:{if(this.#s.nodeType===u.ELEMENT_NODE)t=!0;else{const l=this.#s.getElementById(h);l&&l!==e&&e.contains(l)&&(o?this._matchLeaves(s,l,r)&&f.add(l):f.add(l))}break}case u.SELECTOR_CLASS:{const l=e.getElementsByClassName(h);f=this._matchHTMLCollection(l,{compound:o,filterLeaves:s});break}case u.SELECTOR_TYPE:{if(this.#t.contentType==="text/html"&&!/[*|]/.test(h)){const l=e.getElementsByTagName(h);f=this._matchHTMLCollection(l,{compound:o,filterLeaves:s})}else t=!0;break}default:t=!0}return{nodes:f,pending:t}}_matchCombinator(i,e,r={}){const{combo:a,leaves:s}=i,{name:o}=a,{parentNode:n}=e,{dir:h}=r,f=new Set;if(h===C)switch(o){case"+":{const t=e.nextElementSibling;t&&this._matchLeaves(s,t,r)&&f.add(t);break}case"~":{if(n){const t=this._createTreeWalker(n);let l=this._traverse(e,t);for(l=t.nextSibling();l;)this._matchLeaves(s,l,r)&&f.add(l),l=t.nextSibling()}break}case">":{const t=this._createTreeWalker(e);let l=this._traverse(e,t);for(l=t.firstChild();l;)this._matchLeaves(s,l,r)&&f.add(l),l=t.nextSibling();break}case" ":default:{const{nodes:t,pending:l}=this._findDescendantNodes(s,e);if(t.size)return t;if(l){const c=this._createTreeWalker(e);let m=this._traverse(e,c);for(m=c.nextNode();m&&e.contains(m);)this._matchLeaves(s,m,r)&&f.add(m),m=c.nextNode()}}}else switch(o){case"+":{const t=e.previousElementSibling;t&&this._matchLeaves(s,t,r)&&f.add(t);break}case"~":{if(n){const t=this._createTreeWalker(n);let l=this._traverse(n,t);for(l=t.firstChild();l&&l!==e;)this._matchLeaves(s,l,r)&&f.add(l),l=t.nextSibling()}break}case">":{n&&this._matchLeaves(s,n,r)&&f.add(n);break}case" ":default:{const t=[];let l=n;for(;l;)this._matchLeaves(s,l,r)&&t.push(l),l=l.parentNode;if(t.length)return new Set(t.reverse())}}return f}_findNode(i,e){const{node:r}=e;let a=this._traverse(r,this.#n),s;if(a)for(a.nodeType!==u.ELEMENT_NODE?a=this.#n.nextNode():a===r&&a!==this.#s&&(a=this.#n.nextNode());a;){if(this._matchLeaves(i,a,{warn:this.#i})){s=a;break}a=this.#n.nextNode()}return s??null}_matchSelf(i){const e=[],r=this._matchLeaves(i,this.#e,{warn:this.#i});let a=!1;return r&&(e.push(this.#e),a=!0),[e,a]}_findLineal(i,e={}){const{complex:r}=e,a=[];let s=this._matchLeaves(i,this.#e,{warn:this.#i}),o=!1;if(s&&(a.push(this.#e),o=!0),!s||r){let n=this.#e.parentNode;for(;n&&(s=this._matchLeaves(i,n,{warn:this.#i}),s&&(a.push(n),o=!0),n.parentNode);)n=n.parentNode}return[a,o]}_findFirst(i){const e=[],r=this._findNode(i,{node:this.#e});let a=!1;return r&&(e.push(r),a=!0),[e,a]}_findFromHTMLCollection(i,e={}){const{complex:r,compound:a,filterLeaves:s,targetType:o}=e;let n=[],h=!1,f=!1;const t=i.length;if(t)if(this.#e.nodeType===u.ELEMENT_NODE)for(let l=0;l<t;l++){const c=i[l];if(c!==this.#e&&(this.#e.contains(c)||c.contains(this.#e))){if(a){if(this._matchLeaves(s,c,{warn:this.#i})&&(n.push(c),h=!0,o===A))break}else if(n.push(c),h=!0,o===A)break}}else if(r)if(a)for(let l=0;l<t;l++){const c=i[l];if(this._matchLeaves(s,c,{warn:this.#i})&&(n.push(c),h=!0,o===A))break}else n=[].slice.call(i),h=!0,f=!0;else if(a)for(let l=0;l<t;l++){const c=i[l];if(this._matchLeaves(s,c,{warn:this.#i})&&(n.push(c),h=!0,o===A))break}else n=[].slice.call(i),h=!0,f=!0;return[n,h,f]}_findEntryNodes(i,e,r){const{leaves:a}=i,[s,...o]=a,n=o.length>0,{type:h}=s,f=(0,y.unescapeSelector)(s.name);s.name=f;let t=[],l=!1,c=!1,m=!1;switch(h){case u.SELECTOR_PSEUDO_ELEMENT:{(0,M.matchPseudoElementSelector)(f,{warn:this.#i});break}case u.SELECTOR_ID:{if(e===D)[t,c]=this._matchSelf(a);else if(e===R)[t,c]=this._findLineal(a,{complex:r});else if(e===A&&this.#s.nodeType!==u.ELEMENT_NODE){const p=this.#s.getElementById(f);p&&(n?this._matchLeaves(o,p,{warn:this.#i})&&(t.push(p),c=!0):(t.push(p),c=!0))}else e===A?[t,c]=this._findFirst(a):m=!0;break}case u.SELECTOR_CLASS:{if(e===D)[t,c]=this._matchSelf(a);else if(e===R)[t,c]=this._findLineal(a,{complex:r});else if(this.#s.nodeType===u.DOCUMENT_NODE){const p=this.#s.getElementsByClassName(f);p.length&&([t,c,l]=this._findFromHTMLCollection(p,{complex:r,compound:n,filterLeaves:o,targetType:e}))}else e===A?[t,c]=this._findFirst(a):m=!0;break}case u.SELECTOR_TYPE:{if(e===D)[t,c]=this._matchSelf(a);else if(e===R)[t,c]=this._findLineal(a,{complex:r});else if(this.#t.contentType==="text/html"&&this.#s.nodeType===u.DOCUMENT_NODE&&!/[*|]/.test(f)){const p=this.#s.getElementsByTagName(f);p.length&&([t,c,l]=this._findFromHTMLCollection(p,{complex:r,compound:n,filterLeaves:o,targetType:e}))}else e===A?[t,c]=this._findFirst(a):m=!0;break}default:if(e!==R&&u.REG_SHADOW_HOST.test(f)){if(this.#d&&this.#e.nodeType===u.DOCUMENT_FRAGMENT_NODE){const p=this._matchShadowHostPseudoClass(s,this.#e);p&&(t.push(p),c=!0)}}else e===D?[t,c]=this._matchSelf(a):e===R?[t,c]=this._findLineal(a,{complex:r}):e===A?[t,c]=this._findFirst(a):m=!0}return{collected:l,compound:n,filtered:c,nodes:t,pending:m}}_getEntryTwig(i,e){const r=i.length,a=r>1,s=i[0];let o,n;if(a){const{combo:h,leaves:[{name:f,type:t}]}=s,l=i[r-1],{leaves:[{name:c,type:m}]}=l;if(m===u.SELECTOR_PSEUDO_ELEMENT||m===u.SELECTOR_ID)o=O,n=l;else if(t===u.SELECTOR_PSEUDO_ELEMENT||t===u.SELECTOR_ID)o=C,n=s;else if(e===$)if(f==="*"&&t===u.SELECTOR_TYPE)o=O,n=l;else if(c==="*"&&m===u.SELECTOR_TYPE)o=C,n=s;else if(r===2){const{name:p}=h;/^[+~]$/.test(p)?(o=O,n=l):(o=C,n=s)}else o=C,n=s;else if(c==="*"&&m===u.SELECTOR_TYPE)o=C,n=s;else if(f==="*"&&t===u.SELECTOR_TYPE)o=O,n=l;else{let p;for(const{combo:b,leaves:[_]}of i){const{type:N}=_,k=(0,y.unescapeSelector)(_.name);if(_.name=k,N===u.SELECTOR_PSEUDO_CLASS&&k==="dir"){p=!1;break}if(!p&&b){const{name:d}=b;/^[+~]$/.test(d)&&(p=!0)}}p?(o=C,n=s):(o=O,n=l)}}else o=O,n=s;return{complex:a,dir:o,twig:n}}_collectNodes(i){const e=this.#a.values();if(i===$||i===A){const r=new Set;let a=0;for(const{branch:s}of e){const{complex:o,dir:n,twig:h}=this._getEntryTwig(s,i),{collected:f,compound:t,filtered:l,nodes:c,pending:m}=this._findEntryNodes(h,i,o);c.length?(this.#a[a].find=!0,this.#h[a]=c):m&&r.add(new Map([["index",a],["twig",h]])),this.#a[a].collected=f,this.#a[a].dir=n,this.#a[a].filtered=l||!t,a++}if(r.size){let s,o;this.#e!==this.#s&&this.#e.nodeType===u.ELEMENT_NODE?(s=this.#e,o=this.#n):(s=this.#s,o=this.#r);let n=this._traverse(s,o);for(;n;){let h=!1;if(this.#e.nodeType===u.ELEMENT_NODE?n===this.#e?h=!0:h=this.#e.contains(n):h=!0,h)for(const f of r){const{leaves:t}=f.get("twig");if(this._matchLeaves(t,n,{warn:this.#i})){const c=f.get("index");this.#a[c].filtered=!0,this.#a[c].find=!0,this.#h[c].push(n)}}n!==o.currentNode&&(n=this._traverse(n,o)),n=o.nextNode()}}}else{let r=0;for(const{branch:a}of e){const s=a[a.length-1],o=a.length>1,{compound:n,filtered:h,nodes:f}=this._findEntryNodes(s,i,o);f.length&&(this.#a[r].find=!0,this.#h[r]=f),this.#a[r].dir=O,this.#a[r].filtered=h||!n,r++}}return[this.#a,this.#h]}_getCombinedNodes(i,e,r){const a=[];for(const s of e){const o=this._matchCombinator(i,s,{dir:r,warn:this.#i});o.size&&a.push(...o)}return a.length?new Set(a):new Set}_matchNodeNext(i,e,r){const{combo:a,index:s}=r,{combo:o,leaves:n}=i[s],h={combo:a,leaves:n},f=this._getCombinedNodes(h,e,C);let t;if(f.size)if(s===i.length-1){const[l]=(0,L.sortNodes)(f);t=l}else t=this._matchNodeNext(i,f,{combo:o,index:s+1});return t??null}_matchNodePrev(i,e,r){const{index:a}=r,s=i[a],o=new Set([e]),n=this._getCombinedNodes(s,o,O);let h;if(n.size){if(a===0)h=e;else for(const f of n)if(this._matchNodePrev(i,f,{index:a-1}))return e}return h??null}_matchNodes(i){const[[...e],r]=this._collectNodes(i),a=e.length;let s=new Set;for(let o=0;o<a;o++){const{branch:n,collected:h,dir:f,find:t}=e[o],l=n.length;if(l&&t){const c=r[o],m=c.length,p=l-1;if(p===0)if((i===$||i===A)&&this.#e.nodeType===u.ELEMENT_NODE)for(let b=0;b<m;b++){const _=c[b];if(_!==this.#e&&this.#e.contains(_)&&(s.add(_),i!==$))break}else if(i===$)if(s.size){const b=[...s];s=new Set([...b,...c]),this.#u=!0}else s=new Set(c);else{const[b]=c;s.add(b)}else if(i===$)if(f===C){let{combo:b}=n[0];for(const _ of c){let N=new Set([_]);for(let k=1;k<l;k++){const{combo:d,leaves:w}=n[k],g={combo:b,leaves:w};if(N=this._getCombinedNodes(g,N,f),N.size)if(k===p)if(s.size){const x=[...s];s=new Set([...x,...N]),this.#u=!0}else s=N;else b=d;else break}}}else for(const b of c){let _=new Set([b]);for(let N=p-1;N>=0;N--){const k=n[N];if(_=this._getCombinedNodes(k,_,f),_.size)N===0&&(s.add(b),l>1&&s.size>1&&(this.#u=!0));else break}}else if(i===A&&f===C){const{combo:b}=n[0];let _;for(const N of c)if(_=this._matchNodeNext(n,new Set([N]),{combo:b,index:1}),_){s.add(_);break}if(!_&&!h){const{leaves:N}=n[0],[k]=c;let d=this._findNode(N,{node:k});for(;d;){if(_=this._matchNodeNext(n,new Set([d]),{combo:b,index:1}),_){s.add(_);break}d=this._findNode(N,{node:d})}}}else{let b;for(const _ of c)if(b=this._matchNodePrev(n,_,{index:p-1}),b){s.add(_);break}if(!b&&!h&&i===A){const{leaves:_}=n[p],[N]=c;let k=this._findNode(_,{node:N});for(;k;){if(b=this._matchNodePrev(n,k,{index:p-1}),b){s.add(k);break}k=this._findNode(_,{node:k})}}}}}return s}_find(i){return(i===$||i===A)&&this._prepareQuerySelectorWalker(),this._matchNodes(i)}matches(i,e,r){let a;try{if(e?.nodeType!==u.ELEMENT_NODE){const s=`Unexpected node ${e?.nodeName}`;throw new TypeError(s)}(0,y.filterSelector)(i)?a=this.#c.match(i,e):(this._setup(i,e,r),a=this._find(D).size)}catch(s){this._onError(s)}return!!a}closest(i,e,r){let a;try{if(e?.nodeType!==u.ELEMENT_NODE){const s=`Unexpected node ${e?.nodeName}`;throw new TypeError(s)}if((0,y.filterSelector)(i))a=this.#c.closest(i,e);else{this._setup(i,e,r);const s=this._find(R);if(s.size){let o=this.#e;for(;o;){if(s.has(o)){a=o;break}o=o.parentNode}}}}catch(s){this._onError(s)}return a??null}querySelector(i,e,r){let a;try{if(this._setup(i,e,r),this.#f===this.#t&&!this.#o&&(0,y.filterSelector)(i))a=this.#c.first(i,e);else{const s=this._find(A);s.delete(this.#e),s.size&&([a]=(0,L.sortNodes)(s))}}catch(s){this._onError(s)}return a??null}querySelectorAll(i,e,r){let a;try{if(this._setup(i,e,r),this.#f===this.#t&&!this.#o&&(0,y.filterSelector)(i))a=this.#c.select(i,e);else{const s=this._find($);s.delete(this.#e),s.size&&(this.#u?a=(0,L.sortNodes)(s):a=[...s])}}catch(s){this._onError(s)}return a??[]}}0&&(module.exports={Finder});
1
+ var W=Object.create;var P=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var B=Object.getOwnPropertyNames;var j=Object.getPrototypeOf,G=Object.prototype.hasOwnProperty;var q=(E,a)=>{for(var e in a)P(E,e,{get:a[e],enumerable:!0})},z=(E,a,e,n)=>{if(a&&typeof a=="object"||typeof a=="function")for(let l of B(a))!G.call(E,l)&&l!==e&&P(E,l,{get:()=>a[l],enumerable:!(n=H(a,l))||n.enumerable});return E};var I=(E,a,e)=>(e=E!=null?W(j(E)):{},z(a||!E||!E.__esModule?P(e,"default",{value:E,enumerable:!0}):e,E)),V=E=>z(P({},"__esModule",{value:!0}),E);var Y={};q(Y,{Finder:()=>X});module.exports=V(Y);var U=I(require("is-potential-custom-element-name"),1),F=I(require("nwsapi"),1),C=require("./dom-util.js"),$=I(require("./matcher.js"),1),y=require("./parser.js"),b=require("./constant.js");const T="next",O="prev",M="all",A="first",R="lineal",D="self";class X{#a;#l;#t;#o;#f;#e;#h;#w;#c;#n;#m;#s;#d;#b;#r;#u;#i;#p;constructor(a){this.#p=a,this.#f=a.document,this.#l=new WeakMap,this.#m=new WeakMap,this.#c=(0,F.default)({document:a.document,DOMException:a.DOMException}),this.#c.configure({LOGERRORS:!1})}_onError(a){if(!this.#w)if(a instanceof DOMException||a instanceof this.#p.DOMException)if(a.name===b.NOT_SUPPORTED_ERR)this.#i&&console.warn(a.message);else throw new this.#p.DOMException(a.message,a.name);else throw a}_setup(a,e,n={}){const{noexcept:l,warn:s}=n;return this.#w=!!l,this.#i=!!s,this.#e=e,[this.#t,this.#s,this.#r]=(0,C.resolveContent)(e),this.#d=(0,C.isInShadowTree)(e),[this.#a,this.#h]=this._correspond(a),this.#u=new WeakMap,e}_correspond(a){const e=[];this.#o=!1;let n;if(this.#t){const l=this.#l.get(this.#t);if(l&&l.has(`${a}`)){const s=l.get(`${a}`);this.#o=s.descendant,n=s.ast}}if(n){const l=n.length;for(let s=0;s<l;s++)n[s].collected=!1,n[s].dir=null,n[s].filtered=!1,n[s].find=!1,e[s]=[]}else{let l;try{l=(0,y.parseSelector)(a)}catch(h){this._onError(h)}const s=(0,y.walkAST)(l);let c=!1,r=0;n=[];for(const[...h]of s){const f=[];let t=h.shift();if(t&&t.type!==b.COMBINATOR){const i=new Set;for(;t;){if(t.type===b.COMBINATOR){const[o]=h;if(o.type===b.COMBINATOR){const _=`Invalid selector ${a}`;throw new DOMException(_,b.SYNTAX_ERR)}const m=t.name;/^[\s>]$/.test(m)&&(c=!0),f.push({combo:t,leaves:(0,y.sortAST)(i)}),i.clear()}else if(t){let{name:o}=t;o&&typeof o=="string"&&(o=(0,y.unescapeSelector)(o),typeof o=="string"&&o!==t.name&&(t.name=o),/[|:]/.test(o)&&(t.namespace=!0)),i.add(t)}if(h.length)t=h.shift();else{f.push({combo:null,leaves:(0,y.sortAST)(i)}),i.clear();break}}}n.push({branch:f,collected:!1,dir:null,filtered:!1,find:!1}),e[r]=[],r++}if(this.#t){let h;this.#l.has(this.#t)?h=this.#l.get(this.#t):h=new Map,h.set(`${a}`,{ast:n,descendant:c}),this.#l.set(this.#t,h)}this.#o=c}return[n,e]}_createTreeWalker(a){let e;return this.#u.has(a)?e=this.#u.get(a):(e=this.#f.createTreeWalker(a,b.WALKER_FILTER),this.#u.set(a,e)),e}_prepareQuerySelectorWalker(){return this.#n=this._createTreeWalker(this.#e),this.#b=!1,this.#n}_traverse(a,e=this.#r){let n=e.currentNode,l;if(n===a)l=n;else if(n.contains(a))for(n=e.nextNode();n;){if(n===a){l=n;break}n=e.nextNode()}else{if(n!==e.root)for(;n&&!(n===e.root||n===a);)n=e.parentNode();if(a?.nodeType===b.ELEMENT_NODE)for(;n;){if(n===a){l=n;break}n=e.nextNode()}else l=n}return l??null}_collectNthChild(a,e,n){const{a:l,b:s,reverse:c,selector:r}=a,{parentNode:h}=e,f=new Set;let t;if(r&&(this.#l.has(r)?t=this.#l.get(r):(t=(0,y.walkAST)(r),this.#l.set(r,t))),h){const i=this.#r;let o=this._traverse(h,i);o=i.firstChild();let m=0;for(;o;)m++,o=i.nextSibling();o=this._traverse(h,i);const _=new Set;if(t)for(o=i.firstChild();o;){let u;for(const N of t)if(u=this._matchLeaves(N,o,n),!u)break;u&&_.add(o),o=i.nextSibling()}if(l===0){if(s>0&&s<=m){if(_.size){o=this._traverse(h,i),c?o=i.lastChild():o=i.firstChild();let u=0;for(;o;){if(_.has(o)){if(u===s-1){f.add(o);break}u++}c?o=i.previousSibling():o=i.nextSibling()}}else if(!r){o=this._traverse(h,i),c?o=i.lastChild():o=i.firstChild();let u=0;for(;o;){if(u===s-1){f.add(o);break}c?o=i.previousSibling():o=i.nextSibling(),u++}}}}else{let u=s-1;if(l>0)for(;u<0;)u+=l;if(u>=0&&u<m){o=this._traverse(h,i),c?o=i.lastChild():o=i.firstChild();let N=0,k=l>0?0:s-1;for(;o&&(o&&u>=0&&u<m);)_.size?_.has(o)&&(k===u&&(f.add(o),u+=l),l>0?k++:k--):N===u&&(r||f.add(o),u+=l),c?o=i.previousSibling():o=i.nextSibling(),N++}}if(c&&f.size>1){const u=[...f];return new Set(u.reverse())}}else if(e===this.#s&&l+s===1)if(t){let i;for(const o of t)if(i=this._matchLeaves(o,e,n),i)break;i&&f.add(e)}else f.add(e);return f}_collectNthOfType(a,e){const{a:n,b:l,reverse:s}=a,{localName:c,parentNode:r,prefix:h}=e,f=new Set;if(r){const t=this.#r;let i=this._traverse(r,t);i=t.firstChild();let o=0;for(;i;)o++,i=t.nextSibling();if(n===0){if(l>0&&l<=o){i=this._traverse(r,t),s?i=t.lastChild():i=t.firstChild();let m=0;for(;i;){const{localName:_,prefix:u}=i;if(_===c&&u===h){if(m===l-1){f.add(i);break}m++}s?i=t.previousSibling():i=t.nextSibling()}}}else{let m=l-1;if(n>0)for(;m<0;)m+=n;if(m>=0&&m<o){i=this._traverse(r,t),s?i=t.lastChild():i=t.firstChild();let _=n>0?0:l-1;for(;i;){const{localName:u,prefix:N}=i;if(u===c&&N===h){if(_===m&&(f.add(i),m+=n),m<0||m>=o)break;n>0?_++:_--}s?i=t.previousSibling():i=t.nextSibling()}}}if(s&&f.size>1){const m=[...f];return new Set(m.reverse())}}else e===this.#s&&n+l===1&&f.add(e);return f}_matchAnPlusB(a,e,n,l){const{nth:{a:s,b:c,name:r},selector:h}=a,f=new Map;if(r?(r==="even"?(f.set("a",2),f.set("b",0)):r==="odd"&&(f.set("a",2),f.set("b",1)),n.indexOf("last")>-1&&f.set("reverse",!0)):(typeof s=="string"&&/-?\d+/.test(s)?f.set("a",s*1):f.set("a",0),typeof c=="string"&&/-?\d+/.test(c)?f.set("b",c*1):f.set("b",0),n.indexOf("last")>-1&&f.set("reverse",!0)),/^nth-(?:last-)?child$/.test(n)){h&&f.set("selector",h);const t=Object.fromEntries(f);return this._collectNthChild(t,e,l)}else if(/^nth-(?:last-)?of-type$/.test(n)){const t=Object.fromEntries(f);return this._collectNthOfType(t,e)}return new Set}_matchHasPseudoFunc(a,e,n={}){let l;if(Array.isArray(a)&&a.length){const[s]=a,{type:c}=s;let r;c===b.COMBINATOR?r=a.shift():r={name:" ",type:b.COMBINATOR};const h=[];for(;a.length;){const[i]=a,{type:o}=i;if(o===b.COMBINATOR)break;h.push(a.shift())}const f={combo:r,leaves:h};n.dir=T;const t=this._matchCombinator(f,e,n);if(t.size)if(a.length){for(const i of t)if(l=this._matchHasPseudoFunc(Object.assign([],a),i,n),l)break}else l=!0}return!!l}_matchLogicalPseudoFunc(a,e,n={}){const{astName:l="",branches:s=[],selector:c="",twigBranches:r=[]}=a;let h;if(l==="has")if(c.includes(":has("))h=null;else{let f;for(const t of s)if(f=this._matchHasPseudoFunc(Object.assign([],t),e,n),f)break;f&&(h=e)}else{const f=/^(?:is|where)$/.test(l);n.forgive=f;const t=r.length;let i;for(let o=0;o<t;o++){const m=r[o],_=m.length-1,{leaves:u}=m[_];if(i=this._matchLeaves(u,e,n),i&&_>0){let N=new Set([e]);for(let k=_-1;k>=0;k--){const g=m[k],d=[];n.dir=O;for(const p of N){const w=this._matchCombinator(g,p,n);w.size&&d.push(...w)}if(d.length)k===0?i=!0:N=new Set(d);else{i=!1;break}}}if(i)break}l==="not"?i||(h=e):i&&(h=e)}return h??null}_matchPseudoClassSelector(a,e,n={}){const{children:l,name:s}=a,{localName:c,parentNode:r}=e,{forgive:h,warn:f=this.#i}=n,t=new Set;if(b.REG_LOGICAL_PSEUDO.test(s)){let i;if(this.#l.has(a))i=this.#l.get(a);else{const m=(0,y.walkAST)(a),_=[],u=[];for(const[...N]of m){for(const p of N){const w=(0,y.generateCSS)(p);_.push(w)}const k=[],g=new Set;let d=N.shift();for(;d;)if(d.type===b.COMBINATOR?(k.push({combo:d,leaves:[...g]}),g.clear()):d&&g.add(d),N.length)d=N.shift();else{k.push({combo:null,leaves:[...g]}),g.clear();break}u.push(k)}i={astName:s,branches:m,twigBranches:u,selector:_.join(",")},this.#l.set(a,i)}const o=this._matchLogicalPseudoFunc(i,e,n);o&&t.add(o)}else if(Array.isArray(l))if(/^nth-(?:last-)?(?:child|of-type)$/.test(s)){const[i]=l;return this._matchAnPlusB(i,e,s,n)}else switch(s){case"dir":case"lang":{const i=$.default.matchSelector(a,e);i&&t.add(i);break}case"current":case"nth-col":case"nth-last-col":{if(f){const i=`Unsupported pseudo-class :${s}()`;throw new DOMException(i,b.NOT_SUPPORTED_ERR)}break}case"host":case"host-context":break;default:if(!h){const i=`Unknown pseudo-class :${s}()`;throw new DOMException(i,b.SYNTAX_ERR)}}else{const i=/^a(?:rea)?$/,o=/^(?:(?:fieldse|inpu|selec)t|button|opt(?:group|ion)|textarea)$/,m=/^(?:(?:inpu|selec)t|button|form|textarea)$/,_=/^d(?:etails|ialog)$/,u=/^(?:checkbox|radio)$/,N=/^(?:date(?:time-local)?|month|time|week)$/,k=/(?:(?:rang|tim)e|date(?:time-local)?|month|number|week)$/,g=/^(?:(?:emai|te|ur)l|number|password|search|text)$/;switch(s){case"any-link":case"link":{i.test(c)&&e.hasAttribute("href")&&t.add(e);break}case"local-link":{if(i.test(c)&&e.hasAttribute("href")){const{href:d,origin:p,pathname:w}=new URL(this.#t.URL),S=new URL(e.getAttribute("href"),d);S.origin===p&&S.pathname===w&&t.add(e)}break}case"visited":break;case"target":{const{hash:d}=new URL(this.#t.URL);e.id&&d===`#${e.id}`&&this.#t.contains(e)&&t.add(e);break}case"target-within":{const{hash:d}=new URL(this.#t.URL);if(d){const p=d.replace(/^#/,"");let w=this.#t.getElementById(p);for(;w;){if(w===e){t.add(e);break}w=w.parentNode}}break}case"scope":{this.#e.nodeType===b.ELEMENT_NODE?!this.#d&&e===this.#e&&t.add(e):e===this.#t.documentElement&&t.add(e);break}case"focus":{if(e===this.#t.activeElement){let d=e,p=!0;for(;d;){if(d.hasAttribute("hidden")){p=!1;break}else if(d.hasAttribute("style")){const{display:w,visibility:S}=d.style;if(p=!(w==="none"||S==="hidden"),!p)break}if(d.parentNode&&d.parentNode.nodeType===b.ELEMENT_NODE)d=d.parentNode;else break}p&&t.add(e)}break}case"focus-within":{let d=this.#t.activeElement,p;for(;d;){if(d===e){p=!0;break}d=d.parentNode}if(p){let w=e,S=!0;for(;w;){if(w.hasAttribute("hidden")){S=!1;break}else if(w.hasAttribute("style")){const{display:v,visibility:L}=w.style;if(S=!(v==="none"||L==="hidden"),!S)break}if(w.parentNode&&w.parentNode.nodeType===b.ELEMENT_NODE)w=w.parentNode;else break}S&&t.add(e)}break}case"open":{_.test(c)&&e.hasAttribute("open")&&t.add(e);break}case"closed":{_.test(c)&&!e.hasAttribute("open")&&t.add(e);break}case"disabled":{if(o.test(c)||(0,U.default)(c))if(e.disabled||e.hasAttribute("disabled"))t.add(e);else{let d=r;for(;d&&d.localName!=="fieldset";)d=d.parentNode;d&&r.localName!=="legend"&&d.hasAttribute("disabled")&&t.add(e)}break}case"enabled":{(o.test(c)||(0,U.default)(c))&&!(e.disabled&&e.hasAttribute("disabled"))&&t.add(e);break}case"read-only":{switch(c){case"textarea":{(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&t.add(e);break}case"input":{(!e.type||N.test(e.type)||g.test(e.type))&&(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&t.add(e);break}default:(0,C.isContentEditable)(e)||t.add(e)}break}case"read-write":{switch(c){case"textarea":{e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled")||t.add(e);break}case"input":{(!e.type||N.test(e.type)||g.test(e.type))&&!(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&t.add(e);break}default:(0,C.isContentEditable)(e)&&t.add(e)}break}case"placeholder-shown":{let d;c==="textarea"?d=e:c==="input"&&(e.hasAttribute("type")?g.test(e.getAttribute("type"))&&(d=e):d=e),d&&e.value===""&&e.hasAttribute("placeholder")&&e.getAttribute("placeholder").trim().length&&t.add(e);break}case"checked":{(e.checked&&c==="input"&&e.hasAttribute("type")&&u.test(e.getAttribute("type"))||e.selected&&c==="option")&&t.add(e);break}case"indeterminate":{if(e.indeterminate&&c==="input"&&e.type==="checkbox"||c==="progress"&&!e.hasAttribute("value"))t.add(e);else if(c==="input"&&e.type==="radio"&&!e.hasAttribute("checked")){const d=e.name;let p=e.parentNode;for(;p&&p.localName!=="form";)p=p.parentNode;p||(p=this.#t.documentElement);const w=p.getElementsByTagName("input"),S=w.length;let v;for(let L=0;L<S;L++){const x=w[L];if(x.getAttribute("type")==="radio"&&(d?x.getAttribute("name")===d&&(v=!!x.checked):x.hasAttribute("name")||(v=!!x.checked),v))break}v||t.add(e)}break}case"default":{const d=/^(?:button|reset)$/,p=/^(?:image|submit)$/;if(c==="button"&&!(e.hasAttribute("type")&&d.test(e.getAttribute("type")))||c==="input"&&e.hasAttribute("type")&&p.test(e.getAttribute("type"))){let w=e.parentNode;for(;w&&w.localName!=="form";)w=w.parentNode;if(w){const S=this.#r;let v=this._traverse(w,S);for(v=S.firstChild();v&&w.contains(v);){const L=v.localName;let x;if(L==="button"?x=!(v.hasAttribute("type")&&d.test(v.getAttribute("type"))):L==="input"&&(x=v.hasAttribute("type")&&p.test(v.getAttribute("type"))),x){v===e&&t.add(e);break}v=S.nextNode()}}}else if(c==="input"&&e.hasAttribute("type")&&u.test(e.getAttribute("type"))&&(e.checked||e.hasAttribute("checked")))t.add(e);else if(c==="option"){let w=r,S=!1;for(;w&&w.localName!=="datalist";){if(w.localName==="select"){(w.multiple||w.hasAttribute("multiple"))&&(S=!0);break}w=w.parentNode}if(S)(e.selected||e.hasAttribute("selected"))&&t.add(e);else{const v=new Set,L=this.#r;let x=this._traverse(r,L);for(x=L.firstChild();x;){if(x.selected||x.hasAttribute("selected")){v.add(x);break}x=L.nextSibling()}v.size&&v.has(e)&&t.add(e)}}break}case"valid":{if(m.test(c))e.checkValidity()&&t.add(e);else if(c==="fieldset"){const d=this.#r;let p=this._traverse(e,d);p=d.firstChild();let w;for(;p&&e.contains(p)&&!(m.test(p.localName)&&(w=p.checkValidity(),!w));)p=d.nextNode();w&&t.add(e)}break}case"invalid":{if(m.test(c))e.checkValidity()||t.add(e);else if(c==="fieldset"){const d=this.#r;let p=this._traverse(e,d);p=d.firstChild();let w;for(;p&&e.contains(p)&&!(m.test(p.localName)&&(w=p.checkValidity(),!w));)p=d.nextNode();w||t.add(e)}break}case"in-range":{c==="input"&&!(e.readonly||e.hasAttribute("readonly"))&&!(e.disabled||e.hasAttribute("disabled"))&&e.hasAttribute("type")&&k.test(e.getAttribute("type"))&&!(e.validity.rangeUnderflow||e.validity.rangeOverflow)&&(e.hasAttribute("min")||e.hasAttribute("max")||e.getAttribute("type")==="range")&&t.add(e);break}case"out-of-range":{c==="input"&&!(e.readonly||e.hasAttribute("readonly"))&&!(e.disabled||e.hasAttribute("disabled"))&&e.hasAttribute("type")&&k.test(e.getAttribute("type"))&&(e.validity.rangeUnderflow||e.validity.rangeOverflow)&&t.add(e);break}case"required":{let d;if(/^(?:select|textarea)$/.test(c))d=e;else if(c==="input")if(e.hasAttribute("type")){const p=e.getAttribute("type");(p==="file"||u.test(p)||N.test(p)||g.test(p))&&(d=e)}else d=e;d&&(e.required||e.hasAttribute("required"))&&t.add(e);break}case"optional":{let d;if(/^(?:select|textarea)$/.test(c))d=e;else if(c==="input")if(e.hasAttribute("type")){const p=e.getAttribute("type");(p==="file"||u.test(p)||N.test(p)||g.test(p))&&(d=e)}else d=e;d&&!(e.required||e.hasAttribute("required"))&&t.add(e);break}case"root":{e===this.#t.documentElement&&t.add(e);break}case"empty":{if(e.hasChildNodes()){const d=this.#f.createTreeWalker(e,b.SHOW_ALL);let p=d.firstChild(),w;for(;p&&(w=p.nodeType!==b.ELEMENT_NODE&&p.nodeType!==b.TEXT_NODE,!!w);)p=d.nextSibling();w&&t.add(e)}else t.add(e);break}case"first-child":{(r&&e===r.firstElementChild||e===this.#s)&&t.add(e);break}case"last-child":{(r&&e===r.lastElementChild||e===this.#s)&&t.add(e);break}case"only-child":{(r&&e===r.firstElementChild&&e===r.lastElementChild||e===this.#s)&&t.add(e);break}case"first-of-type":{if(r){const[d]=this._collectNthOfType({a:0,b:1},e);d&&t.add(d)}else e===this.#s&&t.add(e);break}case"last-of-type":{if(r){const[d]=this._collectNthOfType({a:0,b:1,reverse:!0},e);d&&t.add(d)}else e===this.#s&&t.add(e);break}case"only-of-type":{if(r){const[d]=this._collectNthOfType({a:0,b:1},e);if(d===e){const[p]=this._collectNthOfType({a:0,b:1,reverse:!0},e);p===e&&t.add(e)}}else e===this.#s&&t.add(e);break}case"host":case"host-context":break;case"after":case"before":case"first-letter":case"first-line":{if(f){const d=`Unsupported pseudo-element ::${s}`;throw new DOMException(d,b.NOT_SUPPORTED_ERR)}break}case"active":case"autofill":case"blank":case"buffering":case"current":case"defined":case"focus-visible":case"fullscreen":case"future":case"hover":case"modal":case"muted":case"past":case"paused":case"picture-in-picture":case"playing":case"seeking":case"stalled":case"user-invalid":case"user-valid":case"volume-locked":case"-webkit-autofill":{if(f){const d=`Unsupported pseudo-class :${s}`;throw new DOMException(d,b.NOT_SUPPORTED_ERR)}break}default:if(s.startsWith("-webkit-")){if(f){const d=`Unsupported pseudo-class :${s}`;throw new DOMException(d,b.NOT_SUPPORTED_ERR)}}else if(!h){const d=`Unknown pseudo-class :${s}`;throw new DOMException(d,b.SYNTAX_ERR)}}}return t}_matchShadowHostPseudoClass(a,e){const{children:n,name:l}=a;let s;if(Array.isArray(n)){const[c]=(0,y.walkAST)(n[0]),[...r]=c,{host:h}=e;if(l==="host"){let f;for(const t of r){const{type:i}=t;if(i===b.COMBINATOR){const m=`Invalid selector ${(0,y.generateCSS)(a)}`;throw new DOMException(m,b.SYNTAX_ERR)}if(f=this._matchSelector(t,h).has(h),!f)break}f&&(s=e)}else if(l==="host-context"){let f=h,t;for(;f;){for(const i of r){const{type:o}=i;if(o===b.COMBINATOR){const _=`Invalid selector ${(0,y.generateCSS)(a)}`;throw new DOMException(_,b.SYNTAX_ERR)}if(t=this._matchSelector(i,f).has(f),!t)break}if(t)break;f=f.parentNode}t&&(s=e)}}else if(l==="host")s=e;else{const c=`Invalid selector :${l}`;throw new DOMException(c,b.SYNTAX_ERR)}return s??null}_matchSelector(a,e,n){const{type:l}=a,s=new Set;if(a.name===b.EMPTY)return s;const c=(0,y.unescapeSelector)(a.name);if(typeof c=="string"&&c!==a.name&&(a.name=c),e.nodeType===b.ELEMENT_NODE)switch(l){case b.SELECTOR_PSEUDO_ELEMENT:{$.default.matchPseudoElementSelector(c,n);break}case b.SELECTOR_ID:{e.id===c&&s.add(e);break}case b.SELECTOR_CLASS:{e.classList.contains(c)&&s.add(e);break}case b.SELECTOR_PSEUDO_CLASS:return this._matchPseudoClassSelector(a,e,n);default:{const r=$.default.matchSelector(a,e,n);r&&s.add(r)}}else if(this.#d&&l===b.SELECTOR_PSEUDO_CLASS&&e.nodeType===b.DOCUMENT_FRAGMENT_NODE){if(c!=="has"&&b.REG_LOGICAL_PSEUDO.test(c))return this._matchPseudoClassSelector(a,e,n);if(b.REG_SHADOW_HOST.test(c)){const r=this._matchShadowHostPseudoClass(a,e,n);r&&s.add(r)}}return s}_matchLeaves(a,e,n){const{attributes:l,localName:s,nodeType:c}=e;let r=this.#m.get(a),h;if(r&&r.has(e)){const{attr:f,matched:t}=r.get(e);l?.length===f&&(h=t)}if(typeof h!="boolean"){const f=/^(?:(?:fieldse|inpu|selec)t|button|form|textarea)$/;let t;c===b.ELEMENT_NODE&&f.test(s)?t=!1:t=!0;for(const i of a){const{name:o,type:m}=i;if(m===b.SELECTOR_PSEUDO_CLASS&&o==="dir"&&(t=!1),h=this._matchSelector(i,e,n).has(e),!h)break}t&&(r||(r=new WeakMap),r.set(e,{attr:l?.length,matched:h}),this.#m.set(a,r))}return!!h}_matchHTMLCollection(a,e={}){const{compound:n,filterLeaves:l}=e,s=new Set,c=a.length;if(c)if(n)for(let r=0;r<c;r++){const h=a[r];this._matchLeaves(l,h,e)&&s.add(h)}else{const r=[].slice.call(a);return new Set(r)}return s}_findDescendantNodes(a,e,n){const[l,...s]=a,c=s.length>0,{type:r}=l,h=(0,y.unescapeSelector)(l.name);typeof h=="string"&&h!==l.name&&(l.name=h);let f=new Set,t=!1;if(this.#d)t=!0;else switch(r){case b.SELECTOR_PSEUDO_ELEMENT:{$.default.matchPseudoElementSelector(h,n);break}case b.SELECTOR_ID:{if(this.#s.nodeType===b.ELEMENT_NODE)t=!0;else{const i=this.#s.getElementById(h);i&&i!==e&&e.contains(i)&&(c?this._matchLeaves(s,i,n)&&f.add(i):f.add(i))}break}case b.SELECTOR_CLASS:{const i=e.getElementsByClassName(h);f=this._matchHTMLCollection(i,{compound:c,filterLeaves:s});break}case b.SELECTOR_TYPE:{if(this.#t.contentType==="text/html"&&!/[*|]/.test(h)){const i=e.getElementsByTagName(h);f=this._matchHTMLCollection(i,{compound:c,filterLeaves:s})}else t=!0;break}default:t=!0}return{nodes:f,pending:t}}_matchCombinator(a,e,n={}){const{combo:l,leaves:s}=a,{name:c}=l,{parentNode:r}=e,{dir:h}=n,f=new Set;if(h===T)switch(c){case"+":{const t=e.nextElementSibling;t&&this._matchLeaves(s,t,n)&&f.add(t);break}case"~":{if(r){const t=this._createTreeWalker(r);let i=this._traverse(e,t);for(i=t.nextSibling();i;)this._matchLeaves(s,i,n)&&f.add(i),i=t.nextSibling()}break}case">":{const t=this._createTreeWalker(e);let i=this._traverse(e,t);for(i=t.firstChild();i;)this._matchLeaves(s,i,n)&&f.add(i),i=t.nextSibling();break}case" ":default:{const{nodes:t,pending:i}=this._findDescendantNodes(s,e);if(t.size)return t;if(i){const o=this._createTreeWalker(e);let m=this._traverse(e,o);for(m=o.nextNode();m&&e.contains(m);)this._matchLeaves(s,m,n)&&f.add(m),m=o.nextNode()}}}else switch(c){case"+":{const t=e.previousElementSibling;t&&this._matchLeaves(s,t,n)&&f.add(t);break}case"~":{if(r){const t=this._createTreeWalker(r);let i=this._traverse(r,t);for(i=t.firstChild();i&&i!==e;)this._matchLeaves(s,i,n)&&f.add(i),i=t.nextSibling()}break}case">":{r&&this._matchLeaves(s,r,n)&&f.add(r);break}case" ":default:{const t=[];let i=r;for(;i;)this._matchLeaves(s,i,n)&&t.push(i),i=i.parentNode;if(t.length)return new Set(t.reverse())}}return f}_findNode(a,e){const{node:n}=e;let l=this._traverse(n,this.#n),s;if(l)for(l.nodeType!==b.ELEMENT_NODE?l=this.#n.nextNode():l===n&&l!==this.#s&&(l=this.#n.nextNode());l;){if(this._matchLeaves(a,l,{warn:this.#i})){s=l;break}l=this.#n.nextNode()}return s??null}_matchSelf(a){const e=[],n=this._matchLeaves(a,this.#e,{warn:this.#i});let l=!1;return n&&(e.push(this.#e),l=!0),[e,l]}_findLineal(a,e={}){const{complex:n}=e,l=[];let s=this._matchLeaves(a,this.#e,{warn:this.#i}),c=!1;if(s&&(l.push(this.#e),c=!0),!s||n){let r=this.#e.parentNode;for(;r&&(s=this._matchLeaves(a,r,{warn:this.#i}),s&&(l.push(r),c=!0),r.parentNode);)r=r.parentNode}return[l,c]}_findFirst(a){const e=[],n=this._findNode(a,{node:this.#e});let l=!1;return n&&(e.push(n),l=!0),[e,l]}_findFromHTMLCollection(a,e={}){const{complex:n,compound:l,filterLeaves:s,targetType:c}=e;let r=[],h=!1,f=!1;const t=a.length;if(t)if(this.#e.nodeType===b.ELEMENT_NODE)for(let i=0;i<t;i++){const o=a[i];if(o!==this.#e&&(this.#e.contains(o)||o.contains(this.#e))){if(l){if(this._matchLeaves(s,o,{warn:this.#i})&&(r.push(o),h=!0,c===A))break}else if(r.push(o),h=!0,c===A)break}}else if(n)if(l)for(let i=0;i<t;i++){const o=a[i];if(this._matchLeaves(s,o,{warn:this.#i})&&(r.push(o),h=!0,c===A))break}else r=[].slice.call(a),h=!0,f=!0;else if(l)for(let i=0;i<t;i++){const o=a[i];if(this._matchLeaves(s,o,{warn:this.#i})&&(r.push(o),h=!0,c===A))break}else r=[].slice.call(a),h=!0,f=!0;return[r,h,f]}_findEntryNodes(a,e,n){const{leaves:l}=a,[s,...c]=l,r=c.length>0,{name:h,type:f}=s;let t=[],i=!1,o=!1,m=!1;switch(f){case b.SELECTOR_PSEUDO_ELEMENT:{$.default.matchPseudoElementSelector(h,{warn:this.#i});break}case b.SELECTOR_ID:{if(e===D)[t,o]=this._matchSelf(l);else if(e===R)[t,o]=this._findLineal(l,{complex:n});else if(e===A&&this.#s.nodeType!==b.ELEMENT_NODE){const _=this.#s.getElementById(h);_&&(r?this._matchLeaves(c,_,{warn:this.#i})&&(t.push(_),o=!0):(t.push(_),o=!0))}else e===A?[t,o]=this._findFirst(l):m=!0;break}case b.SELECTOR_CLASS:{if(e===D)[t,o]=this._matchSelf(l);else if(e===R)[t,o]=this._findLineal(l,{complex:n});else if(this.#s.nodeType===b.DOCUMENT_NODE){const _=this.#s.getElementsByClassName(h);_.length&&([t,o,i]=this._findFromHTMLCollection(_,{complex:n,compound:r,filterLeaves:c,targetType:e}))}else e===A?[t,o]=this._findFirst(l):m=!0;break}case b.SELECTOR_TYPE:{if(e===D)[t,o]=this._matchSelf(l);else if(e===R)[t,o]=this._findLineal(l,{complex:n});else if(this.#t.contentType==="text/html"&&this.#s.nodeType===b.DOCUMENT_NODE&&!/[*|]/.test(h)){const _=this.#s.getElementsByTagName(h);_.length&&([t,o,i]=this._findFromHTMLCollection(_,{complex:n,compound:r,filterLeaves:c,targetType:e}))}else e===A?[t,o]=this._findFirst(l):m=!0;break}default:if(e!==R&&b.REG_SHADOW_HOST.test(h)){if(this.#d&&this.#e.nodeType===b.DOCUMENT_FRAGMENT_NODE){const _=this._matchShadowHostPseudoClass(s,this.#e);_&&(t.push(_),o=!0)}}else e===D?[t,o]=this._matchSelf(l):e===R?[t,o]=this._findLineal(l,{complex:n}):e===A?[t,o]=this._findFirst(l):m=!0}return{collected:i,compound:r,filtered:o,nodes:t,pending:m}}_getEntryTwig(a,e){const n=a.length,l=n>1,s=a[0];let c,r;if(l){const{combo:h,leaves:[{name:f,type:t}]}=s,i=a[n-1],{leaves:[{name:o,type:m}]}=i;if(m===b.SELECTOR_PSEUDO_ELEMENT||m===b.SELECTOR_ID)c=O,r=i;else if(t===b.SELECTOR_PSEUDO_ELEMENT||t===b.SELECTOR_ID)c=T,r=s;else if(e===M)if(f==="*"&&t===b.SELECTOR_TYPE)c=O,r=i;else if(o==="*"&&m===b.SELECTOR_TYPE)c=T,r=s;else if(n===2){const{name:_}=h;/^[+~]$/.test(_)?(c=O,r=i):(c=T,r=s)}else c=T,r=s;else if(o==="*"&&m===b.SELECTOR_TYPE)c=T,r=s;else if(f==="*"&&t===b.SELECTOR_TYPE)c=O,r=i;else{let _;for(const{combo:u,leaves:[N]}of a){const{name:k,type:g}=N;if(g===b.SELECTOR_PSEUDO_CLASS&&k==="dir"){_=!1;break}if(!_&&u){const{name:d}=u;/^[+~]$/.test(d)&&(_=!0)}}_?(c=T,r=s):(c=O,r=i)}}else c=O,r=s;return{complex:l,dir:c,twig:r}}_collectNodes(a){const e=this.#a.values();if(a===M||a===A){const n=new Set;let l=0;for(const{branch:s}of e){const{complex:c,dir:r,twig:h}=this._getEntryTwig(s,a),{collected:f,compound:t,filtered:i,nodes:o,pending:m}=this._findEntryNodes(h,a,c);o.length?(this.#a[l].find=!0,this.#h[l]=o):m&&n.add(new Map([["index",l],["twig",h]])),this.#a[l].collected=f,this.#a[l].dir=r,this.#a[l].filtered=i||!t,l++}if(n.size){let s,c;this.#e!==this.#s&&this.#e.nodeType===b.ELEMENT_NODE?(s=this.#e,c=this.#n):(s=this.#s,c=this.#r);let r=this._traverse(s,c);for(;r;){let h=!1;if(this.#e.nodeType===b.ELEMENT_NODE?r===this.#e?h=!0:h=this.#e.contains(r):h=!0,h)for(const f of n){const{leaves:t}=f.get("twig");if(this._matchLeaves(t,r,{warn:this.#i})){const o=f.get("index");this.#a[o].filtered=!0,this.#a[o].find=!0,this.#h[o].push(r)}}r!==c.currentNode&&(r=this._traverse(r,c)),r=c.nextNode()}}}else{let n=0;for(const{branch:l}of e){const s=l[l.length-1],c=l.length>1,{compound:r,filtered:h,nodes:f}=this._findEntryNodes(s,a,c);f.length&&(this.#a[n].find=!0,this.#h[n]=f),this.#a[n].dir=O,this.#a[n].filtered=h||!r,n++}}return[this.#a,this.#h]}_getCombinedNodes(a,e,n){const l=[];for(const s of e){const c=this._matchCombinator(a,s,{dir:n,warn:this.#i});c.size&&l.push(...c)}return l.length?new Set(l):new Set}_matchNodeNext(a,e,n){const{combo:l,index:s}=n,{combo:c,leaves:r}=a[s],h={combo:l,leaves:r},f=this._getCombinedNodes(h,e,T);let t;if(f.size)if(s===a.length-1){const[i]=(0,C.sortNodes)(f);t=i}else t=this._matchNodeNext(a,f,{combo:c,index:s+1});return t??null}_matchNodePrev(a,e,n){const{index:l}=n,s=a[l],c=new Set([e]),r=this._getCombinedNodes(s,c,O);let h;if(r.size){if(l===0)h=e;else for(const f of r)if(this._matchNodePrev(a,f,{index:l-1}))return e}return h??null}_matchNodes(a){const[[...e],n]=this._collectNodes(a),l=e.length;let s=new Set;for(let c=0;c<l;c++){const{branch:r,collected:h,dir:f,find:t}=e[c],i=r.length;if(i&&t){const o=n[c],m=o.length,_=i-1;if(_===0)if((a===M||a===A)&&this.#e.nodeType===b.ELEMENT_NODE)for(let u=0;u<m;u++){const N=o[u];if(N!==this.#e&&this.#e.contains(N)&&(s.add(N),a!==M))break}else if(a===M)if(s.size){const u=[...s];s=new Set([...u,...o]),this.#b=!0}else s=new Set(o);else{const[u]=o;s.add(u)}else if(a===M)if(f===T){let{combo:u}=r[0];for(const N of o){let k=new Set([N]);for(let g=1;g<i;g++){const{combo:d,leaves:p}=r[g],w={combo:u,leaves:p};if(k=this._getCombinedNodes(w,k,f),k.size)if(g===_)if(s.size){const S=[...s];s=new Set([...S,...k]),this.#b=!0}else s=k;else u=d;else break}}}else for(const u of o){let N=new Set([u]);for(let k=_-1;k>=0;k--){const g=r[k];if(N=this._getCombinedNodes(g,N,f),N.size)k===0&&(s.add(u),i>1&&s.size>1&&(this.#b=!0));else break}}else if(a===A&&f===T){const{combo:u}=r[0];let N;for(const k of o)if(N=this._matchNodeNext(r,new Set([k]),{combo:u,index:1}),N){s.add(N);break}if(!N&&!h){const{leaves:k}=r[0],[g]=o;let d=this._findNode(k,{node:g});for(;d;){if(N=this._matchNodeNext(r,new Set([d]),{combo:u,index:1}),N){s.add(N);break}d=this._findNode(k,{node:d})}}}else{let u;for(const N of o)if(u=this._matchNodePrev(r,N,{index:_-1}),u){s.add(N);break}if(!u&&!h&&a===A){const{leaves:N}=r[_],[k]=o;let g=this._findNode(N,{node:k});for(;g;){if(u=this._matchNodePrev(r,g,{index:_-1}),u){s.add(g);break}g=this._findNode(N,{node:g})}}}}}return s}_find(a){return(a===M||a===A)&&this._prepareQuerySelectorWalker(),this._matchNodes(a)}matches(a,e,n){let l;try{if(e?.nodeType!==b.ELEMENT_NODE){const s=`Unexpected node ${e?.nodeName}`;throw new TypeError(s)}(0,y.filterSelector)(a)?l=this.#c.match(a,e):(this._setup(a,e,n),l=this._find(D).size)}catch(s){this._onError(s)}return!!l}closest(a,e,n){let l;try{if(e?.nodeType!==b.ELEMENT_NODE){const s=`Unexpected node ${e?.nodeName}`;throw new TypeError(s)}if((0,y.filterSelector)(a))l=this.#c.closest(a,e);else{this._setup(a,e,n);const s=this._find(R);if(s.size){let c=this.#e;for(;c;){if(s.has(c)){l=c;break}c=c.parentNode}}}}catch(s){this._onError(s)}return l??null}querySelector(a,e,n){let l;try{if(this._setup(a,e,n),this.#f===this.#t&&!this.#o&&(0,y.filterSelector)(a))l=this.#c.first(a,e);else{const s=this._find(A);s.delete(this.#e),s.size&&([l]=(0,C.sortNodes)(s))}}catch(s){this._onError(s)}return l??null}querySelectorAll(a,e,n){let l;try{if(this._setup(a,e,n),this.#f===this.#t&&!this.#o&&(0,y.filterSelector)(a))l=this.#c.select(a,e);else{const s=this._find(M);s.delete(this.#e),s.size&&(this.#b?l=(0,C.sortNodes)(s):l=[...s])}}catch(s){this._onError(s)}return l??[]}}0&&(module.exports={Finder});
2
2
  //# sourceMappingURL=finder.js.map