@asamuzakjp/dom-selector 2.0.1 → 2.0.2-a.1
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 +4 -106
- package/dist/cjs/index.js +2 -2
- package/dist/cjs/index.js.map +3 -3
- package/dist/cjs/js/matcher.js +1 -1
- package/dist/cjs/js/matcher.js.map +3 -3
- package/package.json +4 -3
- package/src/index.js +55 -8
- package/src/js/matcher.js +113 -69
- package/types/js/matcher.d.ts +6 -9
package/README.md
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
[](https://www.npmjs.com/package/@asamuzakjp/dom-selector)
|
|
6
6
|
|
|
7
7
|
A CSS selector engine.
|
|
8
|
+
Used in jsdom since [jsdom v23.2.0](https://github.com/jsdom/jsdom/releases/tag/23.2.0).
|
|
8
9
|
|
|
9
10
|
## Install
|
|
10
11
|
|
|
@@ -156,114 +157,11 @@ Returns **[Array][62]<([object][60] \| [undefined][63])>** array of matched n
|
|
|
156
157
|
|:host‑context(s)|✓| |
|
|
157
158
|
|
|
158
159
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
``` javascript
|
|
162
|
-
import { JSDOM } from 'jsdom';
|
|
163
|
-
import {
|
|
164
|
-
closest, matches, querySelector, querySelectorAll
|
|
165
|
-
} from '@asamuzakjp/dom-selector';
|
|
166
|
-
|
|
167
|
-
const dom = new JSDOM('', {
|
|
168
|
-
runScripts: 'dangerously',
|
|
169
|
-
url: 'http://localhost/',
|
|
170
|
-
beforeParse: window => {
|
|
171
|
-
window.Element.prototype.matches = function (...args) {
|
|
172
|
-
if (!args.length) {
|
|
173
|
-
throw new window.TypeError('1 argument required, but only 0 present.');
|
|
174
|
-
}
|
|
175
|
-
const [selector] = args;
|
|
176
|
-
return matches(selector, this);
|
|
177
|
-
};
|
|
178
|
-
window.Element.prototype.closest = function (...args) {
|
|
179
|
-
if (!args.length) {
|
|
180
|
-
throw new window.TypeError('1 argument required, but only 0 present.');
|
|
181
|
-
}
|
|
182
|
-
const [selector] = args;
|
|
183
|
-
return closest(selector, this);
|
|
184
|
-
};
|
|
185
|
-
window.Document.prototype.querySelector = function (...args) {
|
|
186
|
-
if (!args.length) {
|
|
187
|
-
throw new window.TypeError('1 argument required, but only 0 present.');
|
|
188
|
-
}
|
|
189
|
-
const [selector] = args;
|
|
190
|
-
return querySelector(selector, this);
|
|
191
|
-
};
|
|
192
|
-
window.DocumentFragment.prototype.querySelector = function (...args) {
|
|
193
|
-
if (!args.length) {
|
|
194
|
-
throw new window.TypeError('1 argument required, but only 0 present.');
|
|
195
|
-
}
|
|
196
|
-
const [selector] = args;
|
|
197
|
-
return querySelector(selector, this);
|
|
198
|
-
};
|
|
199
|
-
window.Element.prototype.querySelector = function (...args) {
|
|
200
|
-
if (!args.length) {
|
|
201
|
-
throw new window.TypeError('1 argument required, but only 0 present.');
|
|
202
|
-
}
|
|
203
|
-
const [selector] = args;
|
|
204
|
-
return querySelector(selector, this);
|
|
205
|
-
};
|
|
206
|
-
window.Document.prototype.querySelectorAll = function (...args) {
|
|
207
|
-
if (!args.length) {
|
|
208
|
-
throw new window.TypeError('1 argument required, but only 0 present.');
|
|
209
|
-
}
|
|
210
|
-
const [selector] = args;
|
|
211
|
-
return querySelectorAll(selector, this);
|
|
212
|
-
};
|
|
213
|
-
window.DocumentFragment.prototype.querySelectorAll = function (...args) {
|
|
214
|
-
if (!args.length) {
|
|
215
|
-
throw new window.TypeError('1 argument required, but only 0 present.');
|
|
216
|
-
}
|
|
217
|
-
const [selector] = args;
|
|
218
|
-
return querySelectorAll(selector, this);
|
|
219
|
-
};
|
|
220
|
-
window.Element.prototype.querySelectorAll = function (...args) {
|
|
221
|
-
if (!args.length) {
|
|
222
|
-
throw new window.TypeError('1 argument required, but only 0 present.');
|
|
223
|
-
}
|
|
224
|
-
const [selector] = args;
|
|
225
|
-
return querySelectorAll(selector, this);
|
|
226
|
-
};
|
|
227
|
-
}
|
|
228
|
-
});
|
|
229
|
-
```
|
|
230
|
-
|
|
160
|
+
<!--
|
|
231
161
|
### Performance
|
|
232
162
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|matches('.content')|2,619,610 ops/sec ±0.65%|125,248 ops/sec ±0.23%|jsdom is 20.9 times faster than patched-jsdom. patched-jsdom took 0.008msec.|
|
|
236
|
-
|matches('div.container:not(.box)')|1,373,626 ops/sec ±0.82%|73,757 ops/sec ±0.39%|jsdom is 18.6 times faster than patched-jsdom. patched-jsdom took 0.014msec.|
|
|
237
|
-
|matches('.box + .box')|2,268,829 ops/sec ±0.29%|104,000 ops/sec ±0.25%|jsdom is 21.8 times faster than patched-jsdom. patched-jsdom took 0.010msec.|
|
|
238
|
-
|matches('.box ~ .box')|2,197,324 ops/sec ±1.96%|104,061 ops/sec ±1.71%|jsdom is 21.1 times faster than patched-jsdom. patched-jsdom took 0.010msec.|
|
|
239
|
-
|matches('.box > .block')|2,317,373 ops/sec ±1.73%|104,357 ops/sec ±1.55%|jsdom is 22.2 times faster than patched-jsdom. patched-jsdom took 0.010msec.|
|
|
240
|
-
|matches('.box .content')|372,390 ops/sec ±1.11%|59,051 ops/sec ±1.57%|jsdom is 6.3 times faster than patched-jsdom. patched-jsdom took 0.017msec.|
|
|
241
|
-
|matches('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner > .content')|179,843 ops/sec ±1.33%|18,116 ops/sec ±2.72%|jsdom is 9.9 times faster than patched-jsdom. patched-jsdom took 0.055msec.|
|
|
242
|
-
|matches('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner:has(>.content)')|N/A|39,736 ops/sec ±0.60%|jsdom failed. patched-jsdom took 0.025msec.|
|
|
243
|
-
|closest('.content')|1,741,518 ops/sec ±1.78%|70,161 ops/sec ±1.50%|jsdom is 24.8 times faster than patched-jsdom. patched-jsdom took 0.014msec.|
|
|
244
|
-
|closest('div.container:not(.box)')|272,910 ops/sec ±1.50%|34,077 ops/sec ±2.60%|jsdom is 8.0 times faster than patched-jsdom. patched-jsdom took 0.029msec.|
|
|
245
|
-
|closest('.box + .box')|435,648 ops/sec ±1.41%|54,061 ops/sec ±1.55%|jsdom is 8.1 times faster than patched-jsdom. patched-jsdom took 0.018msec.|
|
|
246
|
-
|closest('.box ~ .box')|186,494 ops/sec ±1.66%|34,801 ops/sec ±1.47%|jsdom is 5.4 times faster than patched-jsdom. patched-jsdom took 0.029msec.|
|
|
247
|
-
|closest('.box > .block')|472,464 ops/sec ±1.55%|51,083 ops/sec ±1.44%|jsdom is 9.2 times faster than patched-jsdom. patched-jsdom took 0.020msec.|
|
|
248
|
-
|closest('.box .content')|356,393 ops/sec ±1.48%|42,369 ops/sec ±1.56%|jsdom is 8.4 times faster than patched-jsdom. patched-jsdom took 0.024msec.|
|
|
249
|
-
|closest('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner > .content')|173,085 ops/sec ±1.45%|16,152 ops/sec ±1.51%|jsdom is 10.7 times faster than patched-jsdom. patched-jsdom took 0.062msec.|
|
|
250
|
-
|closest('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner:has(>.content)')|N/A|12,085 ops/sec ±3.06%|jsdom failed. patched-jsdom took 0.083msec.|
|
|
251
|
-
|querySelector('.content')|3,259 ops/sec ±1.17%|61,736 ops/sec ±1.81%|patched-jsdom is 18.9 times faster than jsdom. patched-jsdom took 0.016msec.|
|
|
252
|
-
|querySelector('div.container:not(.box)')|81,006 ops/sec ±1.08%|43,620 ops/sec ±1.80%|jsdom is 1.9 times faster than patched-jsdom. patched-jsdom took 0.023msec.|
|
|
253
|
-
|querySelector('.box + .box')|81,994 ops/sec ±1.38%|58,501 ops/sec ±2.06%|jsdom is 1.4 times faster than patched-jsdom. patched-jsdom took 0.017msec.|
|
|
254
|
-
|querySelector('.box ~ .box')|84,898 ops/sec ±0.19%|29,419 ops/sec ±1.54%|jsdom is 2.9 times faster than patched-jsdom. patched-jsdom took 0.034msec.|
|
|
255
|
-
|querySelector('.box > .block')|1,391 ops/sec ±0.26%|55,995 ops/sec ±1.68%|patched-jsdom is 40.3 times faster than jsdom. patched-jsdom took 0.018msec.|
|
|
256
|
-
|querySelector('.box .content')|676 ops/sec ±1.84%|39,209 ops/sec ±1.53%|patched-jsdom is 58.0 times faster than jsdom. patched-jsdom took 0.026msec.|
|
|
257
|
-
|querySelector('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner > .content')|243 ops/sec ±2.53%|1,002 ops/sec ±1.38%|patched-jsdom is 4.1 times faster than jsdom. patched-jsdom took 0.998msec.|
|
|
258
|
-
|querySelector('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner:has(>.content)')|N/A|795 ops/sec ±1.31%|jsdom failed. patched-jsdom took 1.257msec.|
|
|
259
|
-
|querySelectorAll('.content')|3,005 ops/sec ±1.14%|828 ops/sec ±1.20%|jsdom is 3.6 times faster than patched-jsdom. patched-jsdom took 1.207msec.|
|
|
260
|
-
|querySelectorAll('div.container:not(.box)')|101,993 ops/sec ±1.37%|23,041 ops/sec ±1.67%|jsdom is 4.4 times faster than patched-jsdom. patched-jsdom took 0.043msec.|
|
|
261
|
-
|querySelectorAll('.box + .box')|103,056 ops/sec ±1.41%|29,089 ops/sec ±0.28%|jsdom is 3.5 times faster than patched-jsdom. patched-jsdom took 0.034msec.|
|
|
262
|
-
|querySelectorAll('.box ~ .box')|103,236 ops/sec ±1.39%|11,858 ops/sec ±1.79%|jsdom is 8.7 times faster than patched-jsdom. patched-jsdom took 0.084msec.|
|
|
263
|
-
|querySelectorAll('.box > .block')|1,375 ops/sec ±1.27%|4,280 ops/sec ±1.63%|patched-jsdom is 3.1 times faster than jsdom. patched-jsdom took 0.234msec.|
|
|
264
|
-
|querySelectorAll('.box .content')|669 ops/sec ±1.28%|716 ops/sec ±0.21%|patched-jsdom is 1.1 times faster than jsdom. patched-jsdom took 1.397msec.|
|
|
265
|
-
|querySelectorAll('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner > .content')|279 ops/sec ±0.20%|1,005 ops/sec ±1.43%|patched-jsdom is 3.6 times faster than jsdom. patched-jsdom took 0.995msec.|
|
|
266
|
-
|querySelectorAll('.box:first-child ~ .box:nth-of-type(4n+1) + .box .block.inner:has(>.content)')|N/A|792 ops/sec ±1.42%|jsdom failed. patched-jsdom took 1.262msec.|
|
|
163
|
+
TODO: rewrite benchmark table
|
|
164
|
+
-->
|
|
267
165
|
|
|
268
166
|
|
|
269
167
|
## Acknowledgments
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
var
|
|
1
|
+
var s=Object.defineProperty;var h=Object.getOwnPropertyDescriptor;var u=Object.getOwnPropertyNames;var w=Object.prototype.hasOwnProperty;var a=(r,e)=>{for(var o in e)s(r,o,{get:e[o],enumerable:!0})},y=(r,e,o,l)=>{if(e&&typeof e=="object"||typeof e=="function")for(let c of u(e))!w.call(r,c)&&c!==o&&s(r,c,{get:()=>e[c],enumerable:!(l=h(e,c))||l.enumerable});return r};var f=r=>y(s({},"__esModule",{value:!0}),r);var x={};a(x,{closest:()=>m,matches:()=>i,querySelector:()=>p,querySelectorAll:()=>q});module.exports=f(x);var n=require("./js/matcher.js");/*!
|
|
2
2
|
* DOM Selector - A CSS selector engine.
|
|
3
3
|
* @license MIT
|
|
4
4
|
* @copyright asamuzaK (Kazz)
|
|
5
5
|
* @see {@link https://github.com/asamuzaK/domSelector/blob/main/LICENSE}
|
|
6
|
-
*/const
|
|
6
|
+
*/let t=new n.Matcher;const i=(r,e,o)=>{let l;try{t||(t=new n.Matcher),l=t.matches(e,r,o)}catch(c){throw t=null,c}return l},m=(r,e,o)=>{let l;try{t||(t=new n.Matcher),l=t.closest(e,r,o)}catch(c){throw t=null,c}return l},p=(r,e,o)=>{let l;try{t||(t=new n.Matcher),l=t.querySelector(e,r,o)}catch(c){throw t=null,c}return l},q=(r,e,o)=>{let l;try{t||(t=new n.Matcher),l=t.querySelectorAll(e,r,o)}catch(c){throw t=null,c}return l};0&&(module.exports={closest,matches,querySelector,querySelectorAll});
|
|
7
7
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.js"],
|
|
4
|
-
"sourcesContent": ["/*!\n * DOM Selector - A CSS selector engine.\n * @license MIT\n * @copyright asamuzaK (Kazz)\n * @see {@link https://github.com/asamuzaK/domSelector/blob/main/LICENSE}\n */\n\n/* import */\nimport { Matcher } from './js/matcher.js';\n\n/**\n * matches\n * @param {string} selector - CSS selector\n * @param {object} node - Element node\n * @param {object} [opt] - options\n * @param {boolean} [opt.warn] - console warn e.g. unsupported pseudo-class\n * @returns {boolean} - `true` if matched, `false` otherwise\n */\nexport const matches = (selector, node, opt)
|
|
5
|
-
"mappings": "4ZAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,EAAA,YAAAC,EAAA,kBAAAC,EAAA,qBAAAC,IAAA,eAAAC,EAAAN,GAQA,IAAAO,EAAwB,2BARxB;AAAA;AAAA;AAAA;AAAA;AAAA,
|
|
6
|
-
"names": ["src_exports", "__export", "closest", "matches", "querySelector", "querySelectorAll", "__toCommonJS", "import_matcher", "selector", "node", "opt"]
|
|
4
|
+
"sourcesContent": ["/*!\n * DOM Selector - A CSS selector engine.\n * @license MIT\n * @copyright asamuzaK (Kazz)\n * @see {@link https://github.com/asamuzaK/domSelector/blob/main/LICENSE}\n */\n\n/* import */\nimport { Matcher } from './js/matcher.js';\n\n/* instance */\nlet matcher = new Matcher();\n\n/**\n * matches\n * @param {string} selector - CSS selector\n * @param {object} node - Element node\n * @param {object} [opt] - options\n * @param {boolean} [opt.warn] - console warn e.g. unsupported pseudo-class\n * @returns {boolean} - `true` if matched, `false` otherwise\n */\nexport const matches = (selector, node, opt) => {\n let res;\n try {\n if (!matcher) {\n matcher = new Matcher();\n }\n res = matcher.matches(node, selector, opt);\n } catch (e) {\n matcher = null;\n throw e;\n }\n return res;\n};\n\n/**\n * closest\n * @param {string} selector - CSS selector\n * @param {object} node - Element node\n * @param {object} [opt] - options\n * @param {boolean} [opt.warn] - console warn e.g. unsupported pseudo-class\n * @returns {?object} - matched node\n */\nexport const closest = (selector, node, opt) => {\n let res;\n try {\n if (!matcher) {\n matcher = new Matcher();\n }\n res = matcher.closest(node, selector, opt);\n } catch (e) {\n matcher = null;\n throw e;\n }\n return res;\n};\n\n/**\n * querySelector\n * @param {string} selector - CSS selector\n * @param {object} node - Document, DocumentFragment or Element node\n * @param {object} [opt] - options\n * @param {boolean} [opt.warn] - console warn e.g. unsupported pseudo-class\n * @returns {?object} - matched node\n */\nexport const querySelector = (selector, node, opt) => {\n let res;\n try {\n if (!matcher) {\n matcher = new Matcher();\n }\n res = matcher.querySelector(node, selector, opt);\n } catch (e) {\n matcher = null;\n throw e;\n }\n return res;\n};\n\n/**\n * querySelectorAll\n * NOTE: returns Array, not NodeList\n * @param {string} selector - CSS selector\n * @param {object} node - Document, DocumentFragment or Element node\n * @param {object} [opt] - options\n * @param {boolean} [opt.warn] - console warn e.g. unsupported pseudo-class\n * @returns {Array.<object|undefined>} - array of matched nodes\n */\nexport const querySelectorAll = (selector, node, opt) => {\n let res;\n try {\n if (!matcher) {\n matcher = new Matcher();\n }\n res = matcher.querySelectorAll(node, selector, opt);\n } catch (e) {\n matcher = null;\n throw e;\n }\n return res;\n};\n"],
|
|
5
|
+
"mappings": "4ZAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,EAAA,YAAAC,EAAA,kBAAAC,EAAA,qBAAAC,IAAA,eAAAC,EAAAN,GAQA,IAAAO,EAAwB,2BARxB;AAAA;AAAA;AAAA;AAAA;AAAA,GAWA,IAAIC,EAAU,IAAI,UAUX,MAAML,EAAU,CAACM,EAAUC,EAAMC,IAAQ,CAC9C,IAAIC,EACJ,GAAI,CACGJ,IACHA,EAAU,IAAI,WAEhBI,EAAMJ,EAAQ,QAAQE,EAAMD,EAAUE,CAAG,CAC3C,OAASE,EAAG,CACV,MAAAL,EAAU,KACJK,CACR,CACA,OAAOD,CACT,EAUaV,EAAU,CAACO,EAAUC,EAAMC,IAAQ,CAC9C,IAAIC,EACJ,GAAI,CACGJ,IACHA,EAAU,IAAI,WAEhBI,EAAMJ,EAAQ,QAAQE,EAAMD,EAAUE,CAAG,CAC3C,OAASE,EAAG,CACV,MAAAL,EAAU,KACJK,CACR,CACA,OAAOD,CACT,EAUaR,EAAgB,CAACK,EAAUC,EAAMC,IAAQ,CACpD,IAAIC,EACJ,GAAI,CACGJ,IACHA,EAAU,IAAI,WAEhBI,EAAMJ,EAAQ,cAAcE,EAAMD,EAAUE,CAAG,CACjD,OAASE,EAAG,CACV,MAAAL,EAAU,KACJK,CACR,CACA,OAAOD,CACT,EAWaP,EAAmB,CAACI,EAAUC,EAAMC,IAAQ,CACvD,IAAIC,EACJ,GAAI,CACGJ,IACHA,EAAU,IAAI,WAEhBI,EAAMJ,EAAQ,iBAAiBE,EAAMD,EAAUE,CAAG,CACpD,OAASE,EAAG,CACV,MAAAL,EAAU,KACJK,CACR,CACA,OAAOD,CACT",
|
|
6
|
+
"names": ["src_exports", "__export", "closest", "matches", "querySelector", "querySelectorAll", "__toCommonJS", "import_matcher", "matcher", "selector", "node", "opt", "res", "e"]
|
|
7
7
|
}
|
package/dist/cjs/js/matcher.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var W=Object.create;var R=Object.defineProperty;var z=Object.getOwnPropertyDescriptor;var B=Object.getOwnPropertyNames;var j=Object.getPrototypeOf,V=Object.prototype.hasOwnProperty;var F=(T,s)=>{for(var e in s)R(T,e,{get:s[e],enumerable:!0})},I=(T,s,e,f)=>{if(s&&typeof s=="object"||typeof s=="function")for(let c of B(s))!V.call(T,c)&&c!==e&&R(T,c,{get:()=>s[c],enumerable:!(f=z(s,c))||f.enumerable});return T};var H=(T,s,e)=>(e=T!=null?W(j(T)):{},I(s||!T||!T.__esModule?R(e,"default",{value:T,enumerable:!0}):e,T)),G=T=>I(R({},"__esModule",{value:!0}),T);var Y={};F(Y,{Matcher:()=>q});module.exports=G(Y);var U=H(require("is-potential-custom-element-name"),1),S=require("./dom-util.js"),v=require("./parser.js"),i=require("./constant.js");const C="next",O="prev",A="all",L="first",$="lineal",M="self";class q{#i;#h;#r;#s;#a;#e;#o;#t;#n;#f;#l;#c;constructor(s,e,f={}){const{warn:c}=f;this.#h=new Map([[i.SELECTOR_PSEUDO_ELEMENT,i.BIT_01],[i.SELECTOR_ID,i.BIT_02],[i.SELECTOR_CLASS,i.BIT_04],[i.SELECTOR_TYPE,i.BIT_08],[i.SELECTOR_ATTR,i.BIT_16],[i.SELECTOR_PSEUDO_CLASS,i.BIT_32]]),this.#r=new WeakMap,this.#e=e,[this.#c,this.#s,this.#t,this.#f]=this._setup(e),this.#n=(0,S.isInShadowTree)(e),[this.#i,this.#o]=this._prepare(s),this.#l=!!c}_onError(s){if((s instanceof DOMException||s instanceof this.#c.DOMException)&&s.name===i.NOT_SUPPORTED_ERR)this.#l&&console.warn(s.message);else throw s instanceof DOMException?new this.#c.DOMException(s.message,s.name):s instanceof TypeError?new this.#c.TypeError(s.message):s}_setup(s){let e,f;switch(s?.nodeType){case i.DOCUMENT_NODE:{e=s,f=s;break}case i.DOCUMENT_FRAGMENT_NODE:{e=s.ownerDocument,f=s;break}case i.ELEMENT_NODE:{if(s.ownerDocument.contains(s))e=s.ownerDocument,f=s.ownerDocument;else{let n=s;for(;n&&n.parentNode;)n=n.parentNode;e=n.ownerDocument,f=n}break}default:{let n;throw s?.nodeName?n=`Unexpected node ${s.nodeName}`:n=`Unexpected node ${Object.prototype.toString.call(s).slice(i.TYPE_FROM,i.TYPE_TO)}`,new TypeError(n)}}const c=i.SHOW_DOCUMENT|i.SHOW_DOCUMENT_FRAGMENT|i.SHOW_ELEMENT,l=e.createTreeWalker(f,c);return[e.defaultView,e,f,l]}_sortLeaves(s){const e=[...s];return e.length>1&&e.sort((f,c)=>{const{type:l}=f,{type:b}=c,n=this.#h.get(l),d=this.#h.get(b);let t;return n===d?t=0:n>d?t=1:t=-1,t}),e}_prepare(s){let e;try{e=(0,v.parseSelector)(s)}catch(n){this._onError(n)}const f=(0,v.walkAST)(e),c=[],l=[];let b=0;for(const[...n]of f){const d=[];let t=n.shift();if(t&&t.type!==i.COMBINATOR){const r=new Set;for(;t;){if(t.type===i.COMBINATOR){const[a]=n;if(a.type===i.COMBINATOR){const o=`Invalid combinator ${t.name}${a.name}`;throw new this.#c.DOMException(o,i.SYNTAX_ERR)}d.push({combo:t,leaves:this._sortLeaves(r)}),r.clear()}else t&&r.add(t);if(n.length)t=n.shift();else{d.push({combo:null,leaves:this._sortLeaves(r)}),r.clear();break}}}c.push({branch:d,dir:null,filtered:!1,find:!1}),l[b]=new Set,b++}return[c,l]}_traverse(s={},e=this.#f){let f,c=e.currentNode;if(s.nodeType===i.ELEMENT_NODE&&c===s)f=c;else{if(c!==e.root)for(;c&&!(c===e.root||s.nodeType===i.ELEMENT_NODE&&c===s);)c=e.parentNode();if(s.nodeType===i.ELEMENT_NODE)for(;c;){if(c===s){f=c;break}c=e.nextNode()}else f=c}return f??null}_collectNthChild(s,e){const{a:f,b:c,reverse:l,selector:b}=s,{parentNode:n}=e;let d=new Set,t;if(b&&(this.#r.has(b)?t=this.#r.get(b):(t=(0,v.walkAST)(b),this.#r.set(b,t))),n){const r=i.SHOW_DOCUMENT|i.SHOW_DOCUMENT_FRAGMENT|i.SHOW_ELEMENT,a=this.#s.createTreeWalker(n,r);let o=0,h=a.firstChild();for(;h;)o++,h=a.nextSibling();h=this._traverse(n,a);const m=new Set;if(t)for(h=this._traverse(n,a),h=a.firstChild();h;){let p;for(const g of t)if(p=this._matchLeaves(g,h),!p)break;p&&m.add(h),h=a.nextSibling()}if(f===0){if(c>0&&c<=o){if(m.size){let p=0;for(h=this._traverse(n,a),l?h=a.lastChild():h=a.firstChild();h;){if(m.has(h)){if(p===c-1){d.add(h);break}p++}l?h=a.previousSibling():h=a.nextSibling()}}else if(!b){let p=0;for(h=this._traverse(n,a),l?h=a.lastChild():h=a.firstChild();h;){if(p===c-1){d.add(h);break}l?h=a.previousSibling():h=a.nextSibling(),p++}}}}else{let p=c-1;if(f>0)for(;p<0;)p+=f;if(p>=0&&p<o){let g=0,N=f>0?0:c-1;for(h=this._traverse(n,a),l?h=a.lastChild():h=a.firstChild();h&&(h&&p>=0&&p<o);)m.size?m.has(h)&&(N===p&&(d.add(h),p+=f),f>0?N++:N--):g===p&&(b||d.add(h),p+=f),l?h=a.previousSibling():h=a.nextSibling(),g++}}if(l&&d.size>1){const p=[...d];d=new Set(p.reverse())}}else if(e===this.#t&&this.#t.nodeType===i.ELEMENT_NODE&&f+c===1)if(t){let r;for(const a of t)if(r=this._matchLeaves(a,e),r)break;r&&d.add(e)}else d.add(e);return d}_collectNthOfType(s,e){const{a:f,b:c,reverse:l}=s,{localName:b,parentNode:n,prefix:d}=e;let t=new Set;if(n){const r=i.SHOW_DOCUMENT|i.SHOW_DOCUMENT_FRAGMENT|i.SHOW_ELEMENT,a=this.#s.createTreeWalker(n,r);let o=0,h=a.firstChild();for(;h;)o++,h=a.nextSibling();if(f===0){if(c>0&&c<=o){let m=0;for(h=this._traverse(n,a),l?h=a.lastChild():h=a.firstChild();h;){const{localName:p,prefix:g}=h;if(p===b&&g===d){if(m===c-1){t.add(h);break}m++}l?h=a.previousSibling():h=a.nextSibling()}}}else{let m=c-1;if(f>0)for(;m<0;)m+=f;if(m>=0&&m<o){let p=f>0?0:c-1;for(h=this._traverse(n,a),l?h=a.lastChild():h=a.firstChild();h;){const{localName:g,prefix:N}=h;if(g===b&&N===d){if(p===m&&(t.add(h),m+=f),m<0||m>=o)break;f>0?p++:p--}l?h=a.previousSibling():h=a.nextSibling()}}}if(l&&t.size>1){const m=[...t];t=new Set(m.reverse())}}else e===this.#t&&this.#t.nodeType===i.ELEMENT_NODE&&f+c===1&&t.add(e);return t}_matchAnPlusB(s,e,f){const{nth:{a:c,b:l,name:b},selector:n}=s,d=(0,v.unescapeSelector)(b),t=new Map;d?(d==="even"?(t.set("a",2),t.set("b",0)):d==="odd"&&(t.set("a",2),t.set("b",1)),f.indexOf("last")>-1&&t.set("reverse",!0)):(typeof c=="string"&&/-?\d+/.test(c)?t.set("a",c*1):t.set("a",0),typeof l=="string"&&/-?\d+/.test(l)?t.set("b",l*1):t.set("b",0),f.indexOf("last")>-1&&t.set("reverse",!0));let r=new Set;if(t.has("a")&&t.has("b")){if(/^nth-(?:last-)?child$/.test(f)){n&&t.set("selector",n);const a=Object.fromEntries(t),o=this._collectNthChild(a,e);o.size&&(r=o)}else if(/^nth-(?:last-)?of-type$/.test(f)){const a=Object.fromEntries(t),o=this._collectNthOfType(a,e);o.size&&(r=o)}}return r}_matchPseudoElementSelector(s,e={}){const{forgive:f}=e;switch(s){case"after":case"backdrop":case"before":case"cue":case"cue-region":case"first-letter":case"first-line":case"file-selector-button":case"marker":case"placeholder":case"selection":case"target-text":{if(this.#l){const c=`Unsupported pseudo-element ::${s}`;throw new DOMException(c,i.NOT_SUPPORTED_ERR)}break}case"part":case"slotted":{if(this.#l){const c=`Unsupported pseudo-element ::${s}()`;throw new DOMException(c,i.NOT_SUPPORTED_ERR)}break}default:if(s.startsWith("-webkit-")){if(this.#l){const c=`Unsupported pseudo-element ::${s}`;throw new DOMException(c,i.NOT_SUPPORTED_ERR)}}else if(!f){const c=`Unknown pseudo-element ::${s}`;throw new DOMException(c,i.SYNTAX_ERR)}}}_matchDirectionPseudoClass(s,e){const f=(0,v.unescapeSelector)(s.name),c=(0,S.getDirectionality)(e);let l;return f===c&&(l=e),l??null}_matchLanguagePseudoClass(s,e){const f=(0,v.unescapeSelector)(s.name);let c;if(f==="*")if(e.hasAttribute("lang"))e.getAttribute("lang")&&(c=e);else{let l=e.parentNode;for(;l&&l.nodeType===i.ELEMENT_NODE;){if(l.hasAttribute("lang")){l.getAttribute("lang")&&(c=e);break}l=l.parentNode}}else if(f){const l=`(?:-${i.ALPHA_NUM})*`;if(new RegExp(`^(?:\\*-)?${i.ALPHA_NUM}${l}$`,"i").test(f)){let n;if(f.indexOf("-")>-1){const[d,t,...r]=f.split("-");let a;d==="*"?a=`${i.ALPHA_NUM}${l}`:a=`${d}${l}`;const o=`-${t}${l}`,h=r.length;let m="";if(h)for(let p=0;p<h;p++)m+=`-${r[p]}${l}`;n=new RegExp(`^${a}${o}${m}$`,"i")}else n=new RegExp(`^${f}${l}$`,"i");if(e.hasAttribute("lang"))n.test(e.getAttribute("lang"))&&(c=e);else{let d=e.parentNode;for(;d&&d.nodeType===i.ELEMENT_NODE;){if(d.hasAttribute("lang")){const t=d.getAttribute("lang");n.test(t)&&(c=e);break}d=d.parentNode}}}}return c??null}_matchHasPseudoFunc(s,e){let f;if(Array.isArray(s)&&s.length){const[c]=s,{type:l}=c;let b;l===i.COMBINATOR?b=s.shift():b={name:" ",type:i.COMBINATOR};const n=[];for(;s.length;){const[r]=s,{type:a}=r;if(a===i.COMBINATOR)break;n.push(s.shift())}const d={combo:b,leaves:n},t=this._matchCombinator(d,e,{dir:C});if(t.size)if(s.length){for(const r of t)if(f=this._matchHasPseudoFunc(Object.assign([],s),r),f)break}else f=!0}return!!f}_matchLogicalPseudoFunc(s,e){const{astName:f="",branches:c=[],selector:l="",twigBranches:b=[]}=s;let n;if(f==="has")if(l.includes(":has("))n=null;else{let d;for(const t of c)if(d=this._matchHasPseudoFunc(Object.assign([],t),e),d)break;d&&(n=e)}else{const d=/^(?:is|where)$/.test(f),t=b.length;let r;for(let a=0;a<t;a++){const o=b[a],h=o.length-1,{leaves:m}=o[h];if(r=this._matchLeaves(m,e,{forgive:d}),r&&h>0){let p=new Set([e]);for(let g=h-1;g>=0;g--){const N=o[g],u=[];for(const w of p){const k=this._matchCombinator(N,w,{forgive:d,dir:O});k.size&&u.push(...k)}if(u.length)g===0?r=!0:p=new Set(u);else{r=!1;break}}}if(r)break}f==="not"?r||(n=e):r&&(n=e)}return n??null}_matchPseudoClassSelector(s,e,f={}){const{children:c}=s,{localName:l,parentNode:b}=e,{forgive:n}=f,d=(0,v.unescapeSelector)(s.name);let t=new Set;if(i.REG_LOGICAL_PSEUDO.test(d)){let r;if(this.#r.has(s))r=this.#r.get(s);else{const o=(0,v.walkAST)(s),h=[],m=[];for(const[...p]of o){for(const w of p){const k=(0,v.generateCSS)(w);h.push(k)}const g=[],N=new Set;let u=p.shift();for(;u;)if(u.type===i.COMBINATOR?(g.push({combo:u,leaves:[...N]}),N.clear()):u&&N.add(u),p.length)u=p.shift();else{g.push({combo:null,leaves:[...N]}),N.clear();break}m.push(g)}r={astName:d,branches:o,twigBranches:m,selector:h.join(",")},this.#r.set(s,r)}const a=this._matchLogicalPseudoFunc(r,e);a&&t.add(a)}else if(Array.isArray(c)){const[r]=c;if(/^nth-(?:last-)?(?:child|of-type)$/.test(d)){const a=this._matchAnPlusB(r,e,d);a.size&&(t=a)}else if(d==="dir"){const a=this._matchDirectionPseudoClass(r,e);a&&t.add(a)}else if(d==="lang"){const a=this._matchLanguagePseudoClass(r,e);a&&t.add(a)}else switch(d){case"current":case"nth-col":case"nth-last-col":{if(this.#l){const a=`Unsupported pseudo-class :${d}()`;throw new DOMException(a,i.NOT_SUPPORTED_ERR)}break}case"host":case"host-context":break;default:if(!n){const a=`Unknown pseudo-class :${d}()`;throw new DOMException(a,i.SYNTAX_ERR)}}}else{const r=/^a(?:rea)?$/,a=/^(?:(?:fieldse|inpu|selec)t|button|opt(?:group|ion)|textarea)$/,o=/^(?:(?:inpu|selec)t|button|form|textarea)$/,h=/^d(?:etails|ialog)$/,m=/^(?:checkbox|radio)$/,p=/^(?:date(?:time-local)?|month|time|week)$/,g=/(?:(?:rang|tim)e|date(?:time-local)?|month|number|week)$/,N=/^(?:(?:emai|te|ur)l|number|password|search|text)$/;switch(d){case"any-link":case"link":{r.test(l)&&e.hasAttribute("href")&&t.add(e);break}case"local-link":{if(r.test(l)&&e.hasAttribute("href")){const{href:u,origin:w,pathname:k}=new URL(this.#s.URL),y=new URL(e.getAttribute("href"),u);y.origin===w&&y.pathname===k&&t.add(e)}break}case"visited":break;case"target":{const{hash:u}=new URL(this.#s.URL);e.id&&u===`#${e.id}`&&this.#s.contains(e)&&t.add(e);break}case"target-within":{const{hash:u}=new URL(this.#s.URL);if(u){const w=u.replace(/^#/,"");let k=this.#s.getElementById(w);for(;k;){if(k===e){t.add(e);break}k=k.parentNode}}break}case"scope":{this.#e.nodeType===i.ELEMENT_NODE?e===this.#e&&t.add(e):e===this.#s.documentElement&&t.add(e);break}case"focus":{e===this.#s.activeElement&&t.add(e);break}case"focus-within":{let u=this.#s.activeElement;for(;u;){if(u===e){t.add(e);break}u=u.parentNode}break}case"open":{h.test(l)&&e.hasAttribute("open")&&t.add(e);break}case"closed":{h.test(l)&&!e.hasAttribute("open")&&t.add(e);break}case"disabled":{if(a.test(l)||(0,U.default)(l))if(e.disabled||e.hasAttribute("disabled"))t.add(e);else{let u=b;for(;u&&u.localName!=="fieldset";)u=u.parentNode;u&&b.localName!=="legend"&&u.hasAttribute("disabled")&&t.add(e)}break}case"enabled":{(a.test(l)||(0,U.default)(l))&&!(e.disabled&&e.hasAttribute("disabled"))&&t.add(e);break}case"read-only":{switch(l){case"textarea":{(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&t.add(e);break}case"input":{(!e.type||p.test(e.type)||N.test(e.type))&&(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&t.add(e);break}default:(0,S.isContentEditable)(e)||t.add(e)}break}case"read-write":{switch(l){case"textarea":{e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled")||t.add(e);break}case"input":{(!e.type||p.test(e.type)||N.test(e.type))&&!(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&t.add(e);break}default:(0,S.isContentEditable)(e)&&t.add(e)}break}case"placeholder-shown":{let u;l==="textarea"?u=e:l==="input"&&(e.hasAttribute("type")?N.test(e.getAttribute("type"))&&(u=e):u=e),u&&e.value===""&&e.hasAttribute("placeholder")&&e.getAttribute("placeholder").trim().length&&t.add(e);break}case"checked":{(e.checked&&l==="input"&&e.hasAttribute("type")&&m.test(e.getAttribute("type"))||e.selected&&l==="option")&&t.add(e);break}case"indeterminate":{if(e.indeterminate&&l==="input"&&e.type==="checkbox"||l==="progress"&&!e.hasAttribute("value"))t.add(e);else if(l==="input"&&e.type==="radio"&&!e.hasAttribute("checked")){const u=e.name;let w=e.parentNode;for(;w&&w.localName!=="form";)w=w.parentNode;w||(w=this.#s.documentElement);let k;const y=[].slice.call(w.getElementsByTagName("input"));for(const _ of y)if(_.getAttribute("type")==="radio"&&(u?_.getAttribute("name")===u&&(k=!!_.checked):_.hasAttribute("name")||(k=!!_.checked),k))break;k||t.add(e)}break}case"default":{const u=/^(?:button|reset)$/,w=/^(?:image|submit)$/;if(l==="button"&&!(e.hasAttribute("type")&&u.test(e.getAttribute("type")))||l==="input"&&e.hasAttribute("type")&&w.test(e.getAttribute("type"))){let k=e.parentNode;for(;k&&k.localName!=="form";)k=k.parentNode;if(k){const y=this.#s.createTreeWalker(k,i.SHOW_ELEMENT);let _=y.firstChild();for(;_;){const x=_.localName;let E;if(x==="button"?E=!(_.hasAttribute("type")&&u.test(_.getAttribute("type"))):x==="input"&&(E=_.hasAttribute("type")&&w.test(_.getAttribute("type"))),E){_===e&&t.add(e);break}_=y.nextNode()}}}else if(l==="input"&&e.hasAttribute("type")&&m.test(e.getAttribute("type"))&&(e.checked||e.hasAttribute("checked")))t.add(e);else if(l==="option"){let k=!1,y=b;for(;y&&y.localName!=="datalist";){if(y.localName==="select"){(y.multiple||y.hasAttribute("multiple"))&&(k=!0);break}y=y.parentNode}if(k)(e.selected||e.hasAttribute("selected"))&&t.add(e);else{const _=new Set,x=this.#s.createTreeWalker(b,i.SHOW_ELEMENT);let E=x.firstChild();for(;E;){if(E.selected||E.hasAttribute("selected")){_.add(E);break}E=x.nextSibling()}_.size&&_.has(e)&&t.add(e)}}break}case"valid":{if(o.test(l))e.checkValidity()&&t.add(e);else if(l==="fieldset"){let u;const w=this.#s.createTreeWalker(e,i.SHOW_ELEMENT);let k=w.firstChild();for(;k&&!(o.test(k.localName)&&(u=k.checkValidity(),!u));)k=w.nextNode();u&&t.add(e)}break}case"invalid":{if(o.test(l))e.checkValidity()||t.add(e);else if(l==="fieldset"){let u;const w=this.#s.createTreeWalker(e,i.SHOW_ELEMENT);let k=w.firstChild();for(;k&&!(o.test(k.localName)&&(u=k.checkValidity(),!u));)k=w.nextNode();u||t.add(e)}break}case"in-range":{l==="input"&&!(e.readonly||e.hasAttribute("readonly"))&&!(e.disabled||e.hasAttribute("disabled"))&&e.hasAttribute("type")&&g.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":{l==="input"&&!(e.readonly||e.hasAttribute("readonly"))&&!(e.disabled||e.hasAttribute("disabled"))&&e.hasAttribute("type")&&g.test(e.getAttribute("type"))&&(e.validity.rangeUnderflow||e.validity.rangeOverflow)&&t.add(e);break}case"required":{let u;if(/^(?:select|textarea)$/.test(l))u=e;else if(l==="input")if(e.hasAttribute("type")){const w=e.getAttribute("type");(w==="file"||m.test(w)||p.test(w)||N.test(w))&&(u=e)}else u=e;u&&(e.required||e.hasAttribute("required"))&&t.add(e);break}case"optional":{let u;if(/^(?:select|textarea)$/.test(l))u=e;else if(l==="input")if(e.hasAttribute("type")){const w=e.getAttribute("type");(w==="file"||m.test(w)||p.test(w)||N.test(w))&&(u=e)}else u=e;u&&!(e.required||e.hasAttribute("required"))&&t.add(e);break}case"root":{e===this.#s.documentElement&&t.add(e);break}case"empty":{if(e.hasChildNodes()){let u;const w=this.#s.createTreeWalker(e,i.SHOW_ALL);let k=w.firstChild();for(;k&&(u=k.nodeType!==i.ELEMENT_NODE&&k.nodeType!==i.TEXT_NODE,!!u);)k=w.nextSibling();u&&t.add(e)}else t.add(e);break}case"first-child":{(b&&e===b.firstElementChild||e===this.#t&&this.#t.nodeType===i.ELEMENT_NODE)&&t.add(e);break}case"last-child":{(b&&e===b.lastElementChild||e===this.#t&&this.#t.nodeType===i.ELEMENT_NODE)&&t.add(e);break}case"only-child":{(b&&e===b.firstElementChild&&e===b.lastElementChild||e===this.#t&&this.#t.nodeType===i.ELEMENT_NODE)&&t.add(e);break}case"first-of-type":{if(b){const[u]=this._collectNthOfType({a:0,b:1},e);u&&t.add(u)}else e===this.#t&&this.#t.nodeType===i.ELEMENT_NODE&&t.add(e);break}case"last-of-type":{if(b){const[u]=this._collectNthOfType({a:0,b:1,reverse:!0},e);u&&t.add(u)}else e===this.#t&&this.#t.nodeType===i.ELEMENT_NODE&&t.add(e);break}case"only-of-type":{if(b){const[u]=this._collectNthOfType({a:0,b:1},e);if(u===e){const[w]=this._collectNthOfType({a:0,b:1,reverse:!0},e);w===e&&t.add(e)}}else e===this.#t&&this.#t.nodeType===i.ELEMENT_NODE&&t.add(e);break}case"host":case"host-context":break;case"after":case"before":case"first-letter":case"first-line":{if(this.#l){const u=`Unsupported pseudo-element ::${d}`;throw new DOMException(u,i.NOT_SUPPORTED_ERR)}break}case"active":case"autofill":case"blank":case"buffering":case"current":case"defined":case"focus-visible":case"fullscreen":case"future":case"hover":case"modal":case"muted":case"past":case"paused":case"picture-in-picture":case"playing":case"seeking":case"stalled":case"user-invalid":case"user-valid":case"volume-locked":case"-webkit-autofill":{if(this.#l){const u=`Unsupported pseudo-class :${d}`;throw new DOMException(u,i.NOT_SUPPORTED_ERR)}break}default:if(d.startsWith("-webkit-")){if(this.#l){const u=`Unsupported pseudo-class :${d}`;throw new DOMException(u,i.NOT_SUPPORTED_ERR)}}else if(!n){const u=`Unknown pseudo-class :${d}`;throw new DOMException(u,i.SYNTAX_ERR)}}}return t}_matchAttributeSelector(s,e){const{flags:f,matcher:c,name:l,value:b}=s;if(typeof f=="string"&&!/^[is]$/i.test(f)){const r=`Invalid selector ${(0,v.generateCSS)(s)}`;throw new DOMException(r,i.SYNTAX_ERR)}const{attributes:n}=e;let d;if(n&&n.length){let t;this.#s.contentType==="text/html"?typeof f=="string"&&/^s$/i.test(f)?t=!1:t=!0:typeof f=="string"&&/^i$/i.test(f)?t=!0:t=!1;let r=(0,v.unescapeSelector)(l.name);t&&(r=r.toLowerCase());const a=new Set;if(r.indexOf("|")>-1){const{prefix:o,tagName:h}=(0,S.selectorToNodeProps)(r);for(let{name:m,value:p}of n)switch(t&&(m=m.toLowerCase(),p=p.toLowerCase()),o){case"":{h===m&&a.add(p);break}case"*":{m.indexOf(":")>-1?m.endsWith(`:${h}`)&&a.add(p):h===m&&a.add(p);break}default:if(m.indexOf(":")>-1){const[g,N]=m.split(":");o===g&&h===N&&(0,S.isNamespaceDeclared)(o,e)&&a.add(p)}}}else for(let{name:o,value:h}of n)if(t&&(o=o.toLowerCase(),h=h.toLowerCase()),o.indexOf(":")>-1){const[m,p]=o.split(":");if(m==="xml"&&p==="lang")continue;r===p&&a.add(h)}else r===o&&a.add(h);if(a.size){const{name:o,value:h}=b||{};let m;switch(o?t?m=o.toLowerCase():m=o:h?t?m=h.toLowerCase():m=h:h===""&&(m=h),c){case"=":{typeof m=="string"&&a.has(m)&&(d=e);break}case"~=":{if(m&&typeof m=="string"){for(const p of a)if(new Set(p.split(/\s+/)).has(m)){d=e;break}}break}case"|=":{if(m&&typeof m=="string"){let p;for(const g of a)if(g===m||g.startsWith(`${m}-`)){p=g;break}p&&(d=e)}break}case"^=":{if(m&&typeof m=="string"){let p;for(const g of a)if(g.startsWith(`${m}`)){p=g;break}p&&(d=e)}break}case"$=":{if(m&&typeof m=="string"){let p;for(const g of a)if(g.endsWith(`${m}`)){p=g;break}p&&(d=e)}break}case"*=":{if(m&&typeof m=="string"){let p;for(const g of a)if(g.includes(`${m}`)){p=g;break}p&&(d=e)}break}case null:default:d=e}}}return d??null}_matchClassSelector(s,e){const f=(0,v.unescapeSelector)(s.name);let c;return e.classList.contains(f)&&(c=e),c??null}_matchIDSelector(s,e){const f=(0,v.unescapeSelector)(s.name),{id:c}=e;let l;return f===c&&(l=e),l??null}_matchTypeSelector(s,e,f={}){const c=(0,v.unescapeSelector)(s.name),{localName:l,prefix:b}=e,{forgive:n}=f;let{prefix:d,tagName:t}=(0,S.selectorToNodeProps)(c,e);this.#s.contentType==="text/html"&&(d=d.toLowerCase(),t=t.toLowerCase());let r,a;l.indexOf(":")>-1?[r,a]=l.split(":"):(r=b||"",a=l);let o;if(d===""&&r==="")e.namespaceURI===null&&(t==="*"||t===a)&&(o=e);else if(d==="*")(t==="*"||t===a)&&(o=e);else if(d===r){if((0,S.isNamespaceDeclared)(d,e))(t==="*"||t===a)&&(o=e);else if(!n){const h=`Undeclared namespace ${d}`;throw new DOMException(h,i.SYNTAX_ERR)}}else if(d&&!n&&!(0,S.isNamespaceDeclared)(d,e)){const h=`Undeclared namespace ${d}`;throw new DOMException(h,i.SYNTAX_ERR)}return o??null}_matchShadowHostPseudoClass(s,e){const{children:f}=s,c=(0,v.unescapeSelector)(s.name);let l;if(Array.isArray(f)){const[b]=(0,v.walkAST)(f[0]),[...n]=b,{host:d}=e;if(c==="host"){let t;for(const r of n){const{type:a}=r;if(a===i.COMBINATOR){const h=`Invalid selector ${(0,v.generateCSS)(s)}`;throw new DOMException(h,i.SYNTAX_ERR)}if(t=this._matchSelector(r,d).has(d),!t)break}t&&(l=e)}else if(c==="host-context"){let t,r=d;for(;r;){for(const a of n){const{type:o}=a;if(o===i.COMBINATOR){const m=`Invalid selector ${(0,v.generateCSS)(s)}`;throw new DOMException(m,i.SYNTAX_ERR)}if(t=this._matchSelector(a,r).has(r),!t)break}if(t)break;r=r.parentNode}t&&(l=e)}}else if(c==="host")l=e;else{const b=`Invalid selector :${c}`;throw new DOMException(b,i.SYNTAX_ERR)}return l??null}_matchSelector(s,e,f){const{type:c}=s,l=(0,v.unescapeSelector)(s.name);let b=new Set;if(e.nodeType===i.ELEMENT_NODE)switch(c){case i.SELECTOR_ATTR:{const n=this._matchAttributeSelector(s,e);n&&b.add(n);break}case i.SELECTOR_CLASS:{const n=this._matchClassSelector(s,e);n&&b.add(n);break}case i.SELECTOR_ID:{const n=this._matchIDSelector(s,e);n&&b.add(n);break}case i.SELECTOR_PSEUDO_CLASS:{const n=this._matchPseudoClassSelector(s,e,f);n.size&&(b=n);break}case i.SELECTOR_PSEUDO_ELEMENT:{this._matchPseudoElementSelector(l,f);break}case i.SELECTOR_TYPE:default:{const n=this._matchTypeSelector(s,e,f);n&&b.add(n)}}else if(this.#n&&c===i.SELECTOR_PSEUDO_CLASS&&e.nodeType===i.DOCUMENT_FRAGMENT_NODE){if(l!=="has"&&i.REG_LOGICAL_PSEUDO.test(l)){const n=this._matchPseudoClassSelector(s,e,f);n.size&&(b=n)}else if(i.REG_SHADOW_HOST.test(l)){const n=this._matchShadowHostPseudoClass(s,e);n&&b.add(n)}}return b}_matchLeaves(s,e,f){let c;for(const l of s)if(c=this._matchSelector(l,e,f).has(e),!c)break;return!!c}_findDescendantNodes(s,e){const[f,...c]=s,{type:l}=f,b=(0,v.unescapeSelector)(f.name),n=c.length>0;let d=new Set,t=!1;if(this.#n)t=!0;else switch(l){case i.SELECTOR_ID:{if(this.#t.nodeType===i.ELEMENT_NODE)t=!0;else{const r=this.#t.getElementById(b);r&&r!==e&&e.contains(r)&&(n?this._matchLeaves(c,r)&&d.add(r):d.add(r))}break}case i.SELECTOR_CLASS:{const r=[].slice.call(e.getElementsByClassName(b));if(r.length)if(n)for(const a of r)this._matchLeaves(c,a)&&d.add(a);else d=new Set(r);break}case i.SELECTOR_TYPE:{if(this.#s.contentType==="text/html"&&!/[*|]/.test(b)){const r=[].slice.call(e.getElementsByTagName(b));if(r.length)if(n)for(const a of r)this._matchLeaves(c,a)&&d.add(a);else d=new Set(r)}else t=!0;break}case i.SELECTOR_PSEUDO_ELEMENT:{this._matchPseudoElementSelector(b);break}default:t=!0}return{nodes:d,pending:t}}_matchCombinator(s,e,f={}){const{combo:c,leaves:l}=s,{name:b}=c,{dir:n,forgive:d}=f;let t=new Set;if(n===C)switch(b){case"+":{const r=e.nextElementSibling;r&&this._matchLeaves(l,r,{forgive:d})&&t.add(r);break}case"~":{const{parentNode:r}=e;if(r){const a=this.#s.createTreeWalker(r,i.SHOW_ELEMENT);let o=this._traverse(e,a);for(o===e&&(o=a.nextSibling());o;)this._matchLeaves(l,o,{forgive:d})&&t.add(o),o=a.nextSibling()}break}case">":{const r=this.#s.createTreeWalker(e,i.SHOW_ELEMENT);let a=r.firstChild();for(;a;)this._matchLeaves(l,a,{forgive:d})&&t.add(a),a=r.nextSibling();break}case" ":default:{const{nodes:r,pending:a}=this._findDescendantNodes(l,e);if(r.size)t=r;else if(a){const o=this.#s.createTreeWalker(e,i.SHOW_ELEMENT);let h=o.nextNode();for(;h;)this._matchLeaves(l,h,{forgive:d})&&t.add(h),h=o.nextNode()}}}else switch(b){case"+":{const r=e.previousElementSibling;r&&this._matchLeaves(l,r,{forgive:d})&&t.add(r);break}case"~":{const r=this.#s.createTreeWalker(e.parentNode,i.SHOW_ELEMENT);let a=r.firstChild();for(;a&&a!==e;)this._matchLeaves(l,a,{forgive:d})&&t.add(a),a=r.nextSibling();break}case">":{const r=e.parentNode;r&&this._matchLeaves(l,r,{forgive:d})&&t.add(r);break}case" ":default:{const r=[];let a=e.parentNode;for(;a;)this._matchLeaves(l,a,{forgive:d})&&r.push(a),a=a.parentNode;r.length&&(t=new Set(r.reverse()))}}return t}_findNode(s,e={}){let{node:f,walker:c}=e;c||(c=this.#f);let l,b=this._traverse(f,c);if(b)for((b.nodeType!==i.ELEMENT_NODE||b===f&&b!==this.#t)&&(b=c.nextNode());b;){let n;if(this.#e.nodeType===i.ELEMENT_NODE?b===this.#e?n=!0:n=this.#e.contains(b):n=!0,n&&this._matchLeaves(s,b)){l=b;break}b=c.nextNode()}return l??null}_findEntryNodes(s,e){const{leaves:f}=s,[c,...l]=f,{type:b}=c,n=(0,v.unescapeSelector)(c.name),d=l.length>0;let t=new Set,r=!1,a=!1;switch(b){case i.SELECTOR_ID:{if(e===M)this._matchLeaves(f,this.#e)&&(t.add(this.#e),r=!0);else if(e===$){let o=this.#e;for(;o;)this._matchLeaves(f,o)&&(t.add(o),r=!0),o=o.parentNode}else if(e===A||this.#t.nodeType===i.ELEMENT_NODE)a=!0;else{const o=this.#t.getElementById(n);o&&(t.add(o),r=!0)}break}case i.SELECTOR_CLASS:{if(e===M)this.#e.nodeType===i.ELEMENT_NODE&&this.#e.classList.contains(n)&&t.add(this.#e);else if(e===$){let o=this.#e;for(;o&&o.nodeType===i.ELEMENT_NODE;)o.classList.contains(n)&&t.add(o),o=o.parentNode}else if(e===L){const o=this._findNode(f,{node:this.#e,walker:this.#a});if(o){t.add(o),r=!0;break}}else if(this.#t.nodeType===i.DOCUMENT_FRAGMENT_NODE||this.#t.nodeType===i.ELEMENT_NODE)a=!0;else{const o=[].slice.call(this.#t.getElementsByClassName(n));if(this.#e.nodeType===i.ELEMENT_NODE)for(const h of o)(h===this.#e||(0,S.isInclusive)(h,this.#e))&&t.add(h);else o.length&&(t=new Set(o))}break}case i.SELECTOR_TYPE:{if(e===M)this.#e.nodeType===i.ELEMENT_NODE&&this._matchLeaves(f,this.#e)&&(t.add(this.#e),r=!0);else if(e===$){let o=this.#e;for(;o&&o.nodeType===i.ELEMENT_NODE;)this._matchLeaves(f,o)&&(t.add(o),r=!0),o=o.parentNode}else if(e===L){const o=this._findNode(f,{node:this.#e,walker:this.#a});if(o){t.add(o),r=!0;break}}else if(this.#s.contentType!=="text/html"||/[*|]/.test(n)||this.#t.nodeType===i.DOCUMENT_FRAGMENT_NODE||this.#t.nodeType===i.ELEMENT_NODE)a=!0;else{const o=[].slice.call(this.#t.getElementsByTagName(n));if(this.#e.nodeType===i.ELEMENT_NODE)for(const h of o)(h===this.#e||(0,S.isInclusive)(h,this.#e))&&t.add(h);else o.length&&(t=new Set(o))}break}case i.SELECTOR_PSEUDO_ELEMENT:{this._matchPseudoElementSelector(n);break}default:if(e!==$&&i.REG_SHADOW_HOST.test(n)){if(this.#n&&this.#e.nodeType===i.DOCUMENT_FRAGMENT_NODE){const o=this._matchShadowHostPseudoClass(c,this.#e);o&&t.add(o)}}else if(e===M)this._matchLeaves(f,this.#e)&&(t.add(this.#e),r=!0);else if(e===$){let o=this.#e;for(;o;)this._matchLeaves(f,o)&&(t.add(o),r=!0),o=o.parentNode}else if(e===L){const o=this._findNode(f,{node:this.#e,walker:this.#a});if(o){t.add(o),r=!0;break}}else a=!0}return{compound:d,filtered:r,nodes:t,pending:a}}_getEntryTwig(s,e){const f=s.length,c=f>1,l=s[0];let b,n;if(c){const{combo:d,leaves:[{type:t}]}=l,r=s[f-1],{leaves:[{type:a}]}=r;if(a===i.SELECTOR_PSEUDO_ELEMENT||a===i.SELECTOR_ID)b=O,n=r;else if(t===i.SELECTOR_PSEUDO_ELEMENT||t===i.SELECTOR_ID)b=C,n=l;else if(e===A&&f===2){const{name:o}=d;/^[+~]$/.test(o)?(b=O,n=r):(b=C,n=l)}else if(e===A)b=C,n=l;else{let o;for(const{combo:h,leaves:[m]}of s){const{type:p}=m,g=(0,v.unescapeSelector)(m.name);if(p===i.SELECTOR_PSEUDO_CLASS&&g==="dir"){o=!1;break}if(h){const{name:N}=h;/^[+~]$/.test(N)&&(o=!0)}}o?(b=C,n=l):(b=O,n=r)}}else b=O,n=l;return{complex:c,dir:b,twig:n}}_collectNodes(s){const e=this.#i.values();if(s===A||s===L){const f=new Set;let c=0;for(const{branch:l}of e){const{dir:b,twig:n}=this._getEntryTwig(l,s),{compound:d,filtered:t,nodes:r,pending:a}=this._findEntryNodes(n,s);r.size?(this.#i[c].find=!0,this.#o[c]=r):a&&f.add(new Map([["index",c],["twig",n]])),this.#i[c].dir=b,this.#i[c].filtered=t||!d,c++}if(f.size){let l,b;this.#e!==this.#t&&this.#e.nodeType===i.ELEMENT_NODE?(l=this.#e,b=this.#a):(l=this.#t,b=this.#f);let n=this._traverse(l,b);for(;n;){let d=!1;if(this.#e.nodeType===i.ELEMENT_NODE?n===this.#e?d=!0:d=this.#e.contains(n):d=!0,d)for(const t of f){const{leaves:r}=t.get("twig");if(this._matchLeaves(r,n)){const o=t.get("index");this.#i[o].filtered=!0,this.#i[o].find=!0,this.#o[o].add(n)}}n=b.nextNode()}}}else{let f=0;for(const{branch:c}of e){const l=c[c.length-1],{compound:b,filtered:n,nodes:d}=this._findEntryNodes(l,s);d.size&&(this.#i[f].find=!0,this.#o[f]=d),this.#i[f].dir=O,this.#i[f].filtered=n||!b,f++}}return[this.#i,this.#o]}_sortNodes(s){const e=[...s];return e.length>1&&e.sort((f,c)=>{let l;return(0,S.isPreceding)(c,f)?l=1:l=-1,l}),e}_matchNodes(s){const[...e]=this.#i,f=e.length;let c=new Set;for(let l=0;l<f;l++){const{branch:b,dir:n,filtered:d,find:t}=e[l],r=b.length;if(r&&t){const a=this.#o[l],o=r-1;if(o===0){const{leaves:[,...h]}=b[0];if((s===A||s===L)&&this.#e.nodeType===i.ELEMENT_NODE){for(const m of a)if((d||this._matchLeaves(h,m))&&m!==this.#e&&this.#e.contains(m)&&(c.add(m),s!==A))break}else if(h.length){for(const m of a)if((d||this._matchLeaves(h,m))&&(c.add(m),s!==A))break}else if(s===A){const m=[...c];c=new Set([...m,...a])}else{const[m]=[...a];c.add(m)}}else if(n===C){let{combo:h,leaves:m}=b[0];const[,...p]=m;let g;for(const N of a){if(d||this._matchLeaves(p,N)){let w=new Set([N]);for(let k=1;k<r;k++){const{combo:y,leaves:_}=b[k],x=[];for(const E of w){const P={combo:h,leaves:_},D=this._matchCombinator(P,E,{dir:n});D.size&&x.push(...D)}if(x.length)if(k===o){if(s===A){const E=[...c];c=new Set([...E,...x])}else{const[E]=this._sortNodes(x);c.add(E)}g=!0}else h=y,w=new Set(x),g=!1;else{g=!1;break}}}else g=!1;if(g&&s!==A)break}if(!g&&s===L){const[N]=[...a];let u=this._findNode(m,{node:N,walker:this.#a});for(;u;){let w=new Set([u]);for(let k=1;k<r;k++){const{combo:y,leaves:_}=b[k],x=[];for(const E of w){const P={combo:h,leaves:_},D=this._matchCombinator(P,E,{dir:n});D.size&&x.push(...D)}if(x.length)if(k===o){const[E]=this._sortNodes(x);c.add(E),g=!0}else h=y,w=new Set(x),g=!1;else{g=!1;break}}if(g)break;u=this._findNode(m,{node:u,walker:this.#a}),w=new Set([u])}}}else{const{leaves:h}=b[o],[,...m]=h;let p;for(const g of a){if(d||this._matchLeaves(m,g)){let u=new Set([g]);for(let w=o-1;w>=0;w--){const k=b[w],y=[];for(const _ of u){const x=this._matchCombinator(k,_,{dir:n});x.size&&y.push(...x)}if(y.length)w===0?(c.add(g),p=!0):(u=new Set(y),p=!1);else{p=!1;break}}}if(p&&s!==A)break}if(!p&&s===L){const[g]=[...a];let N=this._findNode(h,{node:g,walker:this.#a});for(;N;){let u=new Set([N]);for(let w=o-1;w>=0;w--){const k=b[w],y=[];for(const _ of u){const x=this._matchCombinator(k,_,{dir:n});x.size&&y.push(...x)}if(y.length)w===0?(c.add(N),p=!0):(u=new Set(y),p=!1);else{p=!1;break}}if(p)break;N=this._findNode(h,{node:N,walker:this.#a}),u=new Set([N])}}}}}return c}_find(s){return(s===A||s===L)&&(this.#a=this.#s.createTreeWalker(this.#e,i.SHOW_ELEMENT)),this._collectNodes(s),this._matchNodes(s)}matches(){if(this.#e.nodeType!==i.ELEMENT_NODE){const e=`Unexpected node ${this.#e.nodeName}`;this._onError(new TypeError(e))}let s;try{const e=this._find(M);e.size&&(s=e.has(this.#e))}catch(e){this._onError(e)}return!!s}closest(){if(this.#e.nodeType!==i.ELEMENT_NODE){const e=`Unexpected node ${this.#e.nodeName}`;this._onError(new TypeError(e))}let s;try{const e=this._find($);let f=this.#e;for(;f;){if(e.has(f)){s=f;break}f=f.parentNode}}catch(e){this._onError(e)}return s??null}querySelector(){let s;try{const e=this._find(L);e.delete(this.#e),e.size&&([s]=this._sortNodes(e))}catch(e){this._onError(e)}return s??null}querySelectorAll(){let s;try{const e=this._find(A);e.delete(this.#e),e.size&&(s=this._sortNodes(e))}catch(e){this._onError(e)}return s??[]}}0&&(module.exports={Matcher});
|
|
1
|
+
var z=Object.create;var R=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var F=Object.getPrototypeOf,V=Object.prototype.hasOwnProperty;var H=(T,l)=>{for(var e in l)R(T,e,{get:l[e],enumerable:!0})},W=(T,l,e,c)=>{if(l&&typeof l=="object"||typeof l=="function")for(let o of j(l))!V.call(T,o)&&o!==e&&R(T,o,{get:()=>l[o],enumerable:!(c=B(l,o))||c.enumerable});return T};var G=(T,l,e)=>(e=T!=null?z(F(T)):{},W(l||!T||!T.__esModule?R(e,"default",{value:T,enumerable:!0}):e,T)),q=T=>W(R({},"__esModule",{value:!0}),T);var X={};H(X,{Matcher:()=>Y});module.exports=q(X);var I=G(require("is-potential-custom-element-name"),1),E=require("./dom-util.js"),v=require("./parser.js"),r=require("./constant.js");const C="next",$="prev",A="all",L="first",O="lineal",D="self",U=r.SHOW_DOCUMENT|r.SHOW_DOCUMENT_FRAGMENT|r.SHOW_ELEMENT;class Y{#i;#d;#r;#s;#a;#e;#o;#t;#b;#n;#f;#h;#l;#c;constructor(){this.#d=new Map([[r.SELECTOR_PSEUDO_ELEMENT,r.BIT_01],[r.SELECTOR_ID,r.BIT_02],[r.SELECTOR_CLASS,r.BIT_04],[r.SELECTOR_TYPE,r.BIT_08],[r.SELECTOR_ATTR,r.BIT_16],[r.SELECTOR_PSEUDO_CLASS,r.BIT_32]]),this.#r=new WeakMap}_onError(l){if(l instanceof DOMException||this.#c&&l instanceof this.#c.DOMException)if(l.name===r.NOT_SUPPORTED_ERR)this.#l&&console.warn(l.message);else throw this.#c?new this.#c.DOMException(l.message,l.name):new DOMException(l.message,l.name);else throw l}_setup(l){let e,c;switch(l?.nodeType){case r.DOCUMENT_NODE:{e=l,c=l;break}case r.DOCUMENT_FRAGMENT_NODE:{e=l.ownerDocument,c=l;break}case r.ELEMENT_NODE:{if(l.ownerDocument.contains(l))e=l.ownerDocument,c=l.ownerDocument;else{let f=l;for(;f&&f.parentNode;)f=f.parentNode;e=f.ownerDocument,c=f}break}default:{let f;throw l?.nodeName?f=`Unexpected node ${l.nodeName}`:f=`Unexpected node ${Object.prototype.toString.call(l).slice(r.TYPE_FROM,r.TYPE_TO)}`,new TypeError(f)}}const o=e.createTreeWalker(c,U);return[e.defaultView,e,c,o]}_sortLeaves(l){const e=[...l];return e.length>1&&e.sort((c,o)=>{const{type:a}=c,{type:f}=o,n=this.#d.get(a),d=this.#d.get(f);let t;return n===d?t=0:n>d?t=1:t=-1,t}),e}_correspond(l){let e;try{e=(0,v.parseSelector)(l)}catch(n){this._onError(n)}const c=(0,v.walkAST)(e),o=[],a=[];let f=0;for(const[...n]of c){const d=[];let t=n.shift();if(t&&t.type!==r.COMBINATOR){const i=new Set;for(;t;){if(t.type===r.COMBINATOR){const[h]=n;if(h.type===r.COMBINATOR){const s=`Invalid combinator ${t.name}${h.name}`;throw new this.#c.DOMException(s,r.SYNTAX_ERR)}d.push({combo:t,leaves:this._sortLeaves(i)}),i.clear()}else t&&i.add(t);if(n.length)t=n.shift();else{d.push({combo:null,leaves:this._sortLeaves(i)}),i.clear();break}}}o.push({branch:d,dir:null,filtered:!1,find:!1}),a[f]=new Set,f++}return[o,a]}_traverse(l={},e=this.#h){let c,o=e.currentNode;if(l.nodeType===r.ELEMENT_NODE&&o===l)c=o;else{if(o!==e.root)for(;o&&!(o===e.root||l.nodeType===r.ELEMENT_NODE&&o===l);)o=e.parentNode();if(l.nodeType===r.ELEMENT_NODE)for(;o;){if(o===l){c=o;break}o=e.nextNode()}else c=o}return c??null}_collectNthChild(l,e){const{a:c,b:o,reverse:a,selector:f}=l,{parentNode:n}=e;let d=new Set,t;if(f&&(this.#r.has(f)?t=this.#r.get(f):(t=(0,v.walkAST)(f),this.#r.set(f,t))),n){const i=this.#s.createTreeWalker(n,U);let h=0,s=i.firstChild();for(;s;)h++,s=i.nextSibling();s=this._traverse(n,i);const m=new Set;if(t)for(s=this._traverse(n,i),s=i.firstChild();s;){let b;for(const p of t)if(b=this._matchLeaves(p,s),!b)break;b&&m.add(s),s=i.nextSibling()}if(c===0){if(o>0&&o<=h){if(m.size){let b=0;for(s=this._traverse(n,i),a?s=i.lastChild():s=i.firstChild();s;){if(m.has(s)){if(b===o-1){d.add(s);break}b++}a?s=i.previousSibling():s=i.nextSibling()}}else if(!f){let b=0;for(s=this._traverse(n,i),a?s=i.lastChild():s=i.firstChild();s;){if(b===o-1){d.add(s);break}a?s=i.previousSibling():s=i.nextSibling(),b++}}}}else{let b=o-1;if(c>0)for(;b<0;)b+=c;if(b>=0&&b<h){let p=0,k=c>0?0:o-1;for(s=this._traverse(n,i),a?s=i.lastChild():s=i.firstChild();s&&(s&&b>=0&&b<h);)m.size?m.has(s)&&(k===b&&(d.add(s),b+=c),c>0?k++:k--):p===b&&(f||d.add(s),b+=c),a?s=i.previousSibling():s=i.nextSibling(),p++}}if(a&&d.size>1){const b=[...d];d=new Set(b.reverse())}}else if(e===this.#t&&this.#t.nodeType===r.ELEMENT_NODE&&c+o===1)if(t){let i;for(const h of t)if(i=this._matchLeaves(h,e),i)break;i&&d.add(e)}else d.add(e);return d}_collectNthOfType(l,e){const{a:c,b:o,reverse:a}=l,{localName:f,parentNode:n,prefix:d}=e;let t=new Set;if(n){const i=this.#s.createTreeWalker(n,U);let h=0,s=i.firstChild();for(;s;)h++,s=i.nextSibling();if(c===0){if(o>0&&o<=h){let m=0;for(s=this._traverse(n,i),a?s=i.lastChild():s=i.firstChild();s;){const{localName:b,prefix:p}=s;if(b===f&&p===d){if(m===o-1){t.add(s);break}m++}a?s=i.previousSibling():s=i.nextSibling()}}}else{let m=o-1;if(c>0)for(;m<0;)m+=c;if(m>=0&&m<h){let b=c>0?0:o-1;for(s=this._traverse(n,i),a?s=i.lastChild():s=i.firstChild();s;){const{localName:p,prefix:k}=s;if(p===f&&k===d){if(b===m&&(t.add(s),m+=c),m<0||m>=h)break;c>0?b++:b--}a?s=i.previousSibling():s=i.nextSibling()}}}if(a&&t.size>1){const m=[...t];t=new Set(m.reverse())}}else e===this.#t&&this.#t.nodeType===r.ELEMENT_NODE&&c+o===1&&t.add(e);return t}_matchAnPlusB(l,e,c){const{nth:{a:o,b:a,name:f},selector:n}=l,d=(0,v.unescapeSelector)(f),t=new Map;d?(d==="even"?(t.set("a",2),t.set("b",0)):d==="odd"&&(t.set("a",2),t.set("b",1)),c.indexOf("last")>-1&&t.set("reverse",!0)):(typeof o=="string"&&/-?\d+/.test(o)?t.set("a",o*1):t.set("a",0),typeof a=="string"&&/-?\d+/.test(a)?t.set("b",a*1):t.set("b",0),c.indexOf("last")>-1&&t.set("reverse",!0));let i=new Set;if(t.has("a")&&t.has("b")){if(/^nth-(?:last-)?child$/.test(c)){n&&t.set("selector",n);const h=Object.fromEntries(t),s=this._collectNthChild(h,e);s.size&&(i=s)}else if(/^nth-(?:last-)?of-type$/.test(c)){const h=Object.fromEntries(t),s=this._collectNthOfType(h,e);s.size&&(i=s)}}return i}_matchPseudoElementSelector(l,e={}){const{forgive:c}=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"placeholder":case"selection":case"target-text":{if(this.#l){const o=`Unsupported pseudo-element ::${l}`;throw new DOMException(o,r.NOT_SUPPORTED_ERR)}break}case"part":case"slotted":{if(this.#l){const o=`Unsupported pseudo-element ::${l}()`;throw new DOMException(o,r.NOT_SUPPORTED_ERR)}break}default:if(l.startsWith("-webkit-")){if(this.#l){const o=`Unsupported pseudo-element ::${l}`;throw new DOMException(o,r.NOT_SUPPORTED_ERR)}}else if(!c){const o=`Unknown pseudo-element ::${l}`;throw new DOMException(o,r.SYNTAX_ERR)}}}_matchDirectionPseudoClass(l,e){const c=(0,v.unescapeSelector)(l.name),o=(0,E.getDirectionality)(e);let a;return c===o&&(a=e),a??null}_matchLanguagePseudoClass(l,e){const c=(0,v.unescapeSelector)(l.name);let o;if(c==="*")if(e.hasAttribute("lang"))e.getAttribute("lang")&&(o=e);else{let a=e.parentNode;for(;a&&a.nodeType===r.ELEMENT_NODE;){if(a.hasAttribute("lang")){a.getAttribute("lang")&&(o=e);break}a=a.parentNode}}else if(c){const a=`(?:-${r.ALPHA_NUM})*`;if(new RegExp(`^(?:\\*-)?${r.ALPHA_NUM}${a}$`,"i").test(c)){let n;if(c.indexOf("-")>-1){const[d,t,...i]=c.split("-");let h;d==="*"?h=`${r.ALPHA_NUM}${a}`:h=`${d}${a}`;const s=`-${t}${a}`,m=i.length;let b="";if(m)for(let p=0;p<m;p++)b+=`-${i[p]}${a}`;n=new RegExp(`^${h}${s}${b}$`,"i")}else n=new RegExp(`^${c}${a}$`,"i");if(e.hasAttribute("lang"))n.test(e.getAttribute("lang"))&&(o=e);else{let d=e.parentNode;for(;d&&d.nodeType===r.ELEMENT_NODE;){if(d.hasAttribute("lang")){const t=d.getAttribute("lang");n.test(t)&&(o=e);break}d=d.parentNode}}}}return o??null}_matchHasPseudoFunc(l,e){let c;if(Array.isArray(l)&&l.length){const[o]=l,{type:a}=o;let f;a===r.COMBINATOR?f=l.shift():f={name:" ",type:r.COMBINATOR};const n=[];for(;l.length;){const[i]=l,{type:h}=i;if(h===r.COMBINATOR)break;n.push(l.shift())}const d={combo:f,leaves:n},t=this._matchCombinator(d,e,{dir:C});if(t.size)if(l.length){for(const i of t)if(c=this._matchHasPseudoFunc(Object.assign([],l),i),c)break}else c=!0}return!!c}_matchLogicalPseudoFunc(l,e){const{astName:c="",branches:o=[],selector:a="",twigBranches:f=[]}=l;let n;if(c==="has")if(a.includes(":has("))n=null;else{let d;for(const t of o)if(d=this._matchHasPseudoFunc(Object.assign([],t),e),d)break;d&&(n=e)}else{const d=/^(?:is|where)$/.test(c),t=f.length;let i;for(let h=0;h<t;h++){const s=f[h],m=s.length-1,{leaves:b}=s[m];if(i=this._matchLeaves(b,e,{forgive:d}),i&&m>0){let p=new Set([e]);for(let k=m-1;k>=0;k--){const N=s[k],u=[];for(const w of p){const g=this._matchCombinator(N,w,{forgive:d,dir:$});g.size&&u.push(...g)}if(u.length)k===0?i=!0:p=new Set(u);else{i=!1;break}}}if(i)break}c==="not"?i||(n=e):i&&(n=e)}return n??null}_matchPseudoClassSelector(l,e,c={}){const{children:o}=l,{localName:a,parentNode:f}=e,{forgive:n}=c,d=(0,v.unescapeSelector)(l.name);let t=new Set;if(r.REG_LOGICAL_PSEUDO.test(d)){let i;if(this.#r.has(l))i=this.#r.get(l);else{const s=(0,v.walkAST)(l),m=[],b=[];for(const[...p]of s){for(const w of p){const g=(0,v.generateCSS)(w);m.push(g)}const k=[],N=new Set;let u=p.shift();for(;u;)if(u.type===r.COMBINATOR?(k.push({combo:u,leaves:[...N]}),N.clear()):u&&N.add(u),p.length)u=p.shift();else{k.push({combo:null,leaves:[...N]}),N.clear();break}b.push(k)}i={astName:d,branches:s,twigBranches:b,selector:m.join(",")},this.#r.set(l,i)}const h=this._matchLogicalPseudoFunc(i,e);h&&t.add(h)}else if(Array.isArray(o)){const[i]=o;if(/^nth-(?:last-)?(?:child|of-type)$/.test(d)){const h=this._matchAnPlusB(i,e,d);h.size&&(t=h)}else if(d==="dir"){const h=this._matchDirectionPseudoClass(i,e);h&&t.add(h)}else if(d==="lang"){const h=this._matchLanguagePseudoClass(i,e);h&&t.add(h)}else switch(d){case"current":case"nth-col":case"nth-last-col":{if(this.#l){const h=`Unsupported pseudo-class :${d}()`;throw new DOMException(h,r.NOT_SUPPORTED_ERR)}break}case"host":case"host-context":break;default:if(!n){const h=`Unknown pseudo-class :${d}()`;throw new DOMException(h,r.SYNTAX_ERR)}}}else{const i=/^a(?:rea)?$/,h=/^(?:(?:fieldse|inpu|selec)t|button|opt(?:group|ion)|textarea)$/,s=/^(?:(?:inpu|selec)t|button|form|textarea)$/,m=/^d(?:etails|ialog)$/,b=/^(?:checkbox|radio)$/,p=/^(?:date(?:time-local)?|month|time|week)$/,k=/(?:(?:rang|tim)e|date(?:time-local)?|month|number|week)$/,N=/^(?:(?:emai|te|ur)l|number|password|search|text)$/;switch(d){case"any-link":case"link":{i.test(a)&&e.hasAttribute("href")&&t.add(e);break}case"local-link":{if(i.test(a)&&e.hasAttribute("href")){const{href:u,origin:w,pathname:g}=new URL(this.#s.URL),y=new URL(e.getAttribute("href"),u);y.origin===w&&y.pathname===g&&t.add(e)}break}case"visited":break;case"target":{const{hash:u}=new URL(this.#s.URL);e.id&&u===`#${e.id}`&&this.#s.contains(e)&&t.add(e);break}case"target-within":{const{hash:u}=new URL(this.#s.URL);if(u){const w=u.replace(/^#/,"");let g=this.#s.getElementById(w);for(;g;){if(g===e){t.add(e);break}g=g.parentNode}}break}case"scope":{this.#e.nodeType===r.ELEMENT_NODE?e===this.#e&&t.add(e):e===this.#s.documentElement&&t.add(e);break}case"focus":{e===this.#s.activeElement&&t.add(e);break}case"focus-within":{let u=this.#s.activeElement;for(;u;){if(u===e){t.add(e);break}u=u.parentNode}break}case"open":{m.test(a)&&e.hasAttribute("open")&&t.add(e);break}case"closed":{m.test(a)&&!e.hasAttribute("open")&&t.add(e);break}case"disabled":{if(h.test(a)||(0,I.default)(a))if(e.disabled||e.hasAttribute("disabled"))t.add(e);else{let u=f;for(;u&&u.localName!=="fieldset";)u=u.parentNode;u&&f.localName!=="legend"&&u.hasAttribute("disabled")&&t.add(e)}break}case"enabled":{(h.test(a)||(0,I.default)(a))&&!(e.disabled&&e.hasAttribute("disabled"))&&t.add(e);break}case"read-only":{switch(a){case"textarea":{(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&t.add(e);break}case"input":{(!e.type||p.test(e.type)||N.test(e.type))&&(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&t.add(e);break}default:(0,E.isContentEditable)(e)||t.add(e)}break}case"read-write":{switch(a){case"textarea":{e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled")||t.add(e);break}case"input":{(!e.type||p.test(e.type)||N.test(e.type))&&!(e.readonly||e.hasAttribute("readonly")||e.disabled||e.hasAttribute("disabled"))&&t.add(e);break}default:(0,E.isContentEditable)(e)&&t.add(e)}break}case"placeholder-shown":{let u;a==="textarea"?u=e:a==="input"&&(e.hasAttribute("type")?N.test(e.getAttribute("type"))&&(u=e):u=e),u&&e.value===""&&e.hasAttribute("placeholder")&&e.getAttribute("placeholder").trim().length&&t.add(e);break}case"checked":{(e.checked&&a==="input"&&e.hasAttribute("type")&&b.test(e.getAttribute("type"))||e.selected&&a==="option")&&t.add(e);break}case"indeterminate":{if(e.indeterminate&&a==="input"&&e.type==="checkbox"||a==="progress"&&!e.hasAttribute("value"))t.add(e);else if(a==="input"&&e.type==="radio"&&!e.hasAttribute("checked")){const u=e.name;let w=e.parentNode;for(;w&&w.localName!=="form";)w=w.parentNode;w||(w=this.#s.documentElement);let g;const y=[].slice.call(w.getElementsByTagName("input"));for(const _ of y)if(_.getAttribute("type")==="radio"&&(u?_.getAttribute("name")===u&&(g=!!_.checked):_.hasAttribute("name")||(g=!!_.checked),g))break;g||t.add(e)}break}case"default":{const u=/^(?:button|reset)$/,w=/^(?:image|submit)$/;if(a==="button"&&!(e.hasAttribute("type")&&u.test(e.getAttribute("type")))||a==="input"&&e.hasAttribute("type")&&w.test(e.getAttribute("type"))){let g=e.parentNode;for(;g&&g.localName!=="form";)g=g.parentNode;if(g){const y=this.#s.createTreeWalker(g,r.SHOW_ELEMENT);let _=y.firstChild();for(;_;){const x=_.localName;let S;if(x==="button"?S=!(_.hasAttribute("type")&&u.test(_.getAttribute("type"))):x==="input"&&(S=_.hasAttribute("type")&&w.test(_.getAttribute("type"))),S){_===e&&t.add(e);break}_=y.nextNode()}}}else if(a==="input"&&e.hasAttribute("type")&&b.test(e.getAttribute("type"))&&(e.checked||e.hasAttribute("checked")))t.add(e);else if(a==="option"){let g=!1,y=f;for(;y&&y.localName!=="datalist";){if(y.localName==="select"){(y.multiple||y.hasAttribute("multiple"))&&(g=!0);break}y=y.parentNode}if(g)(e.selected||e.hasAttribute("selected"))&&t.add(e);else{const _=new Set,x=this.#s.createTreeWalker(f,r.SHOW_ELEMENT);let S=x.firstChild();for(;S;){if(S.selected||S.hasAttribute("selected")){_.add(S);break}S=x.nextSibling()}_.size&&_.has(e)&&t.add(e)}}break}case"valid":{if(s.test(a))e.checkValidity()&&t.add(e);else if(a==="fieldset"){let u;const w=this.#s.createTreeWalker(e,r.SHOW_ELEMENT);let g=w.firstChild();for(;g&&!(s.test(g.localName)&&(u=g.checkValidity(),!u));)g=w.nextNode();u&&t.add(e)}break}case"invalid":{if(s.test(a))e.checkValidity()||t.add(e);else if(a==="fieldset"){let u;const w=this.#s.createTreeWalker(e,r.SHOW_ELEMENT);let g=w.firstChild();for(;g&&!(s.test(g.localName)&&(u=g.checkValidity(),!u));)g=w.nextNode();u||t.add(e)}break}case"in-range":{a==="input"&&!(e.readonly||e.hasAttribute("readonly"))&&!(e.disabled||e.hasAttribute("disabled"))&&e.hasAttribute("type")&&k.test(e.getAttribute("type"))&&!(e.validity.rangeUnderflow||e.validity.rangeOverflow)&&(e.hasAttribute("min")||e.hasAttribute("max")||e.getAttribute("type")==="range")&&t.add(e);break}case"out-of-range":{a==="input"&&!(e.readonly||e.hasAttribute("readonly"))&&!(e.disabled||e.hasAttribute("disabled"))&&e.hasAttribute("type")&&k.test(e.getAttribute("type"))&&(e.validity.rangeUnderflow||e.validity.rangeOverflow)&&t.add(e);break}case"required":{let u;if(/^(?:select|textarea)$/.test(a))u=e;else if(a==="input")if(e.hasAttribute("type")){const w=e.getAttribute("type");(w==="file"||b.test(w)||p.test(w)||N.test(w))&&(u=e)}else u=e;u&&(e.required||e.hasAttribute("required"))&&t.add(e);break}case"optional":{let u;if(/^(?:select|textarea)$/.test(a))u=e;else if(a==="input")if(e.hasAttribute("type")){const w=e.getAttribute("type");(w==="file"||b.test(w)||p.test(w)||N.test(w))&&(u=e)}else u=e;u&&!(e.required||e.hasAttribute("required"))&&t.add(e);break}case"root":{e===this.#s.documentElement&&t.add(e);break}case"empty":{if(e.hasChildNodes()){let u;const w=this.#s.createTreeWalker(e,r.SHOW_ALL);let g=w.firstChild();for(;g&&(u=g.nodeType!==r.ELEMENT_NODE&&g.nodeType!==r.TEXT_NODE,!!u);)g=w.nextSibling();u&&t.add(e)}else t.add(e);break}case"first-child":{(f&&e===f.firstElementChild||e===this.#t&&this.#t.nodeType===r.ELEMENT_NODE)&&t.add(e);break}case"last-child":{(f&&e===f.lastElementChild||e===this.#t&&this.#t.nodeType===r.ELEMENT_NODE)&&t.add(e);break}case"only-child":{(f&&e===f.firstElementChild&&e===f.lastElementChild||e===this.#t&&this.#t.nodeType===r.ELEMENT_NODE)&&t.add(e);break}case"first-of-type":{if(f){const[u]=this._collectNthOfType({a:0,b:1},e);u&&t.add(u)}else e===this.#t&&this.#t.nodeType===r.ELEMENT_NODE&&t.add(e);break}case"last-of-type":{if(f){const[u]=this._collectNthOfType({a:0,b:1,reverse:!0},e);u&&t.add(u)}else e===this.#t&&this.#t.nodeType===r.ELEMENT_NODE&&t.add(e);break}case"only-of-type":{if(f){const[u]=this._collectNthOfType({a:0,b:1},e);if(u===e){const[w]=this._collectNthOfType({a:0,b:1,reverse:!0},e);w===e&&t.add(e)}}else e===this.#t&&this.#t.nodeType===r.ELEMENT_NODE&&t.add(e);break}case"host":case"host-context":break;case"after":case"before":case"first-letter":case"first-line":{if(this.#l){const u=`Unsupported pseudo-element ::${d}`;throw new DOMException(u,r.NOT_SUPPORTED_ERR)}break}case"active":case"autofill":case"blank":case"buffering":case"current":case"defined":case"focus-visible":case"fullscreen":case"future":case"hover":case"modal":case"muted":case"past":case"paused":case"picture-in-picture":case"playing":case"seeking":case"stalled":case"user-invalid":case"user-valid":case"volume-locked":case"-webkit-autofill":{if(this.#l){const u=`Unsupported pseudo-class :${d}`;throw new DOMException(u,r.NOT_SUPPORTED_ERR)}break}default:if(d.startsWith("-webkit-")){if(this.#l){const u=`Unsupported pseudo-class :${d}`;throw new DOMException(u,r.NOT_SUPPORTED_ERR)}}else if(!n){const u=`Unknown pseudo-class :${d}`;throw new DOMException(u,r.SYNTAX_ERR)}}}return t}_matchAttributeSelector(l,e){const{flags:c,matcher:o,name:a,value:f}=l;if(typeof c=="string"&&!/^[is]$/i.test(c)){const i=`Invalid selector ${(0,v.generateCSS)(l)}`;throw new DOMException(i,r.SYNTAX_ERR)}const{attributes:n}=e;let d;if(n&&n.length){let t;this.#s.contentType==="text/html"?typeof c=="string"&&/^s$/i.test(c)?t=!1:t=!0:typeof c=="string"&&/^i$/i.test(c)?t=!0:t=!1;let i=(0,v.unescapeSelector)(a.name);t&&(i=i.toLowerCase());const h=new Set;if(i.indexOf("|")>-1){const{prefix:s,tagName:m}=(0,E.selectorToNodeProps)(i);for(let{name:b,value:p}of n)switch(t&&(b=b.toLowerCase(),p=p.toLowerCase()),s){case"":{m===b&&h.add(p);break}case"*":{b.indexOf(":")>-1?b.endsWith(`:${m}`)&&h.add(p):m===b&&h.add(p);break}default:if(b.indexOf(":")>-1){const[k,N]=b.split(":");s===k&&m===N&&(0,E.isNamespaceDeclared)(s,e)&&h.add(p)}}}else for(let{name:s,value:m}of n)if(t&&(s=s.toLowerCase(),m=m.toLowerCase()),s.indexOf(":")>-1){const[b,p]=s.split(":");if(b==="xml"&&p==="lang")continue;i===p&&h.add(m)}else i===s&&h.add(m);if(h.size){const{name:s,value:m}=f||{};let b;switch(s?t?b=s.toLowerCase():b=s:m?t?b=m.toLowerCase():b=m:m===""&&(b=m),o){case"=":{typeof b=="string"&&h.has(b)&&(d=e);break}case"~=":{if(b&&typeof b=="string"){for(const p of h)if(new Set(p.split(/\s+/)).has(b)){d=e;break}}break}case"|=":{if(b&&typeof b=="string"){let p;for(const k of h)if(k===b||k.startsWith(`${b}-`)){p=k;break}p&&(d=e)}break}case"^=":{if(b&&typeof b=="string"){let p;for(const k of h)if(k.startsWith(`${b}`)){p=k;break}p&&(d=e)}break}case"$=":{if(b&&typeof b=="string"){let p;for(const k of h)if(k.endsWith(`${b}`)){p=k;break}p&&(d=e)}break}case"*=":{if(b&&typeof b=="string"){let p;for(const k of h)if(k.includes(`${b}`)){p=k;break}p&&(d=e)}break}case null:default:d=e}}}return d??null}_matchClassSelector(l,e){const c=(0,v.unescapeSelector)(l.name);let o;return e.classList.contains(c)&&(o=e),o??null}_matchIDSelector(l,e){const c=(0,v.unescapeSelector)(l.name),{id:o}=e;let a;return c===o&&(a=e),a??null}_matchTypeSelector(l,e,c={}){const o=(0,v.unescapeSelector)(l.name),{localName:a,prefix:f}=e,{forgive:n}=c;let{prefix:d,tagName:t}=(0,E.selectorToNodeProps)(o,e);this.#s.contentType==="text/html"&&(d=d.toLowerCase(),t=t.toLowerCase());let i,h;a.indexOf(":")>-1?[i,h]=a.split(":"):(i=f||"",h=a);let s;if(d===""&&i==="")e.namespaceURI===null&&(t==="*"||t===h)&&(s=e);else if(d==="*")(t==="*"||t===h)&&(s=e);else if(d===i){if((0,E.isNamespaceDeclared)(d,e))(t==="*"||t===h)&&(s=e);else if(!n){const m=`Undeclared namespace ${d}`;throw new DOMException(m,r.SYNTAX_ERR)}}else if(d&&!n&&!(0,E.isNamespaceDeclared)(d,e)){const m=`Undeclared namespace ${d}`;throw new DOMException(m,r.SYNTAX_ERR)}return s??null}_matchShadowHostPseudoClass(l,e){const{children:c}=l,o=(0,v.unescapeSelector)(l.name);let a;if(Array.isArray(c)){const[f]=(0,v.walkAST)(c[0]),[...n]=f,{host:d}=e;if(o==="host"){let t;for(const i of n){const{type:h}=i;if(h===r.COMBINATOR){const m=`Invalid selector ${(0,v.generateCSS)(l)}`;throw new DOMException(m,r.SYNTAX_ERR)}if(t=this._matchSelector(i,d).has(d),!t)break}t&&(a=e)}else if(o==="host-context"){let t,i=d;for(;i;){for(const h of n){const{type:s}=h;if(s===r.COMBINATOR){const b=`Invalid selector ${(0,v.generateCSS)(l)}`;throw new DOMException(b,r.SYNTAX_ERR)}if(t=this._matchSelector(h,i).has(i),!t)break}if(t)break;i=i.parentNode}t&&(a=e)}}else if(o==="host")a=e;else{const f=`Invalid selector :${o}`;throw new DOMException(f,r.SYNTAX_ERR)}return a??null}_matchSelector(l,e,c){const{type:o}=l,a=(0,v.unescapeSelector)(l.name);let f=new Set;if(e.nodeType===r.ELEMENT_NODE)switch(o){case r.SELECTOR_ATTR:{const n=this._matchAttributeSelector(l,e);n&&f.add(n);break}case r.SELECTOR_CLASS:{const n=this._matchClassSelector(l,e);n&&f.add(n);break}case r.SELECTOR_ID:{const n=this._matchIDSelector(l,e);n&&f.add(n);break}case r.SELECTOR_PSEUDO_CLASS:{const n=this._matchPseudoClassSelector(l,e,c);n.size&&(f=n);break}case r.SELECTOR_PSEUDO_ELEMENT:{this._matchPseudoElementSelector(a,c);break}case r.SELECTOR_TYPE:default:{const n=this._matchTypeSelector(l,e,c);n&&f.add(n)}}else if(this.#n&&o===r.SELECTOR_PSEUDO_CLASS&&e.nodeType===r.DOCUMENT_FRAGMENT_NODE){if(a!=="has"&&r.REG_LOGICAL_PSEUDO.test(a)){const n=this._matchPseudoClassSelector(l,e,c);n.size&&(f=n)}else if(r.REG_SHADOW_HOST.test(a)){const n=this._matchShadowHostPseudoClass(l,e);n&&f.add(n)}}return f}_matchLeaves(l,e,c){let o;for(const a of l)if(o=this._matchSelector(a,e,c).has(e),!o)break;return!!o}_findDescendantNodes(l,e){const[c,...o]=l,{type:a}=c,f=(0,v.unescapeSelector)(c.name),n=o.length>0;let d=new Set,t=!1;if(this.#n)t=!0;else switch(a){case r.SELECTOR_ID:{if(this.#t.nodeType===r.ELEMENT_NODE)t=!0;else{const i=this.#t.getElementById(f);i&&i!==e&&e.contains(i)&&(n?this._matchLeaves(o,i)&&d.add(i):d.add(i))}break}case r.SELECTOR_CLASS:{const i=[].slice.call(e.getElementsByClassName(f));if(i.length)if(n)for(const h of i)this._matchLeaves(o,h)&&d.add(h);else d=new Set(i);break}case r.SELECTOR_TYPE:{if(this.#s.contentType==="text/html"&&!/[*|]/.test(f)){const i=[].slice.call(e.getElementsByTagName(f));if(i.length)if(n)for(const h of i)this._matchLeaves(o,h)&&d.add(h);else d=new Set(i)}else t=!0;break}case r.SELECTOR_PSEUDO_ELEMENT:{this._matchPseudoElementSelector(f);break}default:t=!0}return{nodes:d,pending:t}}_matchCombinator(l,e,c={}){const{combo:o,leaves:a}=l,{name:f}=o,{dir:n,forgive:d}=c;let t=new Set;if(n===C)switch(f){case"+":{const i=e.nextElementSibling;i&&this._matchLeaves(a,i,{forgive:d})&&t.add(i);break}case"~":{const{parentNode:i}=e;if(i){const h=this.#s.createTreeWalker(i,r.SHOW_ELEMENT);let s=this._traverse(e,h);for(s===e&&(s=h.nextSibling());s;)this._matchLeaves(a,s,{forgive:d})&&t.add(s),s=h.nextSibling()}break}case">":{const i=this.#s.createTreeWalker(e,r.SHOW_ELEMENT);let h=i.firstChild();for(;h;)this._matchLeaves(a,h,{forgive:d})&&t.add(h),h=i.nextSibling();break}case" ":default:{const{nodes:i,pending:h}=this._findDescendantNodes(a,e);if(i.size)t=i;else if(h){const s=this.#s.createTreeWalker(e,r.SHOW_ELEMENT);let m=s.nextNode();for(;m;)this._matchLeaves(a,m,{forgive:d})&&t.add(m),m=s.nextNode()}}}else switch(f){case"+":{const i=e.previousElementSibling;i&&this._matchLeaves(a,i,{forgive:d})&&t.add(i);break}case"~":{const i=this.#s.createTreeWalker(e.parentNode,r.SHOW_ELEMENT);let h=i.firstChild();for(;h&&h!==e;)this._matchLeaves(a,h,{forgive:d})&&t.add(h),h=i.nextSibling();break}case">":{const i=e.parentNode;i&&this._matchLeaves(a,i,{forgive:d})&&t.add(i);break}case" ":default:{const i=[];let h=e.parentNode;for(;h;)this._matchLeaves(a,h,{forgive:d})&&i.push(h),h=h.parentNode;i.length&&(t=new Set(i.reverse()))}}return t}_findNode(l,e={}){let{node:c,walker:o}=e;o||(o=this.#h);let a,f=this._traverse(c,o);if(f)for((f.nodeType!==r.ELEMENT_NODE||f===c&&f!==this.#t)&&(f=o.nextNode());f;){let n;if(this.#e.nodeType===r.ELEMENT_NODE?f===this.#e?n=!0:n=this.#e.contains(f):n=!0,n&&this._matchLeaves(l,f)){a=f;break}f=o.nextNode()}return a??null}_findEntryNodes(l,e){const{leaves:c}=l,[o,...a]=c,{type:f}=o,n=(0,v.unescapeSelector)(o.name),d=a.length>0;let t=new Set,i=!1,h=!1;switch(f){case r.SELECTOR_ID:{if(e===D)this._matchLeaves(c,this.#e)&&(t.add(this.#e),i=!0);else if(e===O){let s=this.#e;for(;s;)this._matchLeaves(c,s)&&(t.add(s),i=!0),s=s.parentNode}else if(e===A||this.#t.nodeType===r.ELEMENT_NODE)h=!0;else{const s=this.#t.getElementById(n);s&&(t.add(s),i=!0)}break}case r.SELECTOR_CLASS:{if(e===D)this.#e.nodeType===r.ELEMENT_NODE&&this.#e.classList.contains(n)&&t.add(this.#e);else if(e===O){let s=this.#e;for(;s&&s.nodeType===r.ELEMENT_NODE;)s.classList.contains(n)&&t.add(s),s=s.parentNode}else if(e===L){const s=this._findNode(c,{node:this.#e,walker:this.#a});s&&(t.add(s),i=!0)}else if(this.#t.nodeType===r.DOCUMENT_FRAGMENT_NODE||this.#t.nodeType===r.ELEMENT_NODE)h=!0;else{const s=[].slice.call(this.#t.getElementsByClassName(n));if(this.#e.nodeType===r.ELEMENT_NODE)for(const m of s)(m===this.#e||(0,E.isInclusive)(m,this.#e))&&t.add(m);else s.length&&(t=new Set(s))}break}case r.SELECTOR_TYPE:{if(e===D)this.#e.nodeType===r.ELEMENT_NODE&&this._matchLeaves(c,this.#e)&&(t.add(this.#e),i=!0);else if(e===O){let s=this.#e;for(;s&&s.nodeType===r.ELEMENT_NODE;)this._matchLeaves(c,s)&&(t.add(s),i=!0),s=s.parentNode}else if(e===L){const s=this._findNode(c,{node:this.#e,walker:this.#a});s&&(t.add(s),i=!0)}else if(this.#s.contentType!=="text/html"||/[*|]/.test(n)||this.#t.nodeType===r.DOCUMENT_FRAGMENT_NODE||this.#t.nodeType===r.ELEMENT_NODE)h=!0;else{const s=[].slice.call(this.#t.getElementsByTagName(n));if(this.#e.nodeType===r.ELEMENT_NODE)for(const m of s)(m===this.#e||(0,E.isInclusive)(m,this.#e))&&t.add(m);else s.length&&(t=new Set(s))}break}case r.SELECTOR_PSEUDO_ELEMENT:{this._matchPseudoElementSelector(n);break}default:if(e!==O&&r.REG_SHADOW_HOST.test(n)){if(this.#n&&this.#e.nodeType===r.DOCUMENT_FRAGMENT_NODE){const s=this._matchShadowHostPseudoClass(o,this.#e);s&&t.add(s)}}else if(e===D)this._matchLeaves(c,this.#e)&&(t.add(this.#e),i=!0);else if(e===O){let s=this.#e;for(;s;)this._matchLeaves(c,s)&&(t.add(s),i=!0),s=s.parentNode}else if(e===L){const s=this._findNode(c,{node:this.#e,walker:this.#a});s&&(t.add(s),i=!0)}else h=!0}return{compound:d,filtered:i,nodes:t,pending:h}}_getEntryTwig(l,e){const c=l.length,o=c>1,a=l[0];let f,n;if(o){const{combo:d,leaves:[{type:t}]}=a,i=l[c-1],{leaves:[{type:h}]}=i;if(h===r.SELECTOR_PSEUDO_ELEMENT||h===r.SELECTOR_ID)f=$,n=i;else if(t===r.SELECTOR_PSEUDO_ELEMENT||t===r.SELECTOR_ID)f=C,n=a;else if(e===A)if(c===2){const{name:s}=d;/^[+~]$/.test(s)?(f=$,n=i):(f=C,n=a)}else f=C,n=a;else{let s,m;for(const{combo:b,leaves:[p]}of l){const{type:k}=p,N=(0,v.unescapeSelector)(p.name);if(k===r.SELECTOR_PSEUDO_CLASS&&N==="dir"){s=!1;break}if(b&&!m){const{name:u}=b;/^[+~]$/.test(u)&&(s=!0,m=!0)}}s?(f=C,n=a):(f=$,n=i)}}else f=$,n=a;return{complex:o,dir:f,twig:n}}_collectNodes(l){const e=this.#i.values();if(l===A||l===L){const c=new Set;let o=0;for(const{branch:a}of e){const{dir:f,twig:n}=this._getEntryTwig(a,l),{compound:d,filtered:t,nodes:i,pending:h}=this._findEntryNodes(n,l);i.size?(this.#i[o].find=!0,this.#o[o]=i):h&&c.add(new Map([["index",o],["twig",n]])),this.#i[o].dir=f,this.#i[o].filtered=t||!d,o++}if(c.size){let a,f;this.#e!==this.#t&&this.#e.nodeType===r.ELEMENT_NODE?(a=this.#e,f=this.#a):(a=this.#t,f=this.#h);let n=this._traverse(a,f);for(;n;){let d=!1;if(this.#e.nodeType===r.ELEMENT_NODE?n===this.#e?d=!0:d=this.#e.contains(n):d=!0,d)for(const t of c){const{leaves:i}=t.get("twig");if(this._matchLeaves(i,n)){const s=t.get("index");this.#i[s].filtered=!0,this.#i[s].find=!0,this.#o[s].add(n)}}n=f.nextNode()}}}else{let c=0;for(const{branch:o}of e){const a=o[o.length-1],{compound:f,filtered:n,nodes:d}=this._findEntryNodes(a,l);d.size&&(this.#i[c].find=!0,this.#o[c]=d),this.#i[c].dir=$,this.#i[c].filtered=n||!f,c++}}return[this.#i,this.#o]}_sortNodes(l){const e=[...l];return e.length>1&&e.sort((c,o)=>{let a;return(0,E.isPreceding)(o,c)?a=1:a=-1,a}),e}_matchNodes(l){const[...e]=this.#i,c=e.length;let o=new Set;for(let a=0;a<c;a++){const{branch:f,dir:n,filtered:d,find:t}=e[a],i=f.length;if(i&&t){const h=this.#o[a],s=i-1;if(s===0){const{leaves:[,...m]}=f[0];if((l===A||l===L)&&this.#e.nodeType===r.ELEMENT_NODE){for(const b of h)if((d||this._matchLeaves(m,b))&&b!==this.#e&&this.#e.contains(b)&&(o.add(b),l!==A))break}else if(m.length){for(const b of h)if((d||this._matchLeaves(m,b))&&(o.add(b),l!==A))break}else if(l===A)if(o.size){const b=[...o];o=new Set([...b,...h]),this.#f=!0}else o=new Set([...h]);else{const[b]=[...h];o.add(b)}}else if(n===C){let{combo:m,leaves:b}=f[0];const[,...p]=b;let k;for(const N of h){if(d||this._matchLeaves(p,N)){let w=new Set([N]);for(let g=1;g<i;g++){const{combo:y,leaves:_}=f[g],x=[];for(const S of w){const P={combo:m,leaves:_},M=this._matchCombinator(P,S,{dir:n});M.size&&x.push(...M)}if(x.length)if(g===s){if(l===A)if(o.size){const S=[...o];o=new Set([...S,...x]),this.#f=!0}else o=new Set([...x]);else{const[S]=this._sortNodes(x);o.add(S)}k=!0}else m=y,w=new Set(x),k=!1;else{k=!1;break}}}else k=!1;if(k&&l!==A)break}if(!k&&l===L){const[N]=[...h];let u=this._findNode(b,{node:N,walker:this.#a});for(;u;){let w=new Set([u]);for(let g=1;g<i;g++){const{combo:y,leaves:_}=f[g],x=[];for(const S of w){const P={combo:m,leaves:_},M=this._matchCombinator(P,S,{dir:n});M.size&&x.push(...M)}if(x.length)if(g===s){const[S]=this._sortNodes(x);o.add(S),k=!0}else m=y,w=new Set(x),k=!1;else{k=!1;break}}if(k)break;u=this._findNode(b,{node:u,walker:this.#a}),w=new Set([u])}}}else{const{leaves:m}=f[s],[,...b]=m;let p;for(const k of h){if(d||this._matchLeaves(b,k)){let u=new Set([k]);for(let w=s-1;w>=0;w--){const g=f[w],y=[];for(const _ of u){const x=this._matchCombinator(g,_,{dir:n});x.size&&y.push(...x)}if(y.length)w===0?(o.add(k),p=!0):(u=new Set(y),p=!1);else{p=!1;break}}}if(p&&l!==A)break}if(!p&&l===L){const[k]=[...h];let N=this._findNode(m,{node:k,walker:this.#a});for(;N;){let u=new Set([N]);for(let w=s-1;w>=0;w--){const g=f[w],y=[];for(const _ of u){const x=this._matchCombinator(g,_,{dir:n});x.size&&y.push(...x)}if(y.length)w===0?(o.add(N),p=!0):(u=new Set(y),p=!1);else{p=!1;break}}if(p)break;N=this._findNode(m,{node:N,walker:this.#a}),u=new Set([N])}}}}}return o}_find(l,e,c,o={}){const{warn:a}=o;if(this.#l=!!a,e){if(e.nodeType!==r.DOCUMENT_NODE&&e.nodeType!==r.DOCUMENT_FRAGMENT_NODE&&e.nodeType!==r.ELEMENT_NODE){const n=`Unexpected node ${e.nodeName}`;throw new TypeError(n)}else if((l===D||l===O)&&e.nodeType!==r.ELEMENT_NODE){const n=`Unexpected node ${e.nodeName}`;throw new TypeError(n)}}else{const d=`Unexpected node ${Object.prototype.toString.call(e).slice(r.TYPE_FROM,r.TYPE_TO)}`;throw new TypeError(d)}if(this.#r=new WeakMap,this.#e=e,[this.#c,this.#s,this.#t,this.#h]=this._setup(e),this.#n=(0,E.isInShadowTree)(e),c&&c===this.#b)for(const n of this.#o)n.clear();else this.#b=c,[this.#i,this.#o]=this._correspond(c);return(l===A||l===L)&&(this.#a=this.#s.createTreeWalker(this.#e,r.SHOW_ELEMENT),this.#f=!1),this._collectNodes(l),this._matchNodes(l)}matches(l,e,c){let o;try{const a=this._find(D,l,e,c);a.size&&(o=a.has(this.#e))}catch(a){this._onError(a)}return!!o}closest(l,e,c){let o;try{const a=this._find(O,l,e,c);let f=this.#e;for(;f;){if(a.has(f)){o=f;break}f=f.parentNode}}catch(a){this._onError(a)}return o??null}querySelector(l,e,c){let o;try{const a=this._find(L,l,e,c);a.delete(this.#e),a.size&&([o]=this._sortNodes(a))}catch(a){this._onError(a)}return o??null}querySelectorAll(l,e,c){let o;try{const a=this._find(A,l,e,c);a.delete(this.#e),a.size&&(this.#f?o=this._sortNodes(a):o=[...a])}catch(a){this._onError(a)}return o??[]}}0&&(module.exports={Matcher});
|
|
2
2
|
//# sourceMappingURL=matcher.js.map
|