@asamuzakjp/dom-selector 1.0.0 → 1.0.2

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
@@ -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.|
@@ -233,34 +233,38 @@ const dom = new JSDOM('', {
233
233
 
234
234
  |Method and CSS selector|Jsdom|Patched-jsdom|Result|
235
235
  |:------------|:------------|:------------|:------------|
236
- |matches('.container.box')|1,704,793 ops/sec ±2.10%|95,691 ops/sec ±2.40%|jsdom is 17.8 times faster. patched-jsdom took 0.010msec.|
237
- |matches('.container:not(.box)')|906,712 ops/sec ±2.02%|57,603 ops/sec ±4.57%|jsdom is 15.7 times faster. patched-jsdom took 0.017msec.|
238
- |matches('.box + .box')|1,566,175 ops/sec ±2.17%|90,759 ops/sec ±5.32%|jsdom is 17.3 times faster. patched-jsdom took 0.011msec.|
239
- |matches('.box ~ .box')|1,585,314 ops/sec ±2.06%|90,257 ops/sec ±3.06%|jsdom is 17.6 times faster. patched-jsdom took 0.011msec.|
240
- |matches('.box > .block')|830,259 ops/sec ±2.29%|79,630 ops/sec ±2.76%|jsdom is 10.4 times faster. patched-jsdom took 0.013msec.|
241
- |matches('.box .content')|1,465,248 ops/sec ±2.74%|95,500 ops/sec ±2.36%|jsdom is 15.3 times faster. patched-jsdom took 0.010msec.|
242
- |matches('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner > .content')|1,531,871 ops/sec ±2.68%|30,324 ops/sec ±3.09%|* jsdom is 50.5 times faster. patched-jsdom took 0.033msec.|
243
- |closest('.container.box')|398,561 ops/sec ±2.74%|46,478 ops/sec ±2.31%|jsdom is 8.6 times faster. patched-jsdom took 0.022msec.|
244
- |closest('.container:not(.box)')|220,109 ops/sec ±2.58%|28,449 ops/sec ±4.44%|jsdom is 7.7 times faster. patched-jsdom took 0.035msec.|
245
- |closest('.box + .box')|355,897 ops/sec ±1.53%|43,752 ops/sec ±1.70%|jsdom is 8.1 times faster. patched-jsdom took 0.023msec.|
246
- |closest('.box ~ .box')|124,255 ops/sec ±1.87%|25,663 ops/sec ±2.76%|jsdom is 4.8 times faster. patched-jsdom took 0.039msec.|
247
- |closest('.box > .block')|387,622 ops/sec ±1.59%|40,747 ops/sec ±3.41%|jsdom is 9.5 times faster. patched-jsdom took 0.025msec.|
248
- |closest('.box .content')|235,499 ops/sec ±2.42%|50,113 ops/sec ±2.81%|jsdom is 4.7 times faster. patched-jsdom took 0.020msec.|
249
- |closest('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner > .content')|235,201 ops/sec ±2.80%|24,940 ops/sec ±2.26%|jsdom is 9.4 times faster. patched-jsdom took 0.040msec.|
250
- |querySelector('.container.box')|59,348 ops/sec ±3.98%|20,617 ops/sec ±2.05%|jsdom is 2.9 times faster. patched-jsdom took 0.049msec.|
251
- |querySelector('.container:not(.box)')|51,310 ops/sec ±2.61%|15,210 ops/sec ±1.42%|jsdom is 3.4 times faster. patched-jsdom took 0.066msec.|
252
- |querySelector('.box + .box')|52,099 ops/sec ±1.32%|16,644 ops/sec ±1.21%|jsdom is 3.1 times faster. patched-jsdom took 0.060msec.|
253
- |jsdom querySelector('.box ~ .box')|53,329 ops/sec ±2.51%|7,801 ops/sec ±2.10%|jsdom is 6.8 times faster. patched-jsdom took 0.128msec.|
254
- |querySelector('.box > .block')|755 ops/sec ±2.34%|2,492 ops/sec ±1.13%|patched-jsdom is 3.3 times faster. patched-jsdom took 0.401msec.|
255
- |querySelector('.box .content')|386 ops/sec ±2.96%|187 ops/sec ±1.12%|jsdom is 2.1 times faster. patched-jsdom took 5.359msec.|
256
- | querySelector('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner > .content')|158 ops/sec ±2.40%|287 ops/sec ±2.03%|patched-jsdom is 1.8 times faster. patched-jsdom took 3.486msec.|
257
- |querySelectorAll('.container.box')|73,206 ops/sec ±3.78%|19,290 ops/sec ±1.55%|jsdom is 3.8 times faster. patched-jsdom took 0.052msec.|
258
- |querySelectorAll('.container:not(.box)')|65,761 ops/sec ±3.31%|14,269 ops/sec ±2.86%|jsdom is 4.6 times faster. patched-jsdom took 0.070msec.|
259
- |querySelectorAll('.box + .box')|69,019 ops/sec ±1.54%|17,941 ops/sec ±1.48%|jsdom is 3.8 times faster. patched-jsdom took 0.056msec.|
260
- |querySelectorAll('.box ~ .box')|64,329 ops/sec ±1.25%|7,748 ops/sec ±3.19%|jsdom is 8.3 times faster. patched-jsdom took 0.129msec.|
261
- |jsdom querySelectorAll('.box > .block')|764 ops/sec ±1.90%|2,086 ops/sec ±1.49%|patched-jsdom is 2.7 times faster. patched-jsdom took 0.479msec.|
262
- |querySelectorAll('.box .content')|370 ops/sec ±1.67%|165 ops/sec ±2.22%|jsdom is 2.3 times faster. patched-jsdom took 6.076msec.|
263
- |querySelectorAll('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner > .content')|165 ops/sec ±1.13%|292 ops/sec ±1.79%|patched-jsdom is 1.8 times faster. patched-jsdom took 3.425msec.|
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.|
264
268
 
265
269
 
266
270
  ## Acknowledgments
@@ -1,2 +1,2 @@
1
- var V=Object.create;var L=Object.defineProperty;var j=Object.getOwnPropertyDescriptor;var H=Object.getOwnPropertyNames;var W=Object.getPrototypeOf,q=Object.prototype.hasOwnProperty;var G=(y,a)=>{for(var e in a)L(y,e,{get:a[e],enumerable:!0})},P=(y,a,e,n)=>{if(a&&typeof a=="object"||typeof a=="function")for(let f of H(a))!q.call(y,f)&&f!==e&&L(y,f,{get:()=>a[f],enumerable:!(n=j(a,f))||n.enumerable});return y};var Y=(y,a,e)=>(e=y!=null?V(W(y)):{},P(a||!y||!y.__esModule?L(e,"default",{value:y,enumerable:!0}):e,y)),X=y=>P(L({},"__esModule",{value:!0}),y);var Q={};G(Q,{Matcher:()=>J});module.exports=X(Q);var D=Y(require("is-potential-custom-element-name"),1),g=require("./dom-util.js"),k=require("./parser.js"),b=require("./constant.js");const C="all",A="first",S="lineal",v="self",M=/^(?:(?:fieldse|inpu|selec)t|button|opt(?:group|ion)|textarea)$/,O=/^(?:(?:(?:in|out)pu|selec)t|button|form|textarea)$/,R=/^a(?:rea)?$/,U=/^d(?:etails|ialog)$/,$=/^(?:checkbox|radio)$/,x=/^(?:(?:emai|te|ur)l|number|password|search|text)$/,B=/(?:(?:rang|tim)e|date(?:time-local)?|month|number|week)$/,z=/^(?:button|reset)$/,F=/^(?:image|submit)$/,I=/^(?:date(?:time-local)?|month|time|week)$/,Z=/^(?:(?:ha|i)s|not|where)$/,K=/^nth-(?:last-)?(?:child|of-type)$/;class J{#a;#i;#e;#r;#t;#o;#l;#s;constructor(a,e,n={}){const{sort:f,warn:h}=n;this.#o=a,this.#e=e,[this.#a,this.#r]=this._prepare(a),this.#t=this._getRoot(e),this.#i=new WeakMap,this.#l=!!f,this.#s=!!h}_onError(a){if(a instanceof DOMException&&a.name===b.NOT_SUPPORTED_ERR)this.#s&&console.warn(a.message);else throw a}_getRoot(a=this.#e){let e,n;switch(a.nodeType){case b.DOCUMENT_NODE:{e=a,n=a;break}case b.DOCUMENT_FRAGMENT_NODE:{e=a.ownerDocument,n=a;break}case b.ELEMENT_NODE:{if((0,g.isSameOrDescendant)(a))e=a.ownerDocument,n=a.ownerDocument;else{let f=a;for(;f&&f.parentNode;)f=f.parentNode;e=f.ownerDocument,n=f}break}default:throw new TypeError(`Unexpected node ${a.nodeName}`)}return{document:e,root:n}}_sortLeaves(a){const e=[...a];if(e.length>1){const n=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]]);e.sort((f,h)=>{const{type:d}=f,{type:p}=h,u=n.get(d),t=n.get(p);let i;return u===t?i=0:u>t?i=1:i=-1,i})}return e}_prepare(a=this.#o){const e=(0,k.parseSelector)(a),n=(0,k.walkAST)(e),f=[],h=[];let d=0;for(const[...p]of n){const u=[];let t=p.shift();if(t&&t.type!==b.COMBINATOR){const i=new Set;for(;t;){if(t.type===b.COMBINATOR){const[l]=p;if(l.type===b.COMBINATOR){const c=`Invalid combinator ${t.name}${l.name}`;throw new DOMException(c,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}}}f.push({branch:u,skip:!1}),h[d]=new Set,d++}return[f,h]}_collectNthChild(a,e){const{a:n,b:f,reverse:h,selector:d}=a,{parentNode:p}=e,u=new Set;let t;if(d&&(this.#i.has(d)?t=this.#i.get(d):(t=(0,k.walkAST)(d),this.#i.set(d,t))),p){const i=[...p.children],l=i.length;if(l){const c=new Set;if(t){const o=t.length;for(const s of i){let r;for(let m=0;m<o;m++){const N=t[m];if(r=this._matchLeaves(N,s),!r)break}r&&c.add(s)}}if(h&&i.reverse(),n===0){if(f>0&&f<=l){if(c.size)for(let o=0;o<l;o++){const s=i[o];if(c.has(s)){u.add(s);break}}else if(!d){const o=i[f-1];u.add(o)}}}else{let o=0,s=f-1;if(n>0)for(;s<0;)s+=++o*n;if(s>=0&&s<l){let r=n>0?0:f-1;for(let m=0;m<l&&s>=0&&s<l;m++){const N=i[m];c.size?c.has(N)&&(r===s&&(u.add(N),s+=n),n>0?r++:r--):m===s&&(d||u.add(N),s+=n)}}}}}else{const{root:i}=this.#t;if(i.nodeType===b.ELEMENT_NODE&&e===i&&n+f===1)if(t){const l=t.length;let c;for(let o=0;o<l;o++){const s=t[o];if(c=this._matchLeaves(s,e),c)break}c&&u.add(e)}else u.add(e)}return u}_collectNthOfType(a,e){const{a:n,b:f,reverse:h}=a,{localName:d,parentNode:p,prefix:u}=e,t=new Set;if(p){const i=[...p.children],l=i.length;if(l)if(h&&i.reverse(),n===0){if(f>0&&f<=l){let c=0;for(let o=0;o<l;o++){const s=i[o],{localName:r,prefix:m}=s;if(r===d&&m===u){if(c===f-1){t.add(s);break}c++}}}}else{let c=f-1;if(n>0)for(;c<0;)c+=n;if(c>=0&&c<l){let o=n>0?0:f-1;for(let s=0;s<l;s++){const r=i[s],{localName:m,prefix:N}=r;if(m===d&&N===u){if(o===c&&(t.add(r),c+=n),c<0||c>=l)break;n>0?o++:o--}}}}}else{const{root:i}=this.#t;i.nodeType===b.ELEMENT_NODE&&e===i&&n+f===1&&t.add(e)}return t}_matchAnPlusB(a,e,n){const{nth:{a:f,b:h,name:d},selector:p}=a,u=(0,k.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(n)&&t.set("reverse",!0)):(typeof f=="string"&&/-?\d+/.test(f)?t.set("a",f*1):t.set("a",0),typeof h=="string"&&/-?\d+/.test(h)?t.set("b",h*1):t.set("b",0),/last/.test(n)&&t.set("reverse",!0));let i=new Set;if(t.has("a")&&t.has("b")){if(/^nth-(?:last-)?child$/.test(n)){p&&t.set("selector",p);const l=Object.fromEntries(t),c=this._collectNthChild(l,e);c.size&&(i=c)}else if(/^nth-(?:last-)?of-type$/.test(n)){const l=Object.fromEntries(t),c=this._collectNthOfType(l,e);c.size&&(i=c)}}return i}_matchPseudoElementSelector(a,e={}){const{forgive:n}=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.#s)throw new DOMException(`Unsupported pseudo-element ::${a}`,b.NOT_SUPPORTED_ERR);break}default:if(a.startsWith("-webkit-")){if(this.#s)throw new DOMException(`Unsupported pseudo-element ::${a}`,b.NOT_SUPPORTED_ERR)}else if(!n)throw new DOMException(`Unknown pseudo-element ::${a}`,b.SYNTAX_ERR)}}_matchDirectionPseudoClass(a,e){const n=(0,k.unescapeSelector)(a.name),f=(0,g.getDirectionality)(e);let h;return n===f&&(h=e),h??null}_matchLanguagePseudoClass(a,e){const{lang:n}=e,f=(0,k.unescapeSelector)(a.name);let h;if(f==="")e.getAttribute("lang")===""&&(h=e);else if(f==="*")e.hasAttribute("lang")||(h=e);else if(/[A-Z\d-]+/i.test(f)){const d="(?:-[A-Za-z\\d]+)?";let p;if(/-/.test(f)){const[u,t,...i]=f.split("-"),l=`${u}${d}`,c=`-${t}${d}`,o=i.length;let s="";if(o)for(let r=0;r<o;r++)s+=`-${i[r]}${d}`;p=new RegExp(`^${l}${c}${s}$`,"i")}else p=new RegExp(`^${f}${d}$`,"i");if(n)p.test(n)&&(h=e);else{let u=e;for(;u;){if(p.test(u.lang)){h=e;break}u=u.parentNode}}}return h??null}_matchHasPseudoFunc(a,e){let n;if(Array.isArray(a)&&a.length){const[f]=a,{type:h}=f;let d;h===b.COMBINATOR?d=a.shift():d={name:" ",type:b.COMBINATOR};const p=[];for(;a.length;){const[i]=a,{type:l}=i;if(l===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 i of t)if(n=this._matchHasPseudoFunc(Object.assign([],a),i),n)break}else n=!0}return!!n}_matchLogicalPseudoFunc(a,e){const{astName:n="",branches:f=[],selector:h="",twigBranches:d=[]}=a;let p;if(n==="has")if(h.includes(":has("))p=null;else{let u;const t=f.length;for(let i=0;i<t;i++){const l=f[i];if(u=this._matchHasPseudoFunc(Object.assign([],l),e),u)break}u&&(p=e)}else{const u=/^(?:is|where)$/.test(n);let t;const i=d.length;for(let l=0;l<i;l++){const c=d[l],o=c.length-1,{leaves:s}=c[o];if(t=this._matchLeaves(s,e,{forgive:u}),t&&o>0){let r=new Set([e]);for(let m=o-1;m>=0;m--){const N=c[m],w=[];for(const E of r){const T=this._matchCombinator(N,E,{forgive:u,find:"prev"});T.size&&w.push(...T)}const _=new Set(w);if(_.size)if(m===0){t=!0;break}else r=_;else{t=!1;break}}}if(t)break}n==="not"?t||(p=e):t&&(p=e)}return p??null}_matchPseudoClassSelector(a,e,n={}){const{children:f}=a,{localName:h,parentNode:d}=e,{forgive:p}=n,u=(0,k.unescapeSelector)(a.name);let t=new Set;if(Z.test(u)){let i;if(this.#i.has(a))i=this.#i.get(a);else{const c=(0,k.walkAST)(a),o=[],s=[];for(const[...r]of c){for(const _ of r){const E=(0,k.generateCSS)(_);o.push(E)}const m=[],N=new Set;let w=r.shift();for(;w;)if(w.type===b.COMBINATOR?(m.push({combo:w,leaves:[...N]}),N.clear()):w&&N.add(w),r.length)w=r.shift();else{m.push({combo:null,leaves:[...N]}),N.clear();break}s.push(m)}i={astName:u,branches:c,twigBranches:s,selector:o.join(",")},this.#i.set(a,i)}const l=this._matchLogicalPseudoFunc(i,e);l&&t.add(l)}else if(Array.isArray(f)){const[i]=f;if(K.test(u)){const l=this._matchAnPlusB(i,e,u);l.size&&(t=l)}else if(u==="dir"){const l=this._matchDirectionPseudoClass(i,e);l&&t.add(l)}else if(u==="lang"){const l=this._matchLanguagePseudoClass(i,e);l&&t.add(l)}else switch(u){case"current":case"nth-col":case"nth-last-col":{if(this.#s)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:l}=this.#t,{documentElement:c}=i,o=new URL(i.URL);switch(u){case"any-link":case"link":{R.test(h)&&e.hasAttribute("href")&&t.add(e);break}case"local-link":{if(R.test(h)&&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,g.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 r=i.getElementById(s);for(;r;){if(r===e){t.add(e);break}r=r.parentNode}}break}case"scope":{this.#e.nodeType===b.ELEMENT_NODE?e===this.#e&&t.add(e):e===c&&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":{U.test(h)&&e.hasAttribute("open")&&t.add(e);break}case"closed":{U.test(h)&&!e.hasAttribute("open")&&t.add(e);break}case"disabled":{if(M.test(h)||(0,D.default)(h))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":{(M.test(h)||(0,D.default)(h))&&!(e.disabled&&e.hasAttribute("disabled"))&&t.add(e);break}case"read-only":{switch(h){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,g.isContentEditable)(e)||t.add(e)}break}case"read-write":{switch(h){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,g.isContentEditable)(e)&&t.add(e)}break}case"placeholder-shown":{let s;h==="textarea"?s=e:h==="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":{(h==="input"&&e.hasAttribute("type")&&$.test(e.getAttribute("type"))&&e.checked||h==="option"&&e.selected)&&t.add(e);break}case"indeterminate":{if(h==="input"&&e.type==="checkbox"&&e.indeterminate||h==="progress"&&!e.hasAttribute("value"))t.add(e);else if(h==="input"&&e.type==="radio"&&!e.hasAttribute("checked")){const s=e.name;let r=e.parentNode;for(;r&&r.localName!=="form";)r=r.parentNode;r||(r=c);const m=[...r.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(h==="button"&&!(e.hasAttribute("type")&&z.test(e.getAttribute("type")))||h==="input"&&e.hasAttribute("type")&&F.test(e.getAttribute("type"))){let s=e.parentNode;for(;s&&s.localName!=="form";)s=s.parentNode;if(s){const r=i.createNodeIterator(s,b.SHOW_ELEMENT);let m=r.nextNode();for(;m;){const N=m.localName;let w;if(N==="button"?w=!(m.hasAttribute("type")&&z.test(m.getAttribute("type"))):N==="input"&&(w=m.hasAttribute("type")&&F.test(m.getAttribute("type"))),w){m===e&&t.add(e);break}m=r.nextNode()}}}else if(h==="input"&&e.hasAttribute("type")&&$.test(e.getAttribute("type"))&&e.hasAttribute("checked"))t.add(e);else if(h==="option"){let s=!1,r=d;for(;r&&r.localName!=="datalist";){if(r.localName==="select"){s=!!r.multiple;break}r=r.parentNode}if(s){if(this.#s)throw new DOMException(`Unsupported pseudo-class :${u}`,b.NOT_SUPPORTED_ERR)}else{const m=d.firstElementChild,N=new Set;let w=m;for(;w;){if(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(h))e.checkValidity()&&t.add(e);else if(/^fieldset$/.test(h)){const s=i.createNodeIterator(e,b.SHOW_ELEMENT);let r=s.nextNode();r===e&&(r=s.nextNode());let m;for(;r&&!(O.test(r.localName)&&(m=r.checkValidity(),!m));)r=s.nextNode();m&&t.add(e)}break}case"invalid":{if(O.test(h))e.checkValidity()||t.add(e);else if(/^fieldset$/.test(h)){const s=i.createNodeIterator(e,b.SHOW_ELEMENT);let r=s.nextNode();r===e&&(r=s.nextNode());let m;for(;r&&!(O.test(r.localName)&&(m=r.checkValidity(),!m));)r=s.nextNode();m||t.add(e)}break}case"in-range":{h==="input"&&!(e.readonly||e.hasAttribute("readonly"))&&!(e.disabled||e.hasAttribute("disabled"))&&e.hasAttribute("type")&&B.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":{h==="input"&&!(e.readonly||e.hasAttribute("readonly"))&&!(e.disabled||e.hasAttribute("disabled"))&&e.hasAttribute("type")&&B.test(e.getAttribute("type"))&&(e.validity.rangeUnderflow||e.validity.rangeOverflow)&&t.add(e);break}case"required":{let s;if(/^(?:select|textarea)$/.test(h))s=e;else if(h==="input")if(e.hasAttribute("type")){const r=e.getAttribute("type");(x.test(r)||$.test(r)||I.test(r)||r==="file")&&(s=e)}else s=e;s&&(e.required||e.hasAttribute("required"))&&t.add(e);break}case"optional":{let s;if(/^(?:select|textarea)$/.test(h))s=e;else if(h==="input")if(e.hasAttribute("type")){const r=e.getAttribute("type");(x.test(r)||$.test(r)||I.test(r)||r==="file")&&(s=e)}else s=e;s&&!(e.required||e.hasAttribute("required"))&&t.add(e);break}case"root":{e===c&&t.add(e);break}case"empty":{if(e.hasChildNodes()){const s=e.childNodes.values();let r;for(const m of s)if(r=m.nodeType!==b.ELEMENT_NODE&&m.nodeType!==b.TEXT_NODE,!r)break;r&&t.add(e)}else t.add(e);break}case"first-child":{(d&&e===d.firstElementChild||l.nodeType===b.ELEMENT_NODE&&e===l)&&t.add(e);break}case"last-child":{(d&&e===d.lastElementChild||l.nodeType===b.ELEMENT_NODE&&e===l)&&t.add(e);break}case"only-child":{(d&&e===d.firstElementChild&&e===d.lastElementChild||l.nodeType===b.ELEMENT_NODE&&e===l)&&t.add(e);break}case"first-of-type":{if(d){const[s]=this._collectNthOfType({a:0,b:1},e);s&&t.add(s)}else l.nodeType===b.ELEMENT_NODE&&e===l&&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 l.nodeType===b.ELEMENT_NODE&&e===l&&t.add(e);break}case"only-of-type":{if(d){const[s]=this._collectNthOfType({a:0,b:1},e);if(s===e){const[r]=this._collectNthOfType({a:0,b:1,reverse:!0},e);r===e&&t.add(e)}}else l.nodeType===b.ELEMENT_NODE&&e===l&&t.add(e);break}case"after":case"before":case"first-letter":case"first-line":{if(this.#s)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.#s)throw new DOMException(`Unsupported pseudo-class :${u}`,b.NOT_SUPPORTED_ERR);break}default:if(u.startsWith("-webkit-")){if(this.#s)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:n,matcher:f,name:h,value:d}=a;if(typeof n=="string"&&!/^[is]$/i.test(n))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 n=="string"&&/^s$/i.test(n)?i=!1:i=!0:typeof n=="string"&&/^i$/i.test(n)?i=!0:i=!1;let{name:l}=h;l=(0,k.unescapeSelector)(l),i&&(l=l.toLowerCase());const c=new Set;if(/\|/.test(l)){const{prefix:o,tagName:s}=(0,g.selectorToNodeProps)(l);for(let{name:r,value:m}of p)switch(i&&(r=r.toLowerCase(),m=m.toLowerCase()),o){case"":{s===r&&c.add(m);break}case"*":{/:/.test(r)?r.endsWith(`:${s}`)&&c.add(m):s===r&&c.add(m);break}default:if(/:/.test(r)){const[N,w]=r.split(":");o===N&&s===w&&(0,g.isNamespaceDeclared)(o,e)&&c.add(m)}}}else for(let{name:o,value:s}of p)if(i&&(o=o.toLowerCase(),s=s.toLowerCase()),/:/.test(o)){const[,r]=o.split(":");l===r&&c.add(s)}else l===o&&c.add(s);if(c.size){const{name:o,value:s}=d||{};let r;switch(o?i?r=o.toLowerCase():r=o:s?i?r=s.toLowerCase():r=s:s===""&&(r=s),f){case"=":{typeof r=="string"&&c.has(r)&&(u=e);break}case"~=":{if(r&&typeof r=="string"){for(const m of c)if(new Set(m.split(/\s+/)).has(r)){u=e;break}}break}case"|=":{if(r&&typeof r=="string"){let m;for(const N of c)if(N===r||N.startsWith(`${r}-`)){m=N;break}m&&(u=e)}break}case"^=":{if(r&&typeof r=="string"){let m;for(const N of c)if(N.startsWith(`${r}`)){m=N;break}m&&(u=e)}break}case"$=":{if(r&&typeof r=="string"){let m;for(const N of c)if(N.endsWith(`${r}`)){m=N;break}m&&(u=e)}break}case"*=":{if(r&&typeof r=="string"){let m;for(const N of c)if(N.includes(`${r}`)){m=N;break}m&&(u=e)}break}case null:default:u=e}}}return u??null}_matchClassSelector(a,e){const n=(0,k.unescapeSelector)(a.name);let f;return e.classList.contains(n)&&(f=e),f??null}_matchIDSelector(a,e){const{id:n}=e,f=(0,k.unescapeSelector)(a.name);let h;return f===n&&(h=e),h??null}_matchTypeSelector(a,e){const n=(0,k.unescapeSelector)(a.name),{localName:f,prefix:h}=e,{document:d}=this.#t;let{prefix:p,tagName:u}=(0,g.selectorToNodeProps)(n,e);d.contentType==="text/html"&&(p=p.toLowerCase(),u=u.toLowerCase());let t,i;/:/.test(f)?[t,i]=f.split(":"):(t=h||"",i=f);let l;return p===""&&t===""?e.namespaceURI===null&&(u==="*"||u===i)&&(l=e):p==="*"?(u==="*"||u===i)&&(l=e):p===t&&(0,g.isNamespaceDeclared)(p,e)&&(u==="*"||u===i)&&(l=e),l??null}_matchSelector(a,e,n){const{type:f}=a;let h=new Set;if(e.nodeType===b.ELEMENT_NODE)switch(f){case b.ATTRIBUTE_SELECTOR:{const d=this._matchAttributeSelector(a,e);d&&h.add(d);break}case b.CLASS_SELECTOR:{const d=this._matchClassSelector(a,e);d&&h.add(d);break}case b.ID_SELECTOR:{const d=this._matchIDSelector(a,e);d&&h.add(d);break}case b.PSEUDO_CLASS_SELECTOR:{const d=this._matchPseudoClassSelector(a,e,n);d.size&&(h=d);break}case b.PSEUDO_ELEMENT_SELECTOR:{const d=(0,k.unescapeSelector)(a.name);this._matchPseudoElementSelector(d,n);break}case b.TYPE_SELECTOR:default:{const d=this._matchTypeSelector(a,e);d&&h.add(d)}}return h}_matchLeaves(a,e,n){let f;for(const h of a)if(f=this._matchSelector(h,e,n).has(e),!f)break;return!!f}_findDescendantNodes(a,e){const[n,...f]=a,{type:h}=n,d=(0,k.unescapeSelector)(n.name),p=f.length>0,{document:u,root:t}=this.#t;let i=new Set,l=!1;switch(h){case b.ID_SELECTOR:{if(t.nodeType===b.ELEMENT_NODE)l=!0;else{const c=t.getElementById(d);if(c&&c!==e){const o=(0,g.isSameOrDescendant)(c,e);let s;o&&(s=c),s&&(p?this._matchLeaves(f,s)&&i.add(s):i.add(s))}}break}case b.CLASS_SELECTOR:{const c=[...e.getElementsByClassName(d)];if(c.length)if(p)for(const o of c)this._matchLeaves(f,o)&&i.add(o);else i=new Set(c);break}case b.TYPE_SELECTOR:{if(u.contentType!=="text/html"||/[*|]/.test(d))l=!0;else{const c=[...e.getElementsByTagName(d)];if(c.length)if(p)for(const o of c)this._matchLeaves(f,o)&&i.add(o);else i=new Set(c)}break}case b.PSEUDO_ELEMENT_SELECTOR:{this._matchPseudoElementSelector(d);break}default:l=!0}return{nodes:i,pending:l}}_matchCombinator(a,e,n={}){const{combo:f,leaves:h}=a,{name:d}=f,{find:p,forgive:u}=n;let t=new Set;if(p==="next")switch(d){case"+":{const i=e.nextElementSibling;i&&this._matchLeaves(h,i)&&t.add(i);break}case"~":{let i=e.nextElementSibling;for(;i;)this._matchLeaves(h,i)&&t.add(i),i=i.nextElementSibling;break}case">":{const i=[...e.children];for(const l of i)this._matchLeaves(h,l)&&t.add(l);break}case" ":default:{const{nodes:i,pending:l}=this._findDescendantNodes(h,e);if(i.size)t=i;else if(l){const{document:c}=this.#t,o=c.createNodeIterator(e,b.SHOW_ELEMENT);let s=o.nextNode();for(s===e&&(s=o.nextNode());s;)this._matchLeaves(h,s)&&t.add(s),s=o.nextNode()}}}else switch(d){case"+":{const i=e.previousElementSibling;i&&this._matchLeaves(h,i,{forgive:u})&&t.add(i);break}case"~":{const i=[];let l=e.previousElementSibling;for(;l;)this._matchLeaves(h,l,{forgive:u})&&i.push(l),l=l.previousElementSibling;i.length&&(t=new Set(i.reverse()));break}case">":{const i=e.parentNode;i&&this._matchLeaves(h,i,{forgive:u})&&t.add(i);break}case" ":default:{const i=[];let l=e.parentNode;for(;l;)this._matchLeaves(h,l,{forgive:u})&&i.push(l),l=l.parentNode;i.length&&(t=new Set(i.reverse()))}}return t}_findNodes(a,e){const{leaves:[n,...f]}=a,{type:h}=n,d=(0,k.unescapeSelector)(n.name),p=f.length>0,{document:u,root:t}=this.#t;let i=new Set,l=!1;switch(h){case b.ID_SELECTOR:{let c;if(e===v)this._matchLeaves([n],this.#e)&&(c=this.#e);else if(e===S){let o=this.#e;for(;o;){if(this._matchLeaves([n],o)){c=o;break}o=o.parentNode}}else t.nodeType===b.ELEMENT_NODE?l=!0:c=t.getElementById(d);c&&(p?this._matchLeaves(f,c)&&i.add(c):i.add(c));break}case b.CLASS_SELECTOR:{const c=[];if(e===v)this.#e.nodeType===b.ELEMENT_NODE&&this.#e.classList.contains(d)&&c.push(this.#e);else if(e===S){let o=this.#e;for(;o&&o.nodeType===b.ELEMENT_NODE;)o.classList.contains(d)&&c.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)&&c.push(s);const r=[...s.getElementsByClassName(d)];c.push(...r)}}else{const o=[...t.getElementsByClassName(d)];c.push(...o)}if(c.length)if(p)for(const o of c)this._matchLeaves(f,o)&&i.add(o);else i=new Set(c);break}case b.TYPE_SELECTOR:{const c=[];if(e===v)this.#e.nodeType===b.ELEMENT_NODE&&this._matchLeaves([n],this.#e)&&c.push(this.#e);else if(e===S){let o=this.#e;for(;o&&o.nodeType===b.ELEMENT_NODE;)this._matchLeaves([n],o)&&c.push(o),o=o.parentNode}else if(u.contentType!=="text/html"||/[*|]/.test(d))l=!0;else if(t.nodeType===b.DOCUMENT_FRAGMENT_NODE){const o=d.toLowerCase(),s=[...t.children];for(const r of s){r.localName===o&&c.push(r);const m=[...r.getElementsByTagName(d)];c.push(...m)}}else{const o=[...t.getElementsByTagName(d)];c.push(...o)}if(c.length)if(p)for(const o of c)this._matchLeaves(f,o)&&i.add(o);else i=new Set(c);break}case b.PSEUDO_ELEMENT_SELECTOR:{this._matchPseudoElementSelector(d);break}default:{const c=[];if(e===v)this._matchLeaves([n],this.#e)&&c.push(this.#e);else if(e===S){let o=this.#e;for(;o;)this._matchLeaves([n],o)&&c.push(o),o=o.parentNode}else l=!0;if(c.length)if(p)for(const o of c)this._matchLeaves(f,o)&&i.add(o);else i=new Set(c)}}return{nodes:i,pending:l}}_collectNodes(a){const e=this.#a.values();if(a===C||a===A){const n=new Set;let f=0;for(const{branch:h}of e){const d=h[0],{nodes:p,pending:u}=this._findNodes(d,a);p.size?this.#r[f]=p:u?n.add(new Map([["index",f],["twig",d]])):this.#a[f].skip=!0,f++}if(n.size){const{document:h,root:d}=this.#t,p=h.createNodeIterator(d,b.SHOW_ELEMENT);let u=p.nextNode();for(;u;){let t=!1;if(this.#e.nodeType===b.ELEMENT_NODE?t=(0,g.isSameOrDescendant)(u,this.#e):t=!0,t)for(const i of n){const{leaves:l}=i.get("twig");if(this._matchLeaves(l,u)){const o=i.get("index");this.#r[o].add(u)}}u=p.nextNode()}}}else{let n=0;for(const{branch:f}of e){const h=f[f.length-1],{nodes:d}=this._findNodes(h,a);d.size?this.#r[n]=d:this.#a[n].skip=!0,n++}}return[this.#a,this.#r]}_matchNodes(a){const[...e]=this.#a,n=e.length;let f=new Set;for(let h=0;h<n;h++){const{branch:d,skip:p}=e[h],u=d.length;if(!p&&u){const t=this.#r[h],i=u-1;if(i===0)if((a===C||a===A)&&this.#e.nodeType===b.ELEMENT_NODE){for(const l of t)if(l!==this.#e&&(0,g.isSameOrDescendant)(l,this.#e)&&(f.add(l),a===A))break}else if(a===A){const[l]=[...t];f.add(l)}else{const l=[...f],c=[...t];f=new Set([...l,...c])}else if(a===C||a===A){let{combo:l}=d[0];for(const c of t){let o=new Set([c]);for(let s=1;s<u;s++){const{combo:r,leaves:m}=d[s],N=[];for(const _ of o){const E={combo:l,leaves:m},T=this._matchCombinator(E,_,{find:"next"});T.size&&N.push(...T)}const w=new Set(N);if(w.size)if(s===i){if(a===A){const[_]=[...w];f.add(_)}else{const _=[...f],E=[...w];f=new Set([..._,...E])}break}else l=r,o=w;else break}}}else for(const l of t){let c=new Set([l]),o;for(let s=i-1;s>=0;s--){const r=d[s],m=[];for(const w of c){const _=this._matchCombinator(r,w,{find:"prev"});_.size&&m.push(..._)}const N=new Set(m);if(N.size)if(o=!0,s===0){f.add(l);break}else c=N;else{o=!1;break}}if(o)break}}}return f}_find(a){return this._collectNodes(a),this._matchNodes(a)}_sortNodes(a){const e=[...a];return e.length>1&&e.sort((n,f)=>{let h;const d=n.compareDocumentPosition(f);return d&b.DOCUMENT_POSITION_PRECEDING||d&b.DOCUMENT_POSITION_CONTAINS?h=1:h=-1,h}),e}matches(){if(this.#e.nodeType!==b.ELEMENT_NODE)throw new TypeError(`Unexpected node ${this.#e.nodeName}`);let a;try{a=this._find(v).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(S);let n=this.#e;for(;n;){if(e.has(n)){a=n;break}n=n.parentNode}}catch(e){this._onError(e)}return a??null}querySelector(){let a;try{const e=this._find(A);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(C);e.delete(this.#e),e.size>1&&this.#l?a.push(...this._sortNodes(e)):e.size&&a.push(...e)}catch(e){this._onError(e)}return a}}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,r)=>{for(var e in r)L(y,e,{get:r[e],enumerable:!0})},R=(y,r,e,c)=>{if(r&&typeof r=="object"||typeof r=="function")for(let h of W(r))!G.call(y,h)&&h!==e&&L(y,h,{get:()=>r[h],enumerable:!(c=H(r,h))||c.enumerable});return y};var X=(y,r,e)=>(e=y!=null?j(q(y)):{},R(r||!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",T="first",v="lineal",S="self",U=/^(?:(?:fieldse|inpu|selec)t|button|opt(?:group|ion)|textarea)$/,$=/^(?:(?:(?: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;#r;#a;#e;#l;#t;#o;#c;#i;constructor(r,e,c={}){const{sort:h,warn:n}=c;this.#r=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=r,this.#e=e,this.#c=!!h,this.#i=!!n,[this.#s,this.#l]=this._prepare(r),this.#t=this._getRoot(e)}_onError(r){if(r instanceof DOMException&&r.name===b.NOT_SUPPORTED_ERR)this.#i&&console.warn(r.message);else throw r}_getRoot(r=this.#e){let e,c;switch(r.nodeType){case b.DOCUMENT_NODE:{e=r,c=r;break}case b.DOCUMENT_FRAGMENT_NODE:{e=r.ownerDocument,c=r;break}case b.ELEMENT_NODE:{if((0,k.isSameOrDescendant)(r))e=r.ownerDocument,c=r.ownerDocument;else{let h=r;for(;h&&h.parentNode;)h=h.parentNode;e=h.ownerDocument,c=h}break}default:throw new TypeError(`Unexpected node ${r.nodeName}`)}return{document:e,root:c}}_sortLeaves(r){const e=[...r];return e.length>1&&e.sort((c,h)=>{const{type:n}=c,{type:d}=h,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(r=this.#o){const e=(0,g.parseSelector)(r),c=(0,g.walkAST)(e),h=[],n=[];let d=0;for(const[...p]of c){const u=[];let t=p.shift();if(t&&t.type!==b.COMBINATOR){const i=new Set;for(;t;){if(t.type===b.COMBINATOR){const[f]=p;if(f.type===b.COMBINATOR){const o=`Invalid combinator ${t.name}${f.name}`;throw new DOMException(o,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}}}h.push({branch:u,find:null,skip:!1}),n[d]=new Set,d++}return[h,n]}_collectNthChild(r,e){const{a:c,b:h,reverse:n,selector:d}=r,{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],f=i.length;if(f){const o=new Set;if(t){const l=t.length;for(const s of i){let a;for(let m=0;m<l;m++){const N=t[m];if(a=this._matchLeaves(N,s),!a)break}a&&o.add(s)}}if(n&&i.reverse(),c===0){if(h>0&&h<=f){if(o.size)for(let l=0;l<f;l++){const s=i[l];if(o.has(s)){u.add(s);break}}else if(!d){const l=i[h-1];u.add(l)}}}else{let l=h-1;if(c>0)for(;l<0;)l+=c;if(l>=0&&l<f){let s=c>0?0:h-1;for(let a=0;a<f&&l>=0&&l<f;a++){const m=i[a];o.size?o.has(m)&&(s===l&&(u.add(m),l+=c),c>0?s++:s--):a===l&&(d||u.add(m),l+=c)}}}}}else{const{root:i}=this.#t;if(i.nodeType===b.ELEMENT_NODE&&e===i&&c+h===1)if(t){const f=t.length;let o;for(let l=0;l<f;l++){const s=t[l];if(o=this._matchLeaves(s,e),o)break}o&&u.add(e)}else u.add(e)}return u}_collectNthOfType(r,e){const{a:c,b:h,reverse:n}=r,{localName:d,parentNode:p,prefix:u}=e,t=new Set;if(p){const i=[...p.children],f=i.length;if(f)if(n&&i.reverse(),c===0){if(h>0&&h<=f){let o=0;for(let l=0;l<f;l++){const s=i[l],{localName:a,prefix:m}=s;if(a===d&&m===u){if(o===h-1){t.add(s);break}o++}}}}else{let o=h-1;if(c>0)for(;o<0;)o+=c;if(o>=0&&o<f){let l=c>0?0:h-1;for(let s=0;s<f;s++){const a=i[s],{localName:m,prefix:N}=a;if(m===d&&N===u){if(l===o&&(t.add(a),o+=c),o<0||o>=f)break;c>0?l++:l--}}}}}else{const{root:i}=this.#t;i.nodeType===b.ELEMENT_NODE&&e===i&&c+h===1&&t.add(e)}return t}_matchAnPlusB(r,e,c){const{nth:{a:h,b:n,name:d},selector:p}=r,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(c)&&t.set("reverse",!0)):(typeof h=="string"&&/-?\d+/.test(h)?t.set("a",h*1):t.set("a",0),typeof n=="string"&&/-?\d+/.test(n)?t.set("b",n*1):t.set("b",0),/last/.test(c)&&t.set("reverse",!0));let i=new Set;if(t.has("a")&&t.has("b")){if(/^nth-(?:last-)?child$/.test(c)){p&&t.set("selector",p);const f=Object.fromEntries(t),o=this._collectNthChild(f,e);o.size&&(i=o)}else if(/^nth-(?:last-)?of-type$/.test(c)){const f=Object.fromEntries(t),o=this._collectNthOfType(f,e);o.size&&(i=o)}}return i}_matchPseudoElementSelector(r,e={}){const{forgive:c}=e;switch(r){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 ::${r}`,b.NOT_SUPPORTED_ERR);break}default:if(r.startsWith("-webkit-")){if(this.#i)throw new DOMException(`Unsupported pseudo-element ::${r}`,b.NOT_SUPPORTED_ERR)}else if(!c)throw new DOMException(`Unknown pseudo-element ::${r}`,b.SYNTAX_ERR)}}_matchDirectionPseudoClass(r,e){const c=(0,g.unescapeSelector)(r.name),h=(0,k.getDirectionality)(e);let n;return c===h&&(n=e),n??null}_matchLanguagePseudoClass(r,e){const c=(0,g.unescapeSelector)(r.name);let h;if(c){if(c==="*")if(e.hasAttribute("lang"))e.getAttribute("lang")&&(h=e);else{let n=e.parentNode;for(;n;){if(n.hasAttribute("lang")){n.getAttribute("lang")&&(h=e);break}n=n.parentNode}}else if(/[A-Z\d-]+/i.test(c)){const n="[A-Z\\d]+",d=`(?:-${n})*`;let p;if(/-/.test(c)){const[u,t,...i]=c.split("-");let f;u==="*"?f=`${n}${d}`:f=`${u}${d}`;const o=`-${t}${d}`,l=i.length;let s="";if(l)for(let a=0;a<l;a++)s+=`-${i[a]}${d}`;p=new RegExp(`^${f}${o}${s}$`,"i")}else p=new RegExp(`^${c}${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(r,e){let c;if(Array.isArray(r)&&r.length){const[h]=r,{type:n}=h;let d;n===b.COMBINATOR?d=r.shift():d={name:" ",type:b.COMBINATOR};const p=[];for(;r.length;){const[i]=r,{type:f}=i;if(f===b.COMBINATOR)break;p.push(r.shift())}const u={combo:d,leaves:p},t=this._matchCombinator(u,e,{find:"next"});if(t.size)if(r.length){for(const i of t)if(c=this._matchHasPseudoFunc(Object.assign([],r),i),c)break}else c=!0}return!!c}_matchLogicalPseudoFunc(r,e){const{astName:c="",branches:h=[],selector:n="",twigBranches:d=[]}=r;let p;if(c==="has")if(n.includes(":has("))p=null;else{let u;const t=h.length;for(let i=0;i<t;i++){const f=h[i];if(u=this._matchHasPseudoFunc(Object.assign([],f),e),u)break}u&&(p=e)}else{const u=/^(?:is|where)$/.test(c);let t;const i=d.length;for(let f=0;f<i;f++){const o=d[f],l=o.length-1,{leaves:s}=o[l];if(t=this._matchLeaves(s,e,{forgive:u}),t&&l>0){let a=new Set([e]);for(let m=l-1;m>=0;m--){const N=o[m],w=[];for(const _ of a){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 a=E;else{t=!1;break}}}if(t)break}c==="not"?t||(p=e):t&&(p=e)}return p??null}_matchPseudoClassSelector(r,e,c={}){const{children:h}=r,{localName:n,parentNode:d}=e,{forgive:p}=c,u=(0,g.unescapeSelector)(r.name);let t=new Set;if(K.test(u)){let i;if(this.#a.has(r))i=this.#a.get(r);else{const o=(0,g.walkAST)(r),l=[],s=[];for(const[...a]of o){for(const E of a){const _=(0,g.generateCSS)(E);l.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:o,twigBranches:s,selector:l.join(",")},this.#a.set(r,i)}const f=this._matchLogicalPseudoFunc(i,e);f&&t.add(f)}else if(Array.isArray(h)){const[i]=h;if(J.test(u)){const f=this._matchAnPlusB(i,e,u);f.size&&(t=f)}else if(u==="dir"){const f=this._matchDirectionPseudoClass(i,e);f&&t.add(f)}else if(u==="lang"){const f=this._matchLanguagePseudoClass(i,e);f&&t.add(f)}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:f}=this.#t,{documentElement:o}=i,l=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"),l.href);s.origin===l.origin&&s.pathname===l.pathname&&t.add(e)}break}case"visited":break;case"target":{(0,k.isSameOrDescendant)(e)&&l.hash&&e.id&&l.hash===`#${e.id}`&&t.add(e);break}case"target-within":{if(l.hash){const s=l.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===o&&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")&&O.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=o);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")&&O.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($.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&&!($.test(a.localName)&&(m=a.checkValidity(),!m));)a=s.nextNode();m&&t.add(e)}break}case"invalid":{if($.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&&!($.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)||O.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)||O.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===o&&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||f.nodeType===b.ELEMENT_NODE&&e===f)&&t.add(e);break}case"last-child":{(d&&e===d.lastElementChild||f.nodeType===b.ELEMENT_NODE&&e===f)&&t.add(e);break}case"only-child":{(d&&e===d.firstElementChild&&e===d.lastElementChild||f.nodeType===b.ELEMENT_NODE&&e===f)&&t.add(e);break}case"first-of-type":{if(d){const[s]=this._collectNthOfType({a:0,b:1},e);s&&t.add(s)}else f.nodeType===b.ELEMENT_NODE&&e===f&&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 f.nodeType===b.ELEMENT_NODE&&e===f&&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 f.nodeType===b.ELEMENT_NODE&&e===f&&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(r,e){const{flags:c,matcher:h,name:n,value:d}=r;if(typeof c=="string"&&!/^[is]$/i.test(c))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 c=="string"&&/^s$/i.test(c)?i=!1:i=!0:typeof c=="string"&&/^i$/i.test(c)?i=!0:i=!1;let{name:f}=n;f=(0,g.unescapeSelector)(f),i&&(f=f.toLowerCase());const o=new Set;if(/\|/.test(f)){const{prefix:l,tagName:s}=(0,k.selectorToNodeProps)(f);for(let{name:a,value:m}of p)switch(i&&(a=a.toLowerCase(),m=m.toLowerCase()),l){case"":{s===a&&o.add(m);break}case"*":{/:/.test(a)?a.endsWith(`:${s}`)&&o.add(m):s===a&&o.add(m);break}default:if(/:/.test(a)){const[N,w]=a.split(":");l===N&&s===w&&(0,k.isNamespaceDeclared)(l,e)&&o.add(m)}}}else for(let{name:l,value:s}of p)if(i&&(l=l.toLowerCase(),s=s.toLowerCase()),/:/.test(l)){const[a,m]=l.split(":");if(a==="xml"&&m==="lang")continue;f===m&&o.add(s)}else f===l&&o.add(s);if(o.size){const{name:l,value:s}=d||{};let a;switch(l?i?a=l.toLowerCase():a=l:s?i?a=s.toLowerCase():a=s:s===""&&(a=s),h){case"=":{typeof a=="string"&&o.has(a)&&(u=e);break}case"~=":{if(a&&typeof a=="string"){for(const m of o)if(new Set(m.split(/\s+/)).has(a)){u=e;break}}break}case"|=":{if(a&&typeof a=="string"){let m;for(const N of o)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 o)if(N.startsWith(`${a}`)){m=N;break}m&&(u=e)}break}case"$=":{if(a&&typeof a=="string"){let m;for(const N of o)if(N.endsWith(`${a}`)){m=N;break}m&&(u=e)}break}case"*=":{if(a&&typeof a=="string"){let m;for(const N of o)if(N.includes(`${a}`)){m=N;break}m&&(u=e)}break}case null:default:u=e}}}return u??null}_matchClassSelector(r,e){const c=(0,g.unescapeSelector)(r.name);let h;return e.classList.contains(c)&&(h=e),h??null}_matchIDSelector(r,e){const{id:c}=e,h=(0,g.unescapeSelector)(r.name);let n;return h===c&&(n=e),n??null}_matchTypeSelector(r,e){const c=(0,g.unescapeSelector)(r.name),{localName:h,prefix:n}=e,{document:d}=this.#t;let{prefix:p,tagName:u}=(0,k.selectorToNodeProps)(c,e);d.contentType==="text/html"&&(p=p.toLowerCase(),u=u.toLowerCase());let t,i;/:/.test(h)?[t,i]=h.split(":"):(t=n||"",i=h);let f;return p===""&&t===""?e.namespaceURI===null&&(u==="*"||u===i)&&(f=e):p==="*"?(u==="*"||u===i)&&(f=e):p===t&&(0,k.isNamespaceDeclared)(p,e)&&(u==="*"||u===i)&&(f=e),f??null}_matchSelector(r,e,c){const{type:h}=r;let n=new Set;if(e.nodeType===b.ELEMENT_NODE)switch(h){case b.ATTRIBUTE_SELECTOR:{const d=this._matchAttributeSelector(r,e);d&&n.add(d);break}case b.CLASS_SELECTOR:{const d=this._matchClassSelector(r,e);d&&n.add(d);break}case b.ID_SELECTOR:{const d=this._matchIDSelector(r,e);d&&n.add(d);break}case b.PSEUDO_CLASS_SELECTOR:{const d=this._matchPseudoClassSelector(r,e,c);d.size&&(n=d);break}case b.PSEUDO_ELEMENT_SELECTOR:{const d=(0,g.unescapeSelector)(r.name);this._matchPseudoElementSelector(d,c);break}case b.TYPE_SELECTOR:default:{const d=this._matchTypeSelector(r,e);d&&n.add(d)}}return n}_matchLeaves(r,e,c){let h;for(const n of r)if(h=this._matchSelector(n,e,c).has(e),!h)break;return!!h}_findDescendantNodes(r,e){const[c,...h]=r,{type:n}=c,d=(0,g.unescapeSelector)(c.name),p=h.length>0,{document:u,root:t}=this.#t;let i=new Set,f=!1;switch(n){case b.ID_SELECTOR:{if(t.nodeType===b.ELEMENT_NODE)f=!0;else{const o=t.getElementById(d);if(o&&o!==e){const l=(0,k.isSameOrDescendant)(o,e);let s;l&&(s=o),s&&(p?this._matchLeaves(h,s)&&i.add(s):i.add(s))}}break}case b.CLASS_SELECTOR:{const o=[...e.getElementsByClassName(d)];if(o.length)if(p)for(const l of o)this._matchLeaves(h,l)&&i.add(l);else i=new Set(o);break}case b.TYPE_SELECTOR:{if(u.contentType!=="text/html"||/[*|]/.test(d))f=!0;else{const o=[...e.getElementsByTagName(d)];if(o.length)if(p)for(const l of o)this._matchLeaves(h,l)&&i.add(l);else i=new Set(o)}break}case b.PSEUDO_ELEMENT_SELECTOR:{this._matchPseudoElementSelector(d);break}default:f=!0}return{nodes:i,pending:f}}_matchCombinator(r,e,c={}){const{combo:h,leaves:n}=r,{name:d}=h,{find:p,forgive:u}=c;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 f of i)this._matchLeaves(n,f)&&t.add(f);break}case" ":default:{const{nodes:i,pending:f}=this._findDescendantNodes(n,e);if(i.size)t=i;else if(f){const{document:o}=this.#t,l=o.createNodeIterator(e,b.SHOW_ELEMENT);let s=l.nextNode();for(s===e&&(s=l.nextNode());s;)this._matchLeaves(n,s)&&t.add(s),s=l.nextNode()}}}else switch(d){case"+":{const i=e.previousElementSibling;i&&this._matchLeaves(n,i,{forgive:u})&&t.add(i);break}case"~":{const i=[];let f=e.previousElementSibling;for(;f;)this._matchLeaves(n,f,{forgive:u})&&i.push(f),f=f.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 f=e.parentNode;for(;f;)this._matchLeaves(n,f,{forgive:u})&&i.push(f),f=f.parentNode;i.length&&(t=new Set(i.reverse()))}}return t}_findNodes(r,e){const{leaves:[c,...h]}=r,{type:n}=c,d=(0,g.unescapeSelector)(c.name),p=h.length>0,{document:u,root:t}=this.#t;let i=new Set,f=!1;switch(n){case b.ID_SELECTOR:{let o;if(e===S)this._matchLeaves([c],this.#e)&&(o=this.#e);else if(e===v){let l=this.#e;for(;l;){if(this._matchLeaves([c],l)){o=l;break}l=l.parentNode}}else t.nodeType===b.ELEMENT_NODE?f=!0:o=t.getElementById(d);o&&(p?this._matchLeaves(h,o)&&i.add(o):i.add(o));break}case b.CLASS_SELECTOR:{const o=[];if(e===S)this.#e.nodeType===b.ELEMENT_NODE&&this.#e.classList.contains(d)&&o.push(this.#e);else if(e===v){let l=this.#e;for(;l&&l.nodeType===b.ELEMENT_NODE;)l.classList.contains(d)&&o.push(l),l=l.parentNode}else if(t.nodeType===b.DOCUMENT_FRAGMENT_NODE){const l=[...t.children];for(const s of l){s.classList.contains(d)&&o.push(s);const a=[...s.getElementsByClassName(d)];o.push(...a)}}else{const l=[...t.getElementsByClassName(d)];o.push(...l)}if(o.length)if(p)for(const l of o)this._matchLeaves(h,l)&&i.add(l);else i=new Set(o);break}case b.TYPE_SELECTOR:{const o=[];if(e===S)this.#e.nodeType===b.ELEMENT_NODE&&this._matchLeaves([c],this.#e)&&o.push(this.#e);else if(e===v){let l=this.#e;for(;l&&l.nodeType===b.ELEMENT_NODE;)this._matchLeaves([c],l)&&o.push(l),l=l.parentNode}else if(u.contentType!=="text/html"||/[*|]/.test(d))f=!0;else if(t.nodeType===b.DOCUMENT_FRAGMENT_NODE){const l=d.toLowerCase(),s=[...t.children];for(const a of s){a.localName===l&&o.push(a);const m=[...a.getElementsByTagName(d)];o.push(...m)}}else{const l=[...t.getElementsByTagName(d)];o.push(...l)}if(o.length)if(p)for(const l of o)this._matchLeaves(h,l)&&i.add(l);else i=new Set(o);break}case b.PSEUDO_ELEMENT_SELECTOR:{this._matchPseudoElementSelector(d);break}default:{const o=[];if(e===S)this._matchLeaves([c],this.#e)&&o.push(this.#e);else if(e===v){let l=this.#e;for(;l;)this._matchLeaves([c],l)&&o.push(l),l=l.parentNode}else f=!0;if(o.length)if(p)for(const l of o)this._matchLeaves(h,l)&&i.add(l);else i=new Set(o)}}return{nodes:i,pending:f}}_getFirstTwig(r){const e=r.length-1,c=r[0];let h,n;if(e){const d=r[e],{leaves:[{type:p}]}=d;p===b.PSEUDO_ELEMENT_SELECTOR||p===b.ID_SELECTOR?(h="prev",n=d):(h="next",n=c)}else h="prev",n=c;return{find:h,twig:n}}_collectNodes(r){const e=this.#s.values();if(r===C||r===T){const c=new Set;let h=0;for(const{branch:n}of e){const{find:d,twig:p}=this._getFirstTwig(n),{nodes:u,pending:t}=this._findNodes(p,r);u.size?this.#l[h]=u:t?c.add(new Map([["index",h],["twig",p]])):this.#s[h].skip=!0,this.#s[h].find=d,h++}if(c.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 c){const{leaves:f}=i.get("twig");if(this._matchLeaves(f,u)){const l=i.get("index");this.#l[l].add(u)}}u=p.nextNode()}}}else{let c=0;for(const{branch:h}of e){const n=h[h.length-1],{nodes:d}=this._findNodes(n,r);d.size?this.#l[c]=d:this.#s[c].skip=!0,this.#s[c].find="prev",c++}}return[this.#s,this.#l]}_matchNodes(r){const[...e]=this.#s,c=e.length;let h=new Set;for(let n=0;n<c;n++){const{branch:d,find:p,skip:u}=e[n],t=d.length;if(!u&&t){const i=this.#l[n],f=t-1;if(f===0)if((r===C||r===T)&&this.#e.nodeType===b.ELEMENT_NODE){for(const o of i)if(o!==this.#e&&(0,k.isSameOrDescendant)(o,this.#e)&&(h.add(o),r===T))break}else if(r===T){const[o]=[...i];h.add(o)}else{const o=[...h],l=[...i];h=new Set([...o,...l])}else if(p==="next"){let{combo:o}=d[0];for(const l of i){let s=new Set([l]);for(let a=1;a<t;a++){const{combo:m,leaves:N}=d[a],w=[];for(const _ of s){const A={combo:o,leaves:N},P=this._matchCombinator(A,_,{find:p});P.size&&w.push(...P)}const E=new Set(w);if(E.size)if(a===f){if(r===T){const[_]=[...E];h.add(_)}else{const _=[...h],A=[...E];h=new Set([..._,...A])}break}else o=m,s=E;else break}}}else for(const o of i){let l=new Set([o]),s;for(let a=f-1;a>=0;a--){const m=d[a],N=[];for(const E of l){const _=this._matchCombinator(m,E,{find:p});_.size&&N.push(..._)}const w=new Set(N);if(w.size)if(s=!0,a===0){h.add(o);break}else l=w;else{s=!1;break}}if(s&&r!==C)break}}}return h}_find(r){return this._collectNodes(r),this._matchNodes(r)}_sortNodes(r){const e=[...r];return e.length>1&&e.sort((c,h)=>{let n;const d=c.compareDocumentPosition(h);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 r;try{r=this._find(S).has(this.#e)}catch(e){this._onError(e)}return!!r}closest(){if(this.#e.nodeType!==b.ELEMENT_NODE)throw new TypeError(`Unexpected node ${this.#e.nodeName}`);let r;try{const e=this._find(v);let c=this.#e;for(;c;){if(e.has(c)){r=c;break}c=c.parentNode}}catch(e){this._onError(e)}return r??null}querySelector(){let r;try{const e=this._find(T);e.delete(this.#e),e.size>1?[r]=this._sortNodes(e):e.size&&([r]=[...e])}catch(e){this._onError(e)}return r??null}querySelectorAll(){const r=[];try{const e=this._find(C);e.delete(this.#e),e.size>1&&this.#c?r.push(...this._sortNodes(e)):e.size&&r.push(...e)}catch(e){this._onError(e)}return r}}0&&(module.exports={Matcher});
2
2
  //# sourceMappingURL=matcher.js.map