@asamuzakjp/dom-selector 1.0.1 → 1.0.3

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.
package/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
  [![release](https://img.shields.io/github/v/release/asamuzaK/domSelector)](https://github.com/asamuzaK/domSelector/releases)
9
9
  -->
10
10
 
11
- Retrieve DOM node from the given CSS selector.
11
+ Gets the DOM node that matches the CSS selector.
12
12
 
13
13
  ## Install
14
14
 
@@ -109,7 +109,7 @@ Returns **[Array][62]<([object][60] \| [undefined][63])>** array of matched n
109
109
  |E\[foo*="bar"\]|✓| |
110
110
  |E\[foo\|="en"\]|✓| |
111
111
  |E:dir(ltr)|✓| |
112
- |E:lang(zh, "\*‑hant")|Partially supported|Quoted keys e.g. `:lang("*-Latn")` fails. It succeeds if escaped e.g. `:lang(\*-Latn)`.|
112
+ |E:lang(en)|Partially supported|Comma-separated list of language codes, e.g. `:lang(en, fr)`, is not yet supported.|
113
113
  |E:any‑link|✓| |
114
114
  |E:link|✓| |
115
115
  |E:visited|✓|Returns `false` or `null` to prevent fingerprinting.|
@@ -231,40 +231,40 @@ const dom = new JSDOM('', {
231
231
 
232
232
  ### Performance
233
233
 
234
- |Method and CSS selector|Jsdom|Patched-jsdom|Result|
235
- |:------------|:------------|:------------|:------------|
236
- |matches('.container.box')|1,974,730 ops/sec ±2.26%|96,201 ops/sec ±19.30%|jsdom is 20.5 times faster. patched-jsdom took 0.010msec.|
237
- |matches('.container:not(.box)')|991,905 ops/sec ±4.18%|67,844 ops/sec ±2.04%|jsdom is 14.6 times faster. patched-jsdom took 0.015msec.|
238
- |matches('.box + .box')|1,761,449 ops/sec ±0.59%|92,724 ops/sec ±1.55%|jsdom is 19.0 times faster. patched-jsdom took 0.011msec.|
239
- |matches('.box ~ .box')|1,756,640 ops/sec ±0.52%|92,254 ops/sec ±2.28%|jsdom is 19.0 times faster. patched-jsdom took 0.011msec.|
240
- |matches('.box > .block')|965,850 ops/sec ±0.19%|77,155 ops/sec ±2.01%|jsdom is 12.5 times faster. patched-jsdom took 0.013msec.|
241
- |matches('.box .content')|1,743,309 ops/sec ±0.25%|99,436 ops/sec ±0.68%|jsdom is 17.5 times faster. patched-jsdom took 0.010msec.|
242
- |matches('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner > .content')|1,698,687 ops/sec ±0.38%|33,653 ops/sec ±2.79%|jsdom is 50.5 times faster. patched-jsdom took 0.030msec.|
243
- |matches('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner:has(> .content)')|N/A|7,974 ops/sec ±4.39%|jsdom throws. patched-jsdom took 0.125msec.|
244
- |closest('.container.box')|463,672 ops/sec ±0.24%|52,369 ops/sec ±0.38%|jsdom is 8.9 times faster. patched-jsdom took 0.019msec.|
245
- |closest('.container:not(.box)')|263,034 ops/sec ±0.32%|32,326 ops/sec ±2.52%|jsdom is 8.1 times faster. patched-jsdom took 0.031msec.|
246
- |closest('.box + .box')|421,756 ops/sec ±2.42%|49,539 ops/sec ±1.98%|jsdom is 8.5 times faster. patched-jsdom took 0.020msec.|
247
- |closest('.box ~ .box')|144,622 ops/sec ±0.38%|29,353 ops/sec ±2.29%|jsdom is 4.9 times faster. patched-jsdom took 0.034msec.|
248
- |closest('.box > .block')|454,446 ops/sec ±0.31%|46,650 ops/sec ±2.24%|jsdom is 9.7 times faster. patched-jsdom took 0.021msec.|
249
- |closest('.box .content')|275,146 ops/sec ±0.23%|55,813 ops/sec ±2.08%|jsdom is 4.9 times faster. patched-jsdom took 0.018msec.|
250
- |closest('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner > .content')|275,823 ops/sec ±0.25%|26,754 ops/sec ±1.96%|jsdom is 10.3 times faster. patched-jsdom took 0.037msec.|
251
- |closest('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner:has(> .content)')|N/A|7,397 ops/sec ±1.94%|jsdom throws. patched-jsdom took 0.135msec.|
252
- |querySelector('.container.box')|68,619 ops/sec ±0.41%|21,887 ops/sec ±1.07%|jsdom is 3.1 times faster. patched-jsdom took 0.046msec.|
253
- |querySelector('.container:not(.box)')|58,150 ops/sec ±2.03%|16,315 ops/sec ±0.30%|jsdom is 3.6 times faster. patched-jsdom took 0.061msec.|
254
- |querySelector('.box + .box')|56,085 ops/sec ±0.23%|17,980 ops/sec ±0.43%|jsdom is 3.1 times faster. patched-jsdom took 0.056msec.|
255
- |jsdom querySelector('.box ~ .box')|58,053 ops/sec ±0.45%|8,473 ops/sec ±0.32%|jsdom is 6.9 times faster. patched-jsdom took 0.118msec.|
256
- |querySelector('.box > .block')|834 ops/sec ±0.23%|2,513 ops/sec ±0.32%|patched-jsdom is 3.0 times faster. patched-jsdom took 0.398msec.|
257
- |querySelector('.box .content')|422 ops/sec ±0.31%|107 ops/sec ±0.33%|jsdom is 3.9 times faster. patched-jsdom took 9.342msec.|
258
- |querySelector('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner > .content')|181 ops/sec ±0.37%|236 ops/sec ±1.38%|patched-jsdom is 1.3 times faster. patched-jsdom took 4.231msec.|
259
- |querySelector('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner:has(> .content)')|N/A|215 ops/sec ±1.77%|jsdom throws. patched-jsdom took 4.648msec.|
260
- |querySelectorAll('.container.box')|80,975 ops/sec ±1.87%|18,873 ops/sec ±2.83%|jsdom is 4.3 times faster. patched-jsdom took 0.053msec.|
261
- |querySelectorAll('.container:not(.box)')|72,800 ops/sec ±0.36%|15,009 ops/sec ±1.78%|jsdom is 4.9 times faster. patched-jsdom took 0.067msec.|
262
- |querySelectorAll('.box + .box')|73,029 ops/sec ±2.18%|17,280 ops/sec ±2.70%|jsdom is 4.2 times faster. patched-jsdom took 0.058msec.|
263
- |querySelectorAll('.box ~ .box')|73,630 ops/sec ±0.56%|8,192 ops/sec ±0.30%|jsdom is 9.0 times faster. patched-jsdom took 0.122msec.|
264
- |jsdom querySelectorAll('.box > .block')|806 ops/sec ±0.27%|2,096 ops/sec ±1.77%|patched-jsdom is 2.6 times faster. patched-jsdom took 0.477msec.|
265
- |querySelectorAll('.box .content')|409 ops/sec ±2.06%|103 ops/sec ±1.21%|jsdom is 4.0 times faster. patched-jsdom took 9.721msec.|
266
- |querySelectorAll('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner > .content')|184 ops/sec ±1.74%|240 ops/sec ±0.88%|patched-jsdom is 1.3 times faster. patched-jsdom took 4.173msec.|
267
- |querySelectorAll('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner:has(> .content)')|N/A|214 ops/sec ±1.32%|jsdom throws. patched-jsdom took 4.673msec.|
234
+ |Method / Selector|Jsdom|Patched-jsdom|Result|
235
+ |:----------------|:----------------|:----------------|:----------------|
236
+ |matches('.container.box')|1,642,216 ops/sec ±3.43%|64,823 ops/sec ±8.74%|jsdom is 25.3 times faster. patched-jsdom took 0.015msec.|
237
+ |matches('.container:not(.box)')|829,464 ops/sec ±3.61%|39,531 ops/sec ±4.15%|jsdom is 21.0 times faster. patched-jsdom took 0.025msec.|
238
+ |matches('.box + .box')|1,598,580 ops/sec ±1.44%|66,518 ops/sec ±2.04%|jsdom is 24.0 times faster. patched-jsdom took 0.015msec.|
239
+ |matches('.box ~ .box')|1,623,837 ops/sec ±1.21%|65,891 ops/sec ±0.94%|jsdom is 24.6 times faster. patched-jsdom took 0.015msec.|
240
+ |matches('.box > .block')|1,621,883 ops/sec ±0.31%|63,556 ops/sec ±2.48%|jsdom is 25.5 times faster. patched-jsdom took 0.016msec.|
241
+ |matches('.box .content')|244,534 ops/sec ±0.48%|36,005 ops/sec ±2.06%|jsdom is 6.8 times faster. patched-jsdom took 0.028msec.|
242
+ |matches('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner > .content')|105,311 ops/sec ±3.69%|7,098 ops/sec ±2.10%|jsdom is 14.8 times faster. patched-jsdom took 0.141msec.|
243
+ |matches('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner:has(> .content)')|N/A|22,913 ops/sec ±1.68%|jsdom throws. patched-jsdom took 0.044msec.|
244
+ |closest('.container.box')|319,262 ops/sec ±0.89%|30,493 ops/sec ±3.23%|jsdom is 10.5 times faster. patched-jsdom took 0.033msec.|
245
+ |closest('.container:not(.box)')|179,788 ops/sec ±0.61%|20,566 ops/sec ±2.22%|jsdom is 8.7 times faster. patched-jsdom took 0.049msec.|
246
+ |closest('.box + .box')|294,027 ops/sec ±0.79%|32,386 ops/sec ±1.67%|jsdom is 9.1 times faster. patched-jsdom took 0.031msec.|
247
+ |closest('.box ~ .box')|101,510 ops/sec ±3.61%|20,699 ops/sec ±2.35%|jsdom is 4.9 times faster. patched-jsdom took 0.048msec.|
248
+ |closest('.box > .block')|314,456 ops/sec ±1.27%|30,538 ops/sec ±1.37%|jsdom is 10.3 times faster. patched-jsdom took 0.033msec.|
249
+ |closest('.box .content')|228,070 ops/sec ±0.68%|24,453 ops/sec ±1.96%|jsdom is 9.3 times faster. patched-jsdom took 0.041msec.|
250
+ |closest('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner > .content')|99,675 ops/sec ±1.33%|6,632 ops/sec ±2.70%|jsdom is 15.0 times faster. patched-jsdom took 0.151msec.|
251
+ |closest('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner:has(> .content)')|N/A|5,411 ops/sec ±1.75%|jsdom throws. patched-jsdom took 0.185msec.|
252
+ |querySelector('.container.box')|43,668 ops/sec ±2.03%|15,026 ops/sec ±3.43%|jsdom is 2.9 times faster. patched-jsdom took 0.067msec.|
253
+ |querySelector('.container:not(.box)')|31,873 ops/sec ±2.43%|10,341 ops/sec ±1.99%|jsdom is 3.1 times faster. patched-jsdom took 0.097msec.|
254
+ |querySelector('.box + .box')|29,090 ops/sec ±5.25%|10,685 ops/sec ±7.23%|jsdom is 2.7 times faster. patched-jsdom took 0.094msec.|
255
+ |jsdom querySelector('.box ~ .box')|34,395 ops/sec ±2.16%|6,492 ops/sec ±1.80%|jsdom is 5.3 times faster. patched-jsdom took 0.154msec.|
256
+ |querySelector('.box > .block')|707 ops/sec ±2.04%|2,037 ops/sec ±2.42%|patched-jsdom is 2.9 times faster. patched-jsdom took 0.491msec.|
257
+ |querySelector('.box .content')|307 ops/sec ±2.88%|196 ops/sec ±1.99%|jsdom is 1.6 times faster. patched-jsdom took 5.110msec.|
258
+ |querySelector('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner > .content')|138 ops/sec ±3.87%|323 ops/sec ±3.10%|patched-jsdom is 2.3 times faster. patched-jsdom took 3.100msec.|
259
+ |querySelector('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner:has(> .content)')|N/A|260 ops/sec ±3.65%|jsdom throws. patched-jsdom took 3.846msec.|
260
+ |querySelectorAll('.container.box')|62,222 ops/sec ±2.29%|15,138 ops/sec ±1.13%|jsdom is 4.1 times faster. patched-jsdom took 0.066msec.|
261
+ |querySelectorAll('.container:not(.box)')|52,862 ops/sec ±1.82%|11,352 ops/sec ±1.30%|jsdom is 4.7 times faster. patched-jsdom took 0.088msec.|
262
+ |querySelectorAll('.box + .box')|50,474 ops/sec ±2.32%|13,338 ops/sec ±1.24%|jsdom is 3.8 times faster. patched-jsdom took 0.075msec.|
263
+ |querySelectorAll('.box ~ .box')|51,083 ops/sec ±0.69%|6,620 ops/sec ±0.92%|jsdom is 7.7 times faster. patched-jsdom took 0.151msec.|
264
+ |jsdom querySelectorAll('.box > .block')|691 ops/sec ±1.53%|1,729 ops/sec ±2.07%|patched-jsdom is 2.5 times faster. patched-jsdom took 0.578msec.|
265
+ |querySelectorAll('.box .content')|305 ops/sec ±2.24%|182 ops/sec ±2.36%|jsdom is 1.7 times faster. patched-jsdom took 5.502msec.|
266
+ |querySelectorAll('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner > .content')|145 ops/sec ±1.60%|333 ops/sec ±2.64%|patched-jsdom is 2.3 times faster. patched-jsdom took 2.999msec.|
267
+ |querySelectorAll('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner:has(> .content)')|N/A| 267 ops/sec ±2.17%|jsdom throws. patched-jsdom took 3.738msec.|
268
268
 
269
269
 
270
270
  ## Acknowledgments
@@ -1,2 +1,2 @@
1
- var j=Object.create;var L=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var W=Object.getOwnPropertyNames;var q=Object.getPrototypeOf,G=Object.prototype.hasOwnProperty;var Y=(y,l)=>{for(var e in l)L(y,e,{get:l[e],enumerable:!0})},R=(y,l,e,f)=>{if(l&&typeof l=="object"||typeof l=="function")for(let c of W(l))!G.call(y,c)&&c!==e&&L(y,c,{get:()=>l[c],enumerable:!(f=H(l,c))||f.enumerable});return y};var X=(y,l,e)=>(e=y!=null?j(q(y)):{},R(l||!y||!y.__esModule?L(e,"default",{value:y,enumerable:!0}):e,y)),Z=y=>R(L({},"__esModule",{value:!0}),y);var ee={};Y(ee,{Matcher:()=>Q});module.exports=Z(ee);var D=X(require("is-potential-custom-element-name"),1),k=require("./dom-util.js"),g=require("./parser.js"),b=require("./constant.js");const C="all",A="first",v="lineal",S="self",U=/^(?:(?:fieldse|inpu|selec)t|button|opt(?:group|ion)|textarea)$/,O=/^(?:(?:(?:in|out)pu|selec)t|button|form|textarea)$/,M=/^a(?:rea)?$/,B=/^d(?:etails|ialog)$/,$=/^(?:checkbox|radio)$/,x=/^(?:(?:emai|te|ur)l|number|password|search|text)$/,z=/(?:(?:rang|tim)e|date(?:time-local)?|month|number|week)$/,F=/^(?:button|reset)$/,V=/^(?:image|submit)$/,I=/^(?:date(?:time-local)?|month|time|week)$/,K=/^(?:(?:ha|i)s|not|where)$/,J=/^nth-(?:last-)?(?:child|of-type)$/;class Q{#s;#r;#a;#e;#l;#t;#o;#c;#i;constructor(l,e,f={}){const{sort:c,warn:n}=f;this.#r=new Map([[b.ATTRIBUTE_SELECTOR,b.BIT_10000],[b.CLASS_SELECTOR,b.BIT_100],[b.ID_SELECTOR,b.BIT_10],[b.PSEUDO_CLASS_SELECTOR,b.BIT_100000],[b.PSEUDO_ELEMENT_SELECTOR,b.BIT_1],[b.TYPE_SELECTOR,b.BIT_1000]]),this.#a=new WeakMap,this.#o=l,this.#e=e,this.#c=!!c,this.#i=!!n,[this.#s,this.#l]=this._prepare(l),this.#t=this._getRoot(e)}_onError(l){if(l instanceof DOMException&&l.name===b.NOT_SUPPORTED_ERR)this.#i&&console.warn(l.message);else throw l}_getRoot(l=this.#e){let e,f;switch(l.nodeType){case b.DOCUMENT_NODE:{e=l,f=l;break}case b.DOCUMENT_FRAGMENT_NODE:{e=l.ownerDocument,f=l;break}case b.ELEMENT_NODE:{if((0,k.isSameOrDescendant)(l))e=l.ownerDocument,f=l.ownerDocument;else{let c=l;for(;c&&c.parentNode;)c=c.parentNode;e=c.ownerDocument,f=c}break}default:throw new TypeError(`Unexpected node ${l.nodeName}`)}return{document:e,root:f}}_sortLeaves(l){const e=[...l];return e.length>1&&e.sort((f,c)=>{const{type:n}=f,{type:d}=c,p=this.#r.get(n),u=this.#r.get(d);let t;return p===u?t=0:p>u?t=1:t=-1,t}),e}_prepare(l=this.#o){const e=(0,g.parseSelector)(l),f=(0,g.walkAST)(e),c=[],n=[];let d=0;for(const[...p]of f){const u=[];let t=p.shift();if(t&&t.type!==b.COMBINATOR){const i=new Set;for(;t;){if(t.type===b.COMBINATOR){const[h]=p;if(h.type===b.COMBINATOR){const r=`Invalid combinator ${t.name}${h.name}`;throw new DOMException(r,b.SYNTAX_ERR)}u.push({combo:t,leaves:this._sortLeaves(i)}),i.clear()}else t&&i.add(t);if(p.length)t=p.shift();else{u.push({combo:null,leaves:this._sortLeaves(i)}),i.clear();break}}}c.push({branch:u,find:null,skip:!1}),n[d]=new Set,d++}return[c,n]}_collectNthChild(l,e){const{a:f,b:c,reverse:n,selector:d}=l,{parentNode:p}=e,u=new Set;let t;if(d&&(this.#a.has(d)?t=this.#a.get(d):(t=(0,g.walkAST)(d),this.#a.set(d,t))),p){const i=[...p.children],h=i.length;if(h){const r=new Set;if(t){const o=t.length;for(const s of i){let a;for(let m=0;m<o;m++){const N=t[m];if(a=this._matchLeaves(N,s),!a)break}a&&r.add(s)}}if(n&&i.reverse(),f===0){if(c>0&&c<=h){if(r.size)for(let o=0;o<h;o++){const s=i[o];if(r.has(s)){u.add(s);break}}else if(!d){const o=i[c-1];u.add(o)}}}else{let o=0,s=c-1;if(f>0)for(;s<0;)s+=++o*f;if(s>=0&&s<h){let a=f>0?0:c-1;for(let m=0;m<h&&s>=0&&s<h;m++){const N=i[m];r.size?r.has(N)&&(a===s&&(u.add(N),s+=f),f>0?a++:a--):m===s&&(d||u.add(N),s+=f)}}}}}else{const{root:i}=this.#t;if(i.nodeType===b.ELEMENT_NODE&&e===i&&f+c===1)if(t){const h=t.length;let r;for(let o=0;o<h;o++){const s=t[o];if(r=this._matchLeaves(s,e),r)break}r&&u.add(e)}else u.add(e)}return u}_collectNthOfType(l,e){const{a:f,b:c,reverse:n}=l,{localName:d,parentNode:p,prefix:u}=e,t=new Set;if(p){const i=[...p.children],h=i.length;if(h)if(n&&i.reverse(),f===0){if(c>0&&c<=h){let r=0;for(let o=0;o<h;o++){const s=i[o],{localName:a,prefix:m}=s;if(a===d&&m===u){if(r===c-1){t.add(s);break}r++}}}}else{let r=c-1;if(f>0)for(;r<0;)r+=f;if(r>=0&&r<h){let o=f>0?0:c-1;for(let s=0;s<h;s++){const a=i[s],{localName:m,prefix:N}=a;if(m===d&&N===u){if(o===r&&(t.add(a),r+=f),r<0||r>=h)break;f>0?o++:o--}}}}}else{const{root:i}=this.#t;i.nodeType===b.ELEMENT_NODE&&e===i&&f+c===1&&t.add(e)}return t}_matchAnPlusB(l,e,f){const{nth:{a:c,b:n,name:d},selector:p}=l,u=(0,g.unescapeSelector)(d),t=new Map;u?(u==="even"?(t.set("a",2),t.set("b",0)):u==="odd"&&(t.set("a",2),t.set("b",1)),/last/.test(f)&&t.set("reverse",!0)):(typeof c=="string"&&/-?\d+/.test(c)?t.set("a",c*1):t.set("a",0),typeof n=="string"&&/-?\d+/.test(n)?t.set("b",n*1):t.set("b",0),/last/.test(f)&&t.set("reverse",!0));let i=new Set;if(t.has("a")&&t.has("b")){if(/^nth-(?:last-)?child$/.test(f)){p&&t.set("selector",p);const h=Object.fromEntries(t),r=this._collectNthChild(h,e);r.size&&(i=r)}else if(/^nth-(?:last-)?of-type$/.test(f)){const h=Object.fromEntries(t),r=this._collectNthOfType(h,e);r.size&&(i=r)}}return i}_matchPseudoElementSelector(l,e={}){const{forgive:f}=e;switch(l){case"after":case"backdrop":case"before":case"cue":case"cue-region":case"first-letter":case"first-line":case"file-selector-button":case"marker":case"part":case"placeholder":case"selection":case"slotted":case"target-text":{if(this.#i)throw new DOMException(`Unsupported pseudo-element ::${l}`,b.NOT_SUPPORTED_ERR);break}default:if(l.startsWith("-webkit-")){if(this.#i)throw new DOMException(`Unsupported pseudo-element ::${l}`,b.NOT_SUPPORTED_ERR)}else if(!f)throw new DOMException(`Unknown pseudo-element ::${l}`,b.SYNTAX_ERR)}}_matchDirectionPseudoClass(l,e){const f=(0,g.unescapeSelector)(l.name),c=(0,k.getDirectionality)(e);let n;return f===c&&(n=e),n??null}_matchLanguagePseudoClass(l,e){const{lang:f}=e,c=(0,g.unescapeSelector)(l.name);let n;if(c==="")e.getAttribute("lang")===""&&(n=e);else if(c==="*")e.hasAttribute("lang")||(n=e);else if(/[A-Z\d-]+/i.test(c)){const d="(?:-[A-Za-z\\d]+)?";let p;if(/-/.test(c)){const[u,t,...i]=c.split("-"),h=`${u}${d}`,r=`-${t}${d}`,o=i.length;let s="";if(o)for(let a=0;a<o;a++)s+=`-${i[a]}${d}`;p=new RegExp(`^${h}${r}${s}$`,"i")}else p=new RegExp(`^${c}${d}$`,"i");if(f)p.test(f)&&(n=e);else{let u=e;for(;u;){if(p.test(u.lang)){n=e;break}u=u.parentNode}}}return n??null}_matchHasPseudoFunc(l,e){let f;if(Array.isArray(l)&&l.length){const[c]=l,{type:n}=c;let d;n===b.COMBINATOR?d=l.shift():d={name:" ",type:b.COMBINATOR};const p=[];for(;l.length;){const[i]=l,{type:h}=i;if(h===b.COMBINATOR)break;p.push(l.shift())}const u={combo:d,leaves:p},t=this._matchCombinator(u,e,{find:"next"});if(t.size)if(l.length){for(const i of t)if(f=this._matchHasPseudoFunc(Object.assign([],l),i),f)break}else f=!0}return!!f}_matchLogicalPseudoFunc(l,e){const{astName:f="",branches:c=[],selector:n="",twigBranches:d=[]}=l;let p;if(f==="has")if(n.includes(":has("))p=null;else{let u;const t=c.length;for(let i=0;i<t;i++){const h=c[i];if(u=this._matchHasPseudoFunc(Object.assign([],h),e),u)break}u&&(p=e)}else{const u=/^(?:is|where)$/.test(f);let t;const i=d.length;for(let h=0;h<i;h++){const r=d[h],o=r.length-1,{leaves:s}=r[o];if(t=this._matchLeaves(s,e,{forgive:u}),t&&o>0){let a=new Set([e]);for(let m=o-1;m>=0;m--){const N=r[m],w=[];for(const _ of a){const T=this._matchCombinator(N,_,{forgive:u,find:"prev"});T.size&&w.push(...T)}const E=new Set(w);if(E.size)if(m===0){t=!0;break}else a=E;else{t=!1;break}}}if(t)break}f==="not"?t||(p=e):t&&(p=e)}return p??null}_matchPseudoClassSelector(l,e,f={}){const{children:c}=l,{localName:n,parentNode:d}=e,{forgive:p}=f,u=(0,g.unescapeSelector)(l.name);let t=new Set;if(K.test(u)){let i;if(this.#a.has(l))i=this.#a.get(l);else{const r=(0,g.walkAST)(l),o=[],s=[];for(const[...a]of r){for(const E of a){const _=(0,g.generateCSS)(E);o.push(_)}const m=[],N=new Set;let w=a.shift();for(;w;)if(w.type===b.COMBINATOR?(m.push({combo:w,leaves:[...N]}),N.clear()):w&&N.add(w),a.length)w=a.shift();else{m.push({combo:null,leaves:[...N]}),N.clear();break}s.push(m)}i={astName:u,branches:r,twigBranches:s,selector:o.join(",")},this.#a.set(l,i)}const h=this._matchLogicalPseudoFunc(i,e);h&&t.add(h)}else if(Array.isArray(c)){const[i]=c;if(J.test(u)){const h=this._matchAnPlusB(i,e,u);h.size&&(t=h)}else if(u==="dir"){const h=this._matchDirectionPseudoClass(i,e);h&&t.add(h)}else if(u==="lang"){const h=this._matchLanguagePseudoClass(i,e);h&&t.add(h)}else switch(u){case"current":case"nth-col":case"nth-last-col":{if(this.#i)throw new DOMException(`Unsupported pseudo-class :${u}()`,b.NOT_SUPPORTED_ERR);break}default:if(!p)throw new DOMException(`Unknown pseudo-class :${u}()`,b.SYNTAX_ERR)}}else{const{document:i,root:h}=this.#t,{documentElement:r}=i,o=new URL(i.URL);switch(u){case"any-link":case"link":{M.test(n)&&e.hasAttribute("href")&&t.add(e);break}case"local-link":{if(M.test(n)&&e.hasAttribute("href")){const s=new URL(e.getAttribute("href"),o.href);s.origin===o.origin&&s.pathname===o.pathname&&t.add(e)}break}case"visited":break;case"target":{(0,k.isSameOrDescendant)(e)&&o.hash&&e.id&&o.hash===`#${e.id}`&&t.add(e);break}case"target-within":{if(o.hash){const s=o.hash.replace(/^#/,"");let a=i.getElementById(s);for(;a;){if(a===e){t.add(e);break}a=a.parentNode}}break}case"scope":{this.#e.nodeType===b.ELEMENT_NODE?e===this.#e&&t.add(e):e===r&&t.add(e);break}case"focus":{e===i.activeElement&&t.add(e);break}case"focus-within":{let s=i.activeElement;for(;s;){if(s===e){t.add(e);break}s=s.parentNode}break}case"open":{B.test(n)&&e.hasAttribute("open")&&t.add(e);break}case"closed":{B.test(n)&&!e.hasAttribute("open")&&t.add(e);break}case"disabled":{if(U.test(n)||(0,D.default)(n))if(e.disabled||e.hasAttribute("disabled"))t.add(e);else{let s=d;for(;s&&s.localName!=="fieldset";)s=s.parentNode;s&&s.hasAttribute("disabled")&&d.localName!=="legend"&&t.add(e)}break}case"enabled":{(U.test(n)||(0,D.default)(n))&&!(e.disabled&&e.hasAttribute("disabled"))&&t.add(e);break}case"read-only":{switch(n){case"textarea":{(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&t.add(e);break}case"input":{(!e.type||x.test(e.type)||I.test(e.type))&&(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&t.add(e);break}default:(0,k.isContentEditable)(e)||t.add(e)}break}case"read-write":{switch(n){case"textarea":{e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled")||t.add(e);break}case"input":{(!e.type||x.test(e.type)||I.test(e.type))&&!(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&t.add(e);break}default:(0,k.isContentEditable)(e)&&t.add(e)}break}case"placeholder-shown":{let s;n==="textarea"?s=e:n==="input"&&(e.hasAttribute("type")?x.test(e.getAttribute("type"))&&(s=e):s=e),s&&e.hasAttribute("placeholder")&&e.getAttribute("placeholder").trim().length&&e.value===""&&t.add(e);break}case"checked":{(n==="input"&&e.hasAttribute("type")&&$.test(e.getAttribute("type"))&&e.checked||n==="option"&&e.selected)&&t.add(e);break}case"indeterminate":{if(n==="input"&&e.type==="checkbox"&&e.indeterminate||n==="progress"&&!e.hasAttribute("value"))t.add(e);else if(n==="input"&&e.type==="radio"&&!e.hasAttribute("checked")){const s=e.name;let a=e.parentNode;for(;a&&a.localName!=="form";)a=a.parentNode;a||(a=r);const m=[...a.getElementsByTagName("input")];let N;for(const w of m)if(w.getAttribute("type")==="radio"&&(s?w.getAttribute("name")===s&&(N=!!w.checked):w.hasAttribute("name")||(N=!!w.checked),N))break;N||t.add(e)}break}case"default":{if(n==="button"&&!(e.hasAttribute("type")&&F.test(e.getAttribute("type")))||n==="input"&&e.hasAttribute("type")&&V.test(e.getAttribute("type"))){let s=e.parentNode;for(;s&&s.localName!=="form";)s=s.parentNode;if(s){const a=i.createNodeIterator(s,b.SHOW_ELEMENT);let m=a.nextNode();for(;m;){const N=m.localName;let w;if(N==="button"?w=!(m.hasAttribute("type")&&F.test(m.getAttribute("type"))):N==="input"&&(w=m.hasAttribute("type")&&V.test(m.getAttribute("type"))),w){m===e&&t.add(e);break}m=a.nextNode()}}}else if(n==="input"&&e.hasAttribute("type")&&$.test(e.getAttribute("type"))&&(e.checked||e.hasAttribute("checked")))t.add(e);else if(n==="option"){let s=!1,a=d;for(;a&&a.localName!=="datalist";){if(a.localName==="select"){(a.multiple||a.hasAttribute("multiple"))&&(s=!0);break}a=a.parentNode}if(s)(e.selected||e.hasAttribute("selected"))&&t.add(e);else{const m=d.firstElementChild,N=new Set;let w=m;for(;w;){if(w.selected||w.hasAttribute("selected")){N.add(w);break}w=w.nextElementSibling}N.size||N.add(m),N.has(e)&&t.add(e)}}break}case"valid":{if(O.test(n))e.checkValidity()&&t.add(e);else if(/^fieldset$/.test(n)){const s=i.createNodeIterator(e,b.SHOW_ELEMENT);let a=s.nextNode();a===e&&(a=s.nextNode());let m;for(;a&&!(O.test(a.localName)&&(m=a.checkValidity(),!m));)a=s.nextNode();m&&t.add(e)}break}case"invalid":{if(O.test(n))e.checkValidity()||t.add(e);else if(/^fieldset$/.test(n)){const s=i.createNodeIterator(e,b.SHOW_ELEMENT);let a=s.nextNode();a===e&&(a=s.nextNode());let m;for(;a&&!(O.test(a.localName)&&(m=a.checkValidity(),!m));)a=s.nextNode();m||t.add(e)}break}case"in-range":{n==="input"&&!(e.readonly||e.hasAttribute("readonly"))&&!(e.disabled||e.hasAttribute("disabled"))&&e.hasAttribute("type")&&z.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":{n==="input"&&!(e.readonly||e.hasAttribute("readonly"))&&!(e.disabled||e.hasAttribute("disabled"))&&e.hasAttribute("type")&&z.test(e.getAttribute("type"))&&(e.validity.rangeUnderflow||e.validity.rangeOverflow)&&t.add(e);break}case"required":{let s;if(/^(?:select|textarea)$/.test(n))s=e;else if(n==="input")if(e.hasAttribute("type")){const a=e.getAttribute("type");(x.test(a)||$.test(a)||I.test(a)||a==="file")&&(s=e)}else s=e;s&&(e.required||e.hasAttribute("required"))&&t.add(e);break}case"optional":{let s;if(/^(?:select|textarea)$/.test(n))s=e;else if(n==="input")if(e.hasAttribute("type")){const a=e.getAttribute("type");(x.test(a)||$.test(a)||I.test(a)||a==="file")&&(s=e)}else s=e;s&&!(e.required||e.hasAttribute("required"))&&t.add(e);break}case"root":{e===r&&t.add(e);break}case"empty":{if(e.hasChildNodes()){const s=e.childNodes.values();let a;for(const m of s)if(a=m.nodeType!==b.ELEMENT_NODE&&m.nodeType!==b.TEXT_NODE,!a)break;a&&t.add(e)}else t.add(e);break}case"first-child":{(d&&e===d.firstElementChild||h.nodeType===b.ELEMENT_NODE&&e===h)&&t.add(e);break}case"last-child":{(d&&e===d.lastElementChild||h.nodeType===b.ELEMENT_NODE&&e===h)&&t.add(e);break}case"only-child":{(d&&e===d.firstElementChild&&e===d.lastElementChild||h.nodeType===b.ELEMENT_NODE&&e===h)&&t.add(e);break}case"first-of-type":{if(d){const[s]=this._collectNthOfType({a:0,b:1},e);s&&t.add(s)}else h.nodeType===b.ELEMENT_NODE&&e===h&&t.add(e);break}case"last-of-type":{if(d){const[s]=this._collectNthOfType({a:0,b:1,reverse:!0},e);s&&t.add(s)}else h.nodeType===b.ELEMENT_NODE&&e===h&&t.add(e);break}case"only-of-type":{if(d){const[s]=this._collectNthOfType({a:0,b:1},e);if(s===e){const[a]=this._collectNthOfType({a:0,b:1,reverse:!0},e);a===e&&t.add(e)}}else h.nodeType===b.ELEMENT_NODE&&e===h&&t.add(e);break}case"after":case"before":case"first-letter":case"first-line":{if(this.#i)throw new DOMException(`Unsupported pseudo-element ::${u}`,b.NOT_SUPPORTED_ERR);break}case"active":case"autofill":case"blank":case"buffering":case"current":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(this.#i)throw new DOMException(`Unsupported pseudo-class :${u}`,b.NOT_SUPPORTED_ERR);break}default:if(u.startsWith("-webkit-")){if(this.#i)throw new DOMException(`Unsupported pseudo-class :${u}`,b.NOT_SUPPORTED_ERR)}else if(!p)throw new DOMException(`Unknown pseudo-class :${u}`,b.SYNTAX_ERR)}}return t}_matchAttributeSelector(l,e){const{flags:f,matcher:c,name:n,value:d}=l;if(typeof f=="string"&&!/^[is]$/i.test(f))throw new DOMException("Invalid attribute selector",b.SYNTAX_ERR);const{attributes:p}=e;let u;if(p&&p.length){const{document:t}=this.#t;let i;t.contentType==="text/html"?typeof f=="string"&&/^s$/i.test(f)?i=!1:i=!0:typeof f=="string"&&/^i$/i.test(f)?i=!0:i=!1;let{name:h}=n;h=(0,g.unescapeSelector)(h),i&&(h=h.toLowerCase());const r=new Set;if(/\|/.test(h)){const{prefix:o,tagName:s}=(0,k.selectorToNodeProps)(h);for(let{name:a,value:m}of p)switch(i&&(a=a.toLowerCase(),m=m.toLowerCase()),o){case"":{s===a&&r.add(m);break}case"*":{/:/.test(a)?a.endsWith(`:${s}`)&&r.add(m):s===a&&r.add(m);break}default:if(/:/.test(a)){const[N,w]=a.split(":");o===N&&s===w&&(0,k.isNamespaceDeclared)(o,e)&&r.add(m)}}}else for(let{name:o,value:s}of p)if(i&&(o=o.toLowerCase(),s=s.toLowerCase()),/:/.test(o)){const[,a]=o.split(":");h===a&&r.add(s)}else h===o&&r.add(s);if(r.size){const{name:o,value:s}=d||{};let a;switch(o?i?a=o.toLowerCase():a=o:s?i?a=s.toLowerCase():a=s:s===""&&(a=s),c){case"=":{typeof a=="string"&&r.has(a)&&(u=e);break}case"~=":{if(a&&typeof a=="string"){for(const m of r)if(new Set(m.split(/\s+/)).has(a)){u=e;break}}break}case"|=":{if(a&&typeof a=="string"){let m;for(const N of r)if(N===a||N.startsWith(`${a}-`)){m=N;break}m&&(u=e)}break}case"^=":{if(a&&typeof a=="string"){let m;for(const N of r)if(N.startsWith(`${a}`)){m=N;break}m&&(u=e)}break}case"$=":{if(a&&typeof a=="string"){let m;for(const N of r)if(N.endsWith(`${a}`)){m=N;break}m&&(u=e)}break}case"*=":{if(a&&typeof a=="string"){let m;for(const N of r)if(N.includes(`${a}`)){m=N;break}m&&(u=e)}break}case null:default:u=e}}}return u??null}_matchClassSelector(l,e){const f=(0,g.unescapeSelector)(l.name);let c;return e.classList.contains(f)&&(c=e),c??null}_matchIDSelector(l,e){const{id:f}=e,c=(0,g.unescapeSelector)(l.name);let n;return c===f&&(n=e),n??null}_matchTypeSelector(l,e){const f=(0,g.unescapeSelector)(l.name),{localName:c,prefix:n}=e,{document:d}=this.#t;let{prefix:p,tagName:u}=(0,k.selectorToNodeProps)(f,e);d.contentType==="text/html"&&(p=p.toLowerCase(),u=u.toLowerCase());let t,i;/:/.test(c)?[t,i]=c.split(":"):(t=n||"",i=c);let h;return p===""&&t===""?e.namespaceURI===null&&(u==="*"||u===i)&&(h=e):p==="*"?(u==="*"||u===i)&&(h=e):p===t&&(0,k.isNamespaceDeclared)(p,e)&&(u==="*"||u===i)&&(h=e),h??null}_matchSelector(l,e,f){const{type:c}=l;let n=new Set;if(e.nodeType===b.ELEMENT_NODE)switch(c){case b.ATTRIBUTE_SELECTOR:{const d=this._matchAttributeSelector(l,e);d&&n.add(d);break}case b.CLASS_SELECTOR:{const d=this._matchClassSelector(l,e);d&&n.add(d);break}case b.ID_SELECTOR:{const d=this._matchIDSelector(l,e);d&&n.add(d);break}case b.PSEUDO_CLASS_SELECTOR:{const d=this._matchPseudoClassSelector(l,e,f);d.size&&(n=d);break}case b.PSEUDO_ELEMENT_SELECTOR:{const d=(0,g.unescapeSelector)(l.name);this._matchPseudoElementSelector(d,f);break}case b.TYPE_SELECTOR:default:{const d=this._matchTypeSelector(l,e);d&&n.add(d)}}return n}_matchLeaves(l,e,f){let c;for(const n of l)if(c=this._matchSelector(n,e,f).has(e),!c)break;return!!c}_findDescendantNodes(l,e){const[f,...c]=l,{type:n}=f,d=(0,g.unescapeSelector)(f.name),p=c.length>0,{document:u,root:t}=this.#t;let i=new Set,h=!1;switch(n){case b.ID_SELECTOR:{if(t.nodeType===b.ELEMENT_NODE)h=!0;else{const r=t.getElementById(d);if(r&&r!==e){const o=(0,k.isSameOrDescendant)(r,e);let s;o&&(s=r),s&&(p?this._matchLeaves(c,s)&&i.add(s):i.add(s))}}break}case b.CLASS_SELECTOR:{const r=[...e.getElementsByClassName(d)];if(r.length)if(p)for(const o of r)this._matchLeaves(c,o)&&i.add(o);else i=new Set(r);break}case b.TYPE_SELECTOR:{if(u.contentType!=="text/html"||/[*|]/.test(d))h=!0;else{const r=[...e.getElementsByTagName(d)];if(r.length)if(p)for(const o of r)this._matchLeaves(c,o)&&i.add(o);else i=new Set(r)}break}case b.PSEUDO_ELEMENT_SELECTOR:{this._matchPseudoElementSelector(d);break}default:h=!0}return{nodes:i,pending:h}}_matchCombinator(l,e,f={}){const{combo:c,leaves:n}=l,{name:d}=c,{find:p,forgive:u}=f;let t=new Set;if(p==="next")switch(d){case"+":{const i=e.nextElementSibling;i&&this._matchLeaves(n,i)&&t.add(i);break}case"~":{let i=e.nextElementSibling;for(;i;)this._matchLeaves(n,i)&&t.add(i),i=i.nextElementSibling;break}case">":{const i=[...e.children];for(const h of i)this._matchLeaves(n,h)&&t.add(h);break}case" ":default:{const{nodes:i,pending:h}=this._findDescendantNodes(n,e);if(i.size)t=i;else if(h){const{document:r}=this.#t,o=r.createNodeIterator(e,b.SHOW_ELEMENT);let s=o.nextNode();for(s===e&&(s=o.nextNode());s;)this._matchLeaves(n,s)&&t.add(s),s=o.nextNode()}}}else switch(d){case"+":{const i=e.previousElementSibling;i&&this._matchLeaves(n,i,{forgive:u})&&t.add(i);break}case"~":{const i=[];let h=e.previousElementSibling;for(;h;)this._matchLeaves(n,h,{forgive:u})&&i.push(h),h=h.previousElementSibling;i.length&&(t=new Set(i.reverse()));break}case">":{const i=e.parentNode;i&&this._matchLeaves(n,i,{forgive:u})&&t.add(i);break}case" ":default:{const i=[];let h=e.parentNode;for(;h;)this._matchLeaves(n,h,{forgive:u})&&i.push(h),h=h.parentNode;i.length&&(t=new Set(i.reverse()))}}return t}_findNodes(l,e){const{leaves:[f,...c]}=l,{type:n}=f,d=(0,g.unescapeSelector)(f.name),p=c.length>0,{document:u,root:t}=this.#t;let i=new Set,h=!1;switch(n){case b.ID_SELECTOR:{let r;if(e===S)this._matchLeaves([f],this.#e)&&(r=this.#e);else if(e===v){let o=this.#e;for(;o;){if(this._matchLeaves([f],o)){r=o;break}o=o.parentNode}}else t.nodeType===b.ELEMENT_NODE?h=!0:r=t.getElementById(d);r&&(p?this._matchLeaves(c,r)&&i.add(r):i.add(r));break}case b.CLASS_SELECTOR:{const r=[];if(e===S)this.#e.nodeType===b.ELEMENT_NODE&&this.#e.classList.contains(d)&&r.push(this.#e);else if(e===v){let o=this.#e;for(;o&&o.nodeType===b.ELEMENT_NODE;)o.classList.contains(d)&&r.push(o),o=o.parentNode}else if(t.nodeType===b.DOCUMENT_FRAGMENT_NODE){const o=[...t.children];for(const s of o){s.classList.contains(d)&&r.push(s);const a=[...s.getElementsByClassName(d)];r.push(...a)}}else{const o=[...t.getElementsByClassName(d)];r.push(...o)}if(r.length)if(p)for(const o of r)this._matchLeaves(c,o)&&i.add(o);else i=new Set(r);break}case b.TYPE_SELECTOR:{const r=[];if(e===S)this.#e.nodeType===b.ELEMENT_NODE&&this._matchLeaves([f],this.#e)&&r.push(this.#e);else if(e===v){let o=this.#e;for(;o&&o.nodeType===b.ELEMENT_NODE;)this._matchLeaves([f],o)&&r.push(o),o=o.parentNode}else if(u.contentType!=="text/html"||/[*|]/.test(d))h=!0;else if(t.nodeType===b.DOCUMENT_FRAGMENT_NODE){const o=d.toLowerCase(),s=[...t.children];for(const a of s){a.localName===o&&r.push(a);const m=[...a.getElementsByTagName(d)];r.push(...m)}}else{const o=[...t.getElementsByTagName(d)];r.push(...o)}if(r.length)if(p)for(const o of r)this._matchLeaves(c,o)&&i.add(o);else i=new Set(r);break}case b.PSEUDO_ELEMENT_SELECTOR:{this._matchPseudoElementSelector(d);break}default:{const r=[];if(e===S)this._matchLeaves([f],this.#e)&&r.push(this.#e);else if(e===v){let o=this.#e;for(;o;)this._matchLeaves([f],o)&&r.push(o),o=o.parentNode}else h=!0;if(r.length)if(p)for(const o of r)this._matchLeaves(c,o)&&i.add(o);else i=new Set(r)}}return{nodes:i,pending:h}}_getFirstTwig(l){const e=l.length-1,f=l[0];let c,n;if(e){const d=l[e],{leaves:[{type:p}]}=d;p===b.PSEUDO_ELEMENT_SELECTOR||p===b.ID_SELECTOR?(c="prev",n=d):(c="next",n=f)}else c="prev",n=f;return{find:c,twig:n}}_collectNodes(l){const e=this.#s.values();if(l===C||l===A){const f=new Set;let c=0;for(const{branch:n}of e){const{find:d,twig:p}=this._getFirstTwig(n),{nodes:u,pending:t}=this._findNodes(p,l);u.size?this.#l[c]=u:t?f.add(new Map([["index",c],["twig",p]])):this.#s[c].skip=!0,this.#s[c].find=d,c++}if(f.size){const{document:n,root:d}=this.#t,p=n.createNodeIterator(d,b.SHOW_ELEMENT);let u=p.nextNode();for(;u;){let t=!1;if(this.#e.nodeType===b.ELEMENT_NODE?t=(0,k.isSameOrDescendant)(u,this.#e):t=!0,t)for(const i of f){const{leaves:h}=i.get("twig");if(this._matchLeaves(h,u)){const o=i.get("index");this.#l[o].add(u)}}u=p.nextNode()}}}else{let f=0;for(const{branch:c}of e){const n=c[c.length-1],{nodes:d}=this._findNodes(n,l);d.size?this.#l[f]=d:this.#s[f].skip=!0,this.#s[f].find="prev",f++}}return[this.#s,this.#l]}_matchNodes(l){const[...e]=this.#s,f=e.length;let c=new Set;for(let n=0;n<f;n++){const{branch:d,find:p,skip:u}=e[n],t=d.length;if(!u&&t){const i=this.#l[n],h=t-1;if(h===0)if((l===C||l===A)&&this.#e.nodeType===b.ELEMENT_NODE){for(const r of i)if(r!==this.#e&&(0,k.isSameOrDescendant)(r,this.#e)&&(c.add(r),l===A))break}else if(l===A){const[r]=[...i];c.add(r)}else{const r=[...c],o=[...i];c=new Set([...r,...o])}else if(p==="next"){let{combo:r}=d[0];for(const o of i){let s=new Set([o]);for(let a=1;a<t;a++){const{combo:m,leaves:N}=d[a],w=[];for(const _ of s){const T={combo:r,leaves:N},P=this._matchCombinator(T,_,{find:p});P.size&&w.push(...P)}const E=new Set(w);if(E.size)if(a===h){if(l===A){const[_]=[...E];c.add(_)}else{const _=[...c],T=[...E];c=new Set([..._,...T])}break}else r=m,s=E;else break}}}else for(const r of i){let o=new Set([r]),s;for(let a=h-1;a>=0;a--){const m=d[a],N=[];for(const E of o){const _=this._matchCombinator(m,E,{find:p});_.size&&N.push(..._)}const w=new Set(N);if(w.size)if(s=!0,a===0){c.add(r);break}else o=w;else{s=!1;break}}if(s&&l!==C)break}}}return c}_find(l){return this._collectNodes(l),this._matchNodes(l)}_sortNodes(l){const e=[...l];return e.length>1&&e.sort((f,c)=>{let n;const d=f.compareDocumentPosition(c);return d&b.DOCUMENT_POSITION_PRECEDING||d&b.DOCUMENT_POSITION_CONTAINS?n=1:n=-1,n}),e}matches(){if(this.#e.nodeType!==b.ELEMENT_NODE)throw new TypeError(`Unexpected node ${this.#e.nodeName}`);let l;try{l=this._find(S).has(this.#e)}catch(e){this._onError(e)}return!!l}closest(){if(this.#e.nodeType!==b.ELEMENT_NODE)throw new TypeError(`Unexpected node ${this.#e.nodeName}`);let l;try{const e=this._find(v);let f=this.#e;for(;f;){if(e.has(f)){l=f;break}f=f.parentNode}}catch(e){this._onError(e)}return l??null}querySelector(){let l;try{const e=this._find(A);e.delete(this.#e),e.size>1?[l]=this._sortNodes(e):e.size&&([l]=[...e])}catch(e){this._onError(e)}return l??null}querySelectorAll(){const l=[];try{const e=this._find(C);e.delete(this.#e),e.size>1&&this.#c?l.push(...this._sortNodes(e)):e.size&&l.push(...e)}catch(e){this._onError(e)}return l}}0&&(module.exports={Matcher});
1
+ var j=Object.create;var L=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var W=Object.getOwnPropertyNames;var q=Object.getPrototypeOf,G=Object.prototype.hasOwnProperty;var Y=(y,a)=>{for(var e in a)L(y,e,{get:a[e],enumerable:!0})},R=(y,a,e,o)=>{if(a&&typeof a=="object"||typeof a=="function")for(let h of W(a))!G.call(y,h)&&h!==e&&L(y,h,{get:()=>a[h],enumerable:!(o=H(a,h))||o.enumerable});return y};var X=(y,a,e)=>(e=y!=null?j(q(y)):{},R(a||!y||!y.__esModule?L(e,"default",{value:y,enumerable:!0}):e,y)),Z=y=>R(L({},"__esModule",{value:!0}),y);var ee={};Y(ee,{Matcher:()=>Q});module.exports=Z(ee);var D=X(require("is-potential-custom-element-name"),1),k=require("./dom-util.js"),g=require("./parser.js"),b=require("./constant.js");const $="all",T="first",v="lineal",S="self",U=/^(?:(?:fieldse|inpu|selec)t|button|opt(?:group|ion)|textarea)$/,C=/^(?:(?:(?:in|out)pu|selec)t|button|form|textarea)$/,M=/^a(?:rea)?$/,B=/^d(?:etails|ialog)$/,O=/^(?:checkbox|radio)$/,x=/^(?:(?:emai|te|ur)l|number|password|search|text)$/,z=/(?:(?:rang|tim)e|date(?:time-local)?|month|number|week)$/,F=/^(?:button|reset)$/,V=/^(?:image|submit)$/,I=/^(?:date(?:time-local)?|month|time|week)$/,K=/^(?:(?:ha|i)s|not|where)$/,J=/^nth-(?:last-)?(?:child|of-type)$/;class Q{#s;#l;#a;#e;#r;#t;#o;#c;#i;constructor(a,e,o={}){const{sort:h,warn:c}=o;this.#l=new Map([[b.PSEUDO_ELEMENT_SELECTOR,b.BIT_1],[b.ID_SELECTOR,b.BIT_10],[b.CLASS_SELECTOR,b.BIT_100],[b.TYPE_SELECTOR,b.BIT_1000],[b.ATTRIBUTE_SELECTOR,b.BIT_10000],[b.PSEUDO_CLASS_SELECTOR,b.BIT_100000]]),this.#a=new WeakMap,this.#o=a,this.#e=e,this.#c=!!h,this.#i=!!c,[this.#s,this.#r]=this._prepare(a),this.#t=this._getRoot(e)}_onError(a){if(a instanceof DOMException&&a.name===b.NOT_SUPPORTED_ERR)this.#i&&console.warn(a.message);else throw a}_getRoot(a=this.#e){let e,o;switch(a.nodeType){case b.DOCUMENT_NODE:{e=a,o=a;break}case b.DOCUMENT_FRAGMENT_NODE:{e=a.ownerDocument,o=a;break}case b.ELEMENT_NODE:{if((0,k.isSameOrDescendant)(a))e=a.ownerDocument,o=a.ownerDocument;else{let h=a;for(;h&&h.parentNode;)h=h.parentNode;e=h.ownerDocument,o=h}break}default:throw new TypeError(`Unexpected node ${a.nodeName}`)}return{document:e,root:o}}_sortLeaves(a){const e=[...a];return e.length>1&&e.sort((o,h)=>{const{type:c}=o,{type:d}=h,p=this.#l.get(c),u=this.#l.get(d);let t;return p===u?t=0:p>u?t=1:t=-1,t}),e}_prepare(a=this.#o){const e=(0,g.parseSelector)(a),o=(0,g.walkAST)(e),h=[],c=[];let d=0;for(const[...p]of o){const u=[];let t=p.shift();if(t&&t.type!==b.COMBINATOR){const l=new Set;for(;t;){if(t.type===b.COMBINATOR){const[n]=p;if(n.type===b.COMBINATOR){const f=`Invalid combinator ${t.name}${n.name}`;throw new DOMException(f,b.SYNTAX_ERR)}u.push({combo:t,leaves:this._sortLeaves(l)}),l.clear()}else t&&l.add(t);if(p.length)t=p.shift();else{u.push({combo:null,leaves:this._sortLeaves(l)}),l.clear();break}}}h.push({branch:u,find:null,skip:!1}),c[d]=new Set,d++}return[h,c]}_collectNthChild(a,e){const{a:o,b:h,reverse:c,selector:d}=a,{parentNode:p}=e,u=new Set;let t;if(d&&(this.#a.has(d)?t=this.#a.get(d):(t=(0,g.walkAST)(d),this.#a.set(d,t))),p){const l=[...p.children],n=l.length;if(n){const f=new Set;if(t){const r=t.length;for(const s of l){let i;for(let m=0;m<r;m++){const N=t[m];if(i=this._matchLeaves(N,s),!i)break}i&&f.add(s)}}if(c&&l.reverse(),o===0){if(h>0&&h<=n){if(f.size)for(let r=0;r<n;r++){const s=l[r];if(f.has(s)){u.add(s);break}}else if(!d){const r=l[h-1];u.add(r)}}}else{let r=h-1;if(o>0)for(;r<0;)r+=o;if(r>=0&&r<n){let s=o>0?0:h-1;for(let i=0;i<n&&r>=0&&r<n;i++){const m=l[i];f.size?f.has(m)&&(s===r&&(u.add(m),r+=o),o>0?s++:s--):i===r&&(d||u.add(m),r+=o)}}}}}else{const{root:l}=this.#t;if(l.nodeType===b.ELEMENT_NODE&&e===l&&o+h===1)if(t){const n=t.length;let f;for(let r=0;r<n;r++){const s=t[r];if(f=this._matchLeaves(s,e),f)break}f&&u.add(e)}else u.add(e)}return u}_collectNthOfType(a,e){const{a:o,b:h,reverse:c}=a,{localName:d,parentNode:p,prefix:u}=e,t=new Set;if(p){const l=[...p.children],n=l.length;if(n)if(c&&l.reverse(),o===0){if(h>0&&h<=n){let f=0;for(let r=0;r<n;r++){const s=l[r],{localName:i,prefix:m}=s;if(i===d&&m===u){if(f===h-1){t.add(s);break}f++}}}}else{let f=h-1;if(o>0)for(;f<0;)f+=o;if(f>=0&&f<n){let r=o>0?0:h-1;for(let s=0;s<n;s++){const i=l[s],{localName:m,prefix:N}=i;if(m===d&&N===u){if(r===f&&(t.add(i),f+=o),f<0||f>=n)break;o>0?r++:r--}}}}}else{const{root:l}=this.#t;l.nodeType===b.ELEMENT_NODE&&e===l&&o+h===1&&t.add(e)}return t}_matchAnPlusB(a,e,o){const{nth:{a:h,b:c,name:d},selector:p}=a,u=(0,g.unescapeSelector)(d),t=new Map;u?(u==="even"?(t.set("a",2),t.set("b",0)):u==="odd"&&(t.set("a",2),t.set("b",1)),/last/.test(o)&&t.set("reverse",!0)):(typeof h=="string"&&/-?\d+/.test(h)?t.set("a",h*1):t.set("a",0),typeof c=="string"&&/-?\d+/.test(c)?t.set("b",c*1):t.set("b",0),/last/.test(o)&&t.set("reverse",!0));let l=new Set;if(t.has("a")&&t.has("b")){if(/^nth-(?:last-)?child$/.test(o)){p&&t.set("selector",p);const n=Object.fromEntries(t),f=this._collectNthChild(n,e);f.size&&(l=f)}else if(/^nth-(?:last-)?of-type$/.test(o)){const n=Object.fromEntries(t),f=this._collectNthOfType(n,e);f.size&&(l=f)}}return l}_matchPseudoElementSelector(a,e={}){const{forgive:o}=e;switch(a){case"after":case"backdrop":case"before":case"cue":case"cue-region":case"first-letter":case"first-line":case"file-selector-button":case"marker":case"part":case"placeholder":case"selection":case"slotted":case"target-text":{if(this.#i)throw new DOMException(`Unsupported pseudo-element ::${a}`,b.NOT_SUPPORTED_ERR);break}default:if(a.startsWith("-webkit-")){if(this.#i)throw new DOMException(`Unsupported pseudo-element ::${a}`,b.NOT_SUPPORTED_ERR)}else if(!o)throw new DOMException(`Unknown pseudo-element ::${a}`,b.SYNTAX_ERR)}}_matchDirectionPseudoClass(a,e){const o=(0,g.unescapeSelector)(a.name),h=(0,k.getDirectionality)(e);let c;return o===h&&(c=e),c??null}_matchLanguagePseudoClass(a,e){const o=(0,g.unescapeSelector)(a.name);let h;if(o){if(o==="*")if(e.hasAttribute("lang"))e.getAttribute("lang")&&(h=e);else{let c=e.parentNode;for(;c;){if(c.hasAttribute("lang")){c.getAttribute("lang")&&(h=e);break}c=c.parentNode}}else if(/[A-Z\d-]+/i.test(o)){const c="[A-Z\\d]+",d=`(?:-${c})*`;let p;if(/-/.test(o)){const[u,t,...l]=o.split("-");let n;u==="*"?n=`${c}${d}`:n=`${u}${d}`;const f=`-${t}${d}`,r=l.length;let s="";if(r)for(let i=0;i<r;i++)s+=`-${l[i]}${d}`;p=new RegExp(`^${n}${f}${s}$`,"i")}else p=new RegExp(`^${o}${d}$`,"i");if(e.hasAttribute("lang"))p.test(e.getAttribute("lang"))&&(h=e);else{let u=e.parentNode;for(;u;){if(u.hasAttribute("lang")){const t=u.getAttribute("lang");p.test(t)&&(h=e);break}u=u.parentNode}}}}return h??null}_matchHasPseudoFunc(a,e){let o;if(Array.isArray(a)&&a.length){const[h]=a,{type:c}=h;let d;c===b.COMBINATOR?d=a.shift():d={name:" ",type:b.COMBINATOR};const p=[];for(;a.length;){const[l]=a,{type:n}=l;if(n===b.COMBINATOR)break;p.push(a.shift())}const u={combo:d,leaves:p},t=this._matchCombinator(u,e,{find:"next"});if(t.size)if(a.length){for(const l of t)if(o=this._matchHasPseudoFunc(Object.assign([],a),l),o)break}else o=!0}return!!o}_matchLogicalPseudoFunc(a,e){const{astName:o="",branches:h=[],selector:c="",twigBranches:d=[]}=a;let p;if(o==="has")if(c.includes(":has("))p=null;else{let u;const t=h.length;for(let l=0;l<t;l++){const n=h[l];if(u=this._matchHasPseudoFunc(Object.assign([],n),e),u)break}u&&(p=e)}else{const u=/^(?:is|where)$/.test(o);let t;const l=d.length;for(let n=0;n<l;n++){const f=d[n],r=f.length-1,{leaves:s}=f[r];if(t=this._matchLeaves(s,e,{forgive:u}),t&&r>0){let i=new Set([e]);for(let m=r-1;m>=0;m--){const N=f[m],w=[];for(const _ of i){const A=this._matchCombinator(N,_,{forgive:u,find:"prev"});A.size&&w.push(...A)}const E=new Set(w);if(E.size)if(m===0){t=!0;break}else i=E;else{t=!1;break}}}if(t)break}o==="not"?t||(p=e):t&&(p=e)}return p??null}_matchPseudoClassSelector(a,e,o={}){const{children:h}=a,{localName:c,parentNode:d}=e,{forgive:p}=o,u=(0,g.unescapeSelector)(a.name);let t=new Set;if(K.test(u)){let l;if(this.#a.has(a))l=this.#a.get(a);else{const f=(0,g.walkAST)(a),r=[],s=[];for(const[...i]of f){for(const E of i){const _=(0,g.generateCSS)(E);r.push(_)}const m=[],N=new Set;let w=i.shift();for(;w;)if(w.type===b.COMBINATOR?(m.push({combo:w,leaves:[...N]}),N.clear()):w&&N.add(w),i.length)w=i.shift();else{m.push({combo:null,leaves:[...N]}),N.clear();break}s.push(m)}l={astName:u,branches:f,twigBranches:s,selector:r.join(",")},this.#a.set(a,l)}const n=this._matchLogicalPseudoFunc(l,e);n&&t.add(n)}else if(Array.isArray(h)){const[l]=h;if(J.test(u)){const n=this._matchAnPlusB(l,e,u);n.size&&(t=n)}else if(u==="dir"){const n=this._matchDirectionPseudoClass(l,e);n&&t.add(n)}else if(u==="lang"){const n=this._matchLanguagePseudoClass(l,e);n&&t.add(n)}else switch(u){case"current":case"nth-col":case"nth-last-col":{if(this.#i)throw new DOMException(`Unsupported pseudo-class :${u}()`,b.NOT_SUPPORTED_ERR);break}default:if(!p)throw new DOMException(`Unknown pseudo-class :${u}()`,b.SYNTAX_ERR)}}else{const{document:l,root:n}=this.#t,{documentElement:f}=l,r=new URL(l.URL);switch(u){case"any-link":case"link":{M.test(c)&&e.hasAttribute("href")&&t.add(e);break}case"local-link":{if(M.test(c)&&e.hasAttribute("href")){const s=new URL(e.getAttribute("href"),r.href);s.origin===r.origin&&s.pathname===r.pathname&&t.add(e)}break}case"visited":break;case"target":{(0,k.isSameOrDescendant)(e)&&r.hash&&e.id&&r.hash===`#${e.id}`&&t.add(e);break}case"target-within":{if(r.hash){const s=r.hash.replace(/^#/,"");let i=l.getElementById(s);for(;i;){if(i===e){t.add(e);break}i=i.parentNode}}break}case"scope":{this.#e.nodeType===b.ELEMENT_NODE?e===this.#e&&t.add(e):e===f&&t.add(e);break}case"focus":{e===l.activeElement&&t.add(e);break}case"focus-within":{let s=l.activeElement;for(;s;){if(s===e){t.add(e);break}s=s.parentNode}break}case"open":{B.test(c)&&e.hasAttribute("open")&&t.add(e);break}case"closed":{B.test(c)&&!e.hasAttribute("open")&&t.add(e);break}case"disabled":{if(U.test(c)||(0,D.default)(c))if(e.disabled||e.hasAttribute("disabled"))t.add(e);else{let s=d;for(;s&&s.localName!=="fieldset";)s=s.parentNode;s&&s.hasAttribute("disabled")&&d.localName!=="legend"&&t.add(e)}break}case"enabled":{(U.test(c)||(0,D.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||x.test(e.type)||I.test(e.type))&&(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&t.add(e);break}default:(0,k.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||x.test(e.type)||I.test(e.type))&&!(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&t.add(e);break}default:(0,k.isContentEditable)(e)&&t.add(e)}break}case"placeholder-shown":{let s;c==="textarea"?s=e:c==="input"&&(e.hasAttribute("type")?x.test(e.getAttribute("type"))&&(s=e):s=e),s&&e.hasAttribute("placeholder")&&e.getAttribute("placeholder").trim().length&&e.value===""&&t.add(e);break}case"checked":{(c==="input"&&e.hasAttribute("type")&&O.test(e.getAttribute("type"))&&e.checked||c==="option"&&e.selected)&&t.add(e);break}case"indeterminate":{if(c==="input"&&e.type==="checkbox"&&e.indeterminate||c==="progress"&&!e.hasAttribute("value"))t.add(e);else if(c==="input"&&e.type==="radio"&&!e.hasAttribute("checked")){const s=e.name;let i=e.parentNode;for(;i&&i.localName!=="form";)i=i.parentNode;i||(i=f);const m=[...i.getElementsByTagName("input")];let N;for(const w of m)if(w.getAttribute("type")==="radio"&&(s?w.getAttribute("name")===s&&(N=!!w.checked):w.hasAttribute("name")||(N=!!w.checked),N))break;N||t.add(e)}break}case"default":{if(c==="button"&&!(e.hasAttribute("type")&&F.test(e.getAttribute("type")))||c==="input"&&e.hasAttribute("type")&&V.test(e.getAttribute("type"))){let s=e.parentNode;for(;s&&s.localName!=="form";)s=s.parentNode;if(s){const i=l.createNodeIterator(s,b.SHOW_ELEMENT);let m=i.nextNode();for(;m;){const N=m.localName;let w;if(N==="button"?w=!(m.hasAttribute("type")&&F.test(m.getAttribute("type"))):N==="input"&&(w=m.hasAttribute("type")&&V.test(m.getAttribute("type"))),w){m===e&&t.add(e);break}m=i.nextNode()}}}else if(c==="input"&&e.hasAttribute("type")&&O.test(e.getAttribute("type"))&&(e.checked||e.hasAttribute("checked")))t.add(e);else if(c==="option"){let s=!1,i=d;for(;i&&i.localName!=="datalist";){if(i.localName==="select"){(i.multiple||i.hasAttribute("multiple"))&&(s=!0);break}i=i.parentNode}if(s)(e.selected||e.hasAttribute("selected"))&&t.add(e);else{const m=d.firstElementChild,N=new Set;let w=m;for(;w;){if(w.selected||w.hasAttribute("selected")){N.add(w);break}w=w.nextElementSibling}N.size||N.add(m),N.has(e)&&t.add(e)}}break}case"valid":{if(C.test(c))e.checkValidity()&&t.add(e);else if(/^fieldset$/.test(c)){const s=l.createNodeIterator(e,b.SHOW_ELEMENT);let i=s.nextNode();i===e&&(i=s.nextNode());let m;for(;i&&!(C.test(i.localName)&&(m=i.checkValidity(),!m));)i=s.nextNode();m&&t.add(e)}break}case"invalid":{if(C.test(c))e.checkValidity()||t.add(e);else if(/^fieldset$/.test(c)){const s=l.createNodeIterator(e,b.SHOW_ELEMENT);let i=s.nextNode();i===e&&(i=s.nextNode());let m;for(;i&&!(C.test(i.localName)&&(m=i.checkValidity(),!m));)i=s.nextNode();m||t.add(e)}break}case"in-range":{c==="input"&&!(e.readonly||e.hasAttribute("readonly"))&&!(e.disabled||e.hasAttribute("disabled"))&&e.hasAttribute("type")&&z.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")&&z.test(e.getAttribute("type"))&&(e.validity.rangeUnderflow||e.validity.rangeOverflow)&&t.add(e);break}case"required":{let s;if(/^(?:select|textarea)$/.test(c))s=e;else if(c==="input")if(e.hasAttribute("type")){const i=e.getAttribute("type");(x.test(i)||O.test(i)||I.test(i)||i==="file")&&(s=e)}else s=e;s&&(e.required||e.hasAttribute("required"))&&t.add(e);break}case"optional":{let s;if(/^(?:select|textarea)$/.test(c))s=e;else if(c==="input")if(e.hasAttribute("type")){const i=e.getAttribute("type");(x.test(i)||O.test(i)||I.test(i)||i==="file")&&(s=e)}else s=e;s&&!(e.required||e.hasAttribute("required"))&&t.add(e);break}case"root":{e===f&&t.add(e);break}case"empty":{if(e.hasChildNodes()){const s=e.childNodes.values();let i;for(const m of s)if(i=m.nodeType!==b.ELEMENT_NODE&&m.nodeType!==b.TEXT_NODE,!i)break;i&&t.add(e)}else t.add(e);break}case"first-child":{(d&&e===d.firstElementChild||n.nodeType===b.ELEMENT_NODE&&e===n)&&t.add(e);break}case"last-child":{(d&&e===d.lastElementChild||n.nodeType===b.ELEMENT_NODE&&e===n)&&t.add(e);break}case"only-child":{(d&&e===d.firstElementChild&&e===d.lastElementChild||n.nodeType===b.ELEMENT_NODE&&e===n)&&t.add(e);break}case"first-of-type":{if(d){const[s]=this._collectNthOfType({a:0,b:1},e);s&&t.add(s)}else n.nodeType===b.ELEMENT_NODE&&e===n&&t.add(e);break}case"last-of-type":{if(d){const[s]=this._collectNthOfType({a:0,b:1,reverse:!0},e);s&&t.add(s)}else n.nodeType===b.ELEMENT_NODE&&e===n&&t.add(e);break}case"only-of-type":{if(d){const[s]=this._collectNthOfType({a:0,b:1},e);if(s===e){const[i]=this._collectNthOfType({a:0,b:1,reverse:!0},e);i===e&&t.add(e)}}else n.nodeType===b.ELEMENT_NODE&&e===n&&t.add(e);break}case"after":case"before":case"first-letter":case"first-line":{if(this.#i)throw new DOMException(`Unsupported pseudo-element ::${u}`,b.NOT_SUPPORTED_ERR);break}case"active":case"autofill":case"blank":case"buffering":case"current":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(this.#i)throw new DOMException(`Unsupported pseudo-class :${u}`,b.NOT_SUPPORTED_ERR);break}default:if(u.startsWith("-webkit-")){if(this.#i)throw new DOMException(`Unsupported pseudo-class :${u}`,b.NOT_SUPPORTED_ERR)}else if(!p)throw new DOMException(`Unknown pseudo-class :${u}`,b.SYNTAX_ERR)}}return t}_matchAttributeSelector(a,e){const{flags:o,matcher:h,name:c,value:d}=a;if(typeof o=="string"&&!/^[is]$/i.test(o))throw new DOMException("Invalid attribute selector",b.SYNTAX_ERR);const{attributes:p}=e;let u;if(p&&p.length){const{document:t}=this.#t;let l;t.contentType==="text/html"?typeof o=="string"&&/^s$/i.test(o)?l=!1:l=!0:typeof o=="string"&&/^i$/i.test(o)?l=!0:l=!1;let{name:n}=c;n=(0,g.unescapeSelector)(n),l&&(n=n.toLowerCase());const f=new Set;if(/\|/.test(n)){const{prefix:r,tagName:s}=(0,k.selectorToNodeProps)(n);for(let{name:i,value:m}of p)switch(l&&(i=i.toLowerCase(),m=m.toLowerCase()),r){case"":{s===i&&f.add(m);break}case"*":{/:/.test(i)?i.endsWith(`:${s}`)&&f.add(m):s===i&&f.add(m);break}default:if(/:/.test(i)){const[N,w]=i.split(":");r===N&&s===w&&(0,k.isNamespaceDeclared)(r,e)&&f.add(m)}}}else for(let{name:r,value:s}of p)if(l&&(r=r.toLowerCase(),s=s.toLowerCase()),/:/.test(r)){const[i,m]=r.split(":");if(i==="xml"&&m==="lang")continue;n===m&&f.add(s)}else n===r&&f.add(s);if(f.size){const{name:r,value:s}=d||{};let i;switch(r?l?i=r.toLowerCase():i=r:s?l?i=s.toLowerCase():i=s:s===""&&(i=s),h){case"=":{typeof i=="string"&&f.has(i)&&(u=e);break}case"~=":{if(i&&typeof i=="string"){for(const m of f)if(new Set(m.split(/\s+/)).has(i)){u=e;break}}break}case"|=":{if(i&&typeof i=="string"){let m;for(const N of f)if(N===i||N.startsWith(`${i}-`)){m=N;break}m&&(u=e)}break}case"^=":{if(i&&typeof i=="string"){let m;for(const N of f)if(N.startsWith(`${i}`)){m=N;break}m&&(u=e)}break}case"$=":{if(i&&typeof i=="string"){let m;for(const N of f)if(N.endsWith(`${i}`)){m=N;break}m&&(u=e)}break}case"*=":{if(i&&typeof i=="string"){let m;for(const N of f)if(N.includes(`${i}`)){m=N;break}m&&(u=e)}break}case null:default:u=e}}}return u??null}_matchClassSelector(a,e){const o=(0,g.unescapeSelector)(a.name);let h;return e.classList.contains(o)&&(h=e),h??null}_matchIDSelector(a,e){const{id:o}=e,h=(0,g.unescapeSelector)(a.name);let c;return h===o&&(c=e),c??null}_matchTypeSelector(a,e){const o=(0,g.unescapeSelector)(a.name),{localName:h,prefix:c}=e,{document:d}=this.#t;let{prefix:p,tagName:u}=(0,k.selectorToNodeProps)(o,e);d.contentType==="text/html"&&(p=p.toLowerCase(),u=u.toLowerCase());let t,l;/:/.test(h)?[t,l]=h.split(":"):(t=c||"",l=h);let n;return p===""&&t===""?e.namespaceURI===null&&(u==="*"||u===l)&&(n=e):p==="*"?(u==="*"||u===l)&&(n=e):p===t&&(0,k.isNamespaceDeclared)(p,e)&&(u==="*"||u===l)&&(n=e),n??null}_matchSelector(a,e,o){const{type:h}=a;let c=new Set;if(e.nodeType===b.ELEMENT_NODE)switch(h){case b.ATTRIBUTE_SELECTOR:{const d=this._matchAttributeSelector(a,e);d&&c.add(d);break}case b.CLASS_SELECTOR:{const d=this._matchClassSelector(a,e);d&&c.add(d);break}case b.ID_SELECTOR:{const d=this._matchIDSelector(a,e);d&&c.add(d);break}case b.PSEUDO_CLASS_SELECTOR:{const d=this._matchPseudoClassSelector(a,e,o);d.size&&(c=d);break}case b.PSEUDO_ELEMENT_SELECTOR:{const d=(0,g.unescapeSelector)(a.name);this._matchPseudoElementSelector(d,o);break}case b.TYPE_SELECTOR:default:{const d=this._matchTypeSelector(a,e);d&&c.add(d)}}return c}_matchLeaves(a,e,o){let h;for(const c of a)if(h=this._matchSelector(c,e,o).has(e),!h)break;return!!h}_findDescendantNodes(a,e){const[o,...h]=a,{type:c}=o,d=(0,g.unescapeSelector)(o.name),p=h.length>0,u=new Set;let t=!1;switch(c){case b.ID_SELECTOR:{const{root:l}=this.#t;if(l.nodeType===b.ELEMENT_NODE)t=!0;else{const n=l.getElementById(d);if(n&&n!==e){const f=(0,k.isSameOrDescendant)(n,e);let r;f&&(r=n),r&&(p?this._matchLeaves(h,r)&&u.add(r):u.add(r))}}break}case b.PSEUDO_ELEMENT_SELECTOR:{this._matchPseudoElementSelector(d);break}default:t=!0}return{nodes:u,pending:t}}_matchCombinator(a,e,o={}){const{combo:h,leaves:c}=a,{name:d}=h,{find:p,forgive:u}=o;let t=new Set;if(p==="next")switch(d){case"+":{const l=e.nextElementSibling;l&&this._matchLeaves(c,l,{forgive:u})&&t.add(l);break}case"~":{let l=e.nextElementSibling;for(;l;)this._matchLeaves(c,l,{forgive:u})&&t.add(l),l=l.nextElementSibling;break}case">":{const l=[...e.children];for(const n of l)this._matchLeaves(c,n,{forgive:u})&&t.add(n);break}case" ":default:{const{nodes:l,pending:n}=this._findDescendantNodes(c,e);if(l.size)t=l;else if(n){const{document:f}=this.#t,r=f.createNodeIterator(e,b.SHOW_ELEMENT);let s=r.nextNode();for(s===e&&(s=r.nextNode());s;)this._matchLeaves(c,s,{forgive:u})&&t.add(s),s=r.nextNode()}}}else switch(d){case"+":{const l=e.previousElementSibling;l&&this._matchLeaves(c,l,{forgive:u})&&t.add(l);break}case"~":{const l=[];let n=e.previousElementSibling;for(;n;)this._matchLeaves(c,n,{forgive:u})&&l.push(n),n=n.previousElementSibling;l.length&&(t=new Set(l.reverse()));break}case">":{const l=e.parentNode;l&&this._matchLeaves(c,l,{forgive:u})&&t.add(l);break}case" ":default:{const l=[];let n=e.parentNode;for(;n;)this._matchLeaves(c,n,{forgive:u})&&l.push(n),n=n.parentNode;l.length&&(t=new Set(l.reverse()))}}return t}_findNodes(a,e){const{leaves:[o,...h]}=a,{type:c}=o,d=(0,g.unescapeSelector)(o.name),p=h.length>0,{document:u,root:t}=this.#t;let l=new Set,n=!1;switch(c){case b.ID_SELECTOR:{let f;if(e===S)this._matchLeaves([o],this.#e)&&(f=this.#e);else if(e===v){let r=this.#e;for(;r;){if(this._matchLeaves([o],r)){f=r;break}r=r.parentNode}}else t.nodeType===b.ELEMENT_NODE?n=!0:f=t.getElementById(d);f&&(p?this._matchLeaves(h,f)&&l.add(f):l.add(f));break}case b.CLASS_SELECTOR:{const f=[];if(e===S)this.#e.nodeType===b.ELEMENT_NODE&&this.#e.classList.contains(d)&&f.push(this.#e);else if(e===v){let r=this.#e;for(;r&&r.nodeType===b.ELEMENT_NODE;)r.classList.contains(d)&&f.push(r),r=r.parentNode}else if(t.nodeType===b.DOCUMENT_FRAGMENT_NODE){const r=[...t.children];for(const s of r){s.classList.contains(d)&&f.push(s);const i=[...s.getElementsByClassName(d)];f.push(...i)}}else{const r=[...t.getElementsByClassName(d)];f.push(...r)}if(f.length)if(p)for(const r of f)this._matchLeaves(h,r)&&l.add(r);else l=new Set(f);break}case b.TYPE_SELECTOR:{const f=[];if(e===S)this.#e.nodeType===b.ELEMENT_NODE&&this._matchLeaves([o],this.#e)&&f.push(this.#e);else if(e===v){let r=this.#e;for(;r&&r.nodeType===b.ELEMENT_NODE;)this._matchLeaves([o],r)&&f.push(r),r=r.parentNode}else if(u.contentType!=="text/html"||/[*|]/.test(d))n=!0;else if(t.nodeType===b.DOCUMENT_FRAGMENT_NODE){const r=d.toLowerCase(),s=[...t.children];for(const i of s){i.localName===r&&f.push(i);const m=[...i.getElementsByTagName(d)];f.push(...m)}}else{const r=[...t.getElementsByTagName(d)];f.push(...r)}if(f.length)if(p)for(const r of f)this._matchLeaves(h,r)&&l.add(r);else l=new Set(f);break}case b.PSEUDO_ELEMENT_SELECTOR:{this._matchPseudoElementSelector(d);break}default:{const f=[];if(e===S)this._matchLeaves([o],this.#e)&&f.push(this.#e);else if(e===v){let r=this.#e;for(;r;)this._matchLeaves([o],r)&&f.push(r),r=r.parentNode}else n=!0;if(f.length)if(p)for(const r of f)this._matchLeaves(h,r)&&l.add(r);else l=new Set(f)}}return{nodes:l,pending:n}}_getFirstTwig(a){const e=a.length-1,o=a[0];let h,c;if(e){const d=a[e],{leaves:[{type:p}]}=d;p===b.PSEUDO_ELEMENT_SELECTOR||p===b.ID_SELECTOR?(h="prev",c=d):(h="next",c=o)}else h="prev",c=o;return{find:h,twig:c}}_collectNodes(a){const e=this.#s.values();if(a===$||a===T){const o=new Set;let h=0;for(const{branch:c}of e){const{find:d,twig:p}=this._getFirstTwig(c),{nodes:u,pending:t}=this._findNodes(p,a);u.size?this.#r[h]=u:t?o.add(new Map([["index",h],["twig",p]])):this.#s[h].skip=!0,this.#s[h].find=d,h++}if(o.size){const{document:c,root:d}=this.#t,p=c.createNodeIterator(d,b.SHOW_ELEMENT);let u=p.nextNode();for(;u;){let t=!1;if(this.#e.nodeType===b.ELEMENT_NODE?t=(0,k.isSameOrDescendant)(u,this.#e):t=!0,t)for(const l of o){const{leaves:n}=l.get("twig");if(this._matchLeaves(n,u)){const r=l.get("index");this.#r[r].add(u)}}u=p.nextNode()}}}else{let o=0;for(const{branch:h}of e){const c=h[h.length-1],{nodes:d}=this._findNodes(c,a);d.size?this.#r[o]=d:this.#s[o].skip=!0,this.#s[o].find="prev",o++}}return[this.#s,this.#r]}_matchNodes(a){const[...e]=this.#s,o=e.length;let h=new Set;for(let c=0;c<o;c++){const{branch:d,find:p,skip:u}=e[c],t=d.length;if(!u&&t){const l=this.#r[c],n=t-1;if(n===0)if((a===$||a===T)&&this.#e.nodeType===b.ELEMENT_NODE){for(const f of l)if(f!==this.#e&&(0,k.isSameOrDescendant)(f,this.#e)&&(h.add(f),a===T))break}else if(a===T){const[f]=[...l];h.add(f)}else{const f=[...h],r=[...l];h=new Set([...f,...r])}else if(p==="next"){let{combo:f}=d[0];for(const r of l){let s=new Set([r]);for(let i=1;i<t;i++){const{combo:m,leaves:N}=d[i],w=[];for(const _ of s){const A={combo:f,leaves:N},P=this._matchCombinator(A,_,{find:p});P.size&&w.push(...P)}const E=new Set(w);if(E.size)if(i===n){if(a===T){const[_]=[...E];h.add(_)}else{const _=[...h],A=[...E];h=new Set([..._,...A])}break}else f=m,s=E;else break}}}else for(const f of l){let r=new Set([f]),s;for(let i=n-1;i>=0;i--){const m=d[i],N=[];for(const E of r){const _=this._matchCombinator(m,E,{find:p});_.size&&N.push(..._)}const w=new Set(N);if(w.size)if(s=!0,i===0){h.add(f);break}else r=w;else{s=!1;break}}if(s&&a!==$)break}}}return h}_find(a){return this._collectNodes(a),this._matchNodes(a)}_sortNodes(a){const e=[...a];return e.length>1&&e.sort((o,h)=>{let c;const d=o.compareDocumentPosition(h);return d&b.DOCUMENT_POSITION_PRECEDING||d&b.DOCUMENT_POSITION_CONTAINS?c=1:c=-1,c}),e}matches(){if(this.#e.nodeType!==b.ELEMENT_NODE)throw new TypeError(`Unexpected node ${this.#e.nodeName}`);let a;try{a=this._find(S).has(this.#e)}catch(e){this._onError(e)}return!!a}closest(){if(this.#e.nodeType!==b.ELEMENT_NODE)throw new TypeError(`Unexpected node ${this.#e.nodeName}`);let a;try{const e=this._find(v);let o=this.#e;for(;o;){if(e.has(o)){a=o;break}o=o.parentNode}}catch(e){this._onError(e)}return a??null}querySelector(){let a;try{const e=this._find(T);e.delete(this.#e),e.size>1?[a]=this._sortNodes(e):e.size&&([a]=[...e])}catch(e){this._onError(e)}return a??null}querySelectorAll(){const a=[];try{const e=this._find($);e.delete(this.#e),e.size>1&&this.#c?a.push(...this._sortNodes(e)):e.size&&a.push(...e)}catch(e){this._onError(e)}return a}}0&&(module.exports={Matcher});
2
2
  //# sourceMappingURL=matcher.js.map