shadow-dom-selector 2.0.3 → 3.0.0
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 +62 -53
- package/dist/esm/index.d.ts +14 -15
- package/dist/esm/index.js +1 -1
- package/dist/index.d.ts +14 -15
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -119,23 +119,23 @@ asyncQuerySelectorAll('article$ div$ p')
|
|
|
119
119
|
});
|
|
120
120
|
|
|
121
121
|
// Using async dot notation
|
|
122
|
-
import {
|
|
122
|
+
import { AsyncSelector } from 'shadow-dom-selector';
|
|
123
123
|
|
|
124
|
-
const selector =
|
|
124
|
+
const selector = new AsyncSelector();
|
|
125
125
|
|
|
126
|
-
selector.article.$.div.$.element
|
|
126
|
+
selector.query('article').$.query('div').$.element
|
|
127
127
|
.then((shadowRoot) => {
|
|
128
128
|
// Do stuff with the shadowRoot
|
|
129
129
|
// If it is not found after all the retries, it will return null
|
|
130
130
|
});
|
|
131
131
|
|
|
132
|
-
selector.article.$.div
|
|
132
|
+
selector.query('article').$.query('div').$.query('section > h1').element
|
|
133
133
|
.then((h1) => {
|
|
134
134
|
// Do stuff with the h1 element
|
|
135
135
|
// If it is not found after all the retries, it will return null
|
|
136
136
|
});
|
|
137
137
|
|
|
138
|
-
selector.article.$.div.$.p.all
|
|
138
|
+
selector.query('article').$.query('div').$.query('p').all
|
|
139
139
|
.then((paragraphs) => {
|
|
140
140
|
// Do stuff with the paragraphs
|
|
141
141
|
// If they are not found after all the retries, it will return an empty NodeList
|
|
@@ -184,7 +184,7 @@ ShadowDomSelector.shadowRootQuerySelector;
|
|
|
184
184
|
ShadowDomSelector.asyncQuerySelector;
|
|
185
185
|
ShadowDomSelector.asyncQuerySelectorAll;
|
|
186
186
|
ShadowDomSelector.asyncShadowRootQuerySelector;
|
|
187
|
-
ShadowDomSelector.
|
|
187
|
+
ShadowDomSelector.AsyncSelector;
|
|
188
188
|
```
|
|
189
189
|
|
|
190
190
|
## API
|
|
@@ -201,8 +201,8 @@ querySelector(root, selectors): Element | null;
|
|
|
201
201
|
|
|
202
202
|
| Parameter | Optional | Description |
|
|
203
203
|
| ------------ | ------------- | -------------------------------------------------- |
|
|
204
|
-
| selectors
|
|
205
|
-
| root
|
|
204
|
+
| `selectors` | no | A string containing one or more selectors to match. Selectors must end in a Shadow DOM (`$`) |
|
|
205
|
+
| `root` | yes | The element from where the query should be performed, it defaults to `document` |
|
|
206
206
|
|
|
207
207
|
#### querySelectorAll
|
|
208
208
|
|
|
@@ -216,8 +216,8 @@ querySelectorAll(root, selectors): NodeListOf<Element>;
|
|
|
216
216
|
|
|
217
217
|
| Parameter | Optional | Description |
|
|
218
218
|
| ------------ | ------------- | -------------------------------------------------- |
|
|
219
|
-
| selectors
|
|
220
|
-
| root
|
|
219
|
+
| `selectors` | no | A string containing one or more selectors to match. Selectors must end in a Shadow DOM (`$`) |
|
|
220
|
+
| `root` | yes | The element from where the query should be performed, it defaults to `document` |
|
|
221
221
|
|
|
222
222
|
#### shadowRootQuerySelector
|
|
223
223
|
|
|
@@ -231,8 +231,8 @@ shadowRootQuerySelector(root, selectors): ShadowRoot | null;
|
|
|
231
231
|
|
|
232
232
|
| Parameter | Optional | Description |
|
|
233
233
|
| ------------ | ------------- | -------------------------------------------------- |
|
|
234
|
-
| selectors
|
|
235
|
-
| root
|
|
234
|
+
| `selectors` | no | A string containing one or more selectors to match. Selectors must end in a Shadow DOM (`$`) |
|
|
235
|
+
| `root` | yes | The element from where the query should be performed, it defaults to `document` |
|
|
236
236
|
|
|
237
237
|
#### asyncQuerySelector
|
|
238
238
|
|
|
@@ -252,11 +252,11 @@ asyncQuerySelector(selectors, asyncParams): Promise<Element | null>;
|
|
|
252
252
|
asyncQuerySelector(root, selectors, asyncParams): Promise<Element | null>;
|
|
253
253
|
```
|
|
254
254
|
|
|
255
|
-
| Parameter
|
|
256
|
-
|
|
|
257
|
-
| selectors
|
|
258
|
-
| root
|
|
259
|
-
| asyncParams
|
|
255
|
+
| Parameter | Optional | Description |
|
|
256
|
+
| ------------- | ------------- | -------------------------------------------------- |
|
|
257
|
+
| `selectors` | no | A string containing one or more selectors to match. Selectors must end in a Shadow DOM (`$`) |
|
|
258
|
+
| `root` | yes | The element from where the query should be performed, it defaults to `document` |
|
|
259
|
+
| `asyncParams` | yes | An object containing the parameters which control the retries |
|
|
260
260
|
|
|
261
261
|
```typescript
|
|
262
262
|
// asyncParams properties
|
|
@@ -284,11 +284,11 @@ asyncQuerySelectorAll(selectors, asyncParams): Promise<NodeListOf<Element>>;
|
|
|
284
284
|
asyncQuerySelectorAll(root, selectors, asyncParams): Promise<NodeListOf<Element>>;
|
|
285
285
|
```
|
|
286
286
|
|
|
287
|
-
| Parameter
|
|
288
|
-
|
|
|
289
|
-
| selectors
|
|
290
|
-
| root
|
|
291
|
-
| asyncParams
|
|
287
|
+
| Parameter | Optional | Description |
|
|
288
|
+
| ------------- | ------------- | -------------------------------------------------- |
|
|
289
|
+
| `selectors` | no | A string containing one or more selectors to match. Selectors must end in a Shadow DOM (`$`) |
|
|
290
|
+
| `root` | yes | The element from where the query should be performed, it defaults to `document` |
|
|
291
|
+
| `asyncParams` | yes | An object containing the parameters which control the retries |
|
|
292
292
|
|
|
293
293
|
```typescript
|
|
294
294
|
// asyncParams properties
|
|
@@ -316,11 +316,11 @@ asyncShadowRootQuerySelector(selectors, asyncParams): Promise<ShadowRoot | null>
|
|
|
316
316
|
asyncShadowRootQuerySelector(root, selectors, asyncParams): Promise<ShadowRoot | null>;
|
|
317
317
|
```
|
|
318
318
|
|
|
319
|
-
| Parameter
|
|
320
|
-
|
|
|
321
|
-
| selectors
|
|
322
|
-
| root
|
|
323
|
-
| asyncParams
|
|
319
|
+
| Parameter | Optional | Description |
|
|
320
|
+
| ------------- | ------------- | -------------------------------------------------- |
|
|
321
|
+
| `selectors` | no | A string containing one or more selectors to match. Selectors must end in a Shadow DOM (`$`) |
|
|
322
|
+
| `root` | yes | The element from where the query should be performed, it defaults to `document` |
|
|
323
|
+
| `asyncParams` | yes | An object containing the parameters which control the retries |
|
|
324
324
|
|
|
325
325
|
```typescript
|
|
326
326
|
// asyncParams properties
|
|
@@ -330,20 +330,24 @@ asyncShadowRootQuerySelector(root, selectors, asyncParams): Promise<ShadowRoot |
|
|
|
330
330
|
}
|
|
331
331
|
```
|
|
332
332
|
|
|
333
|
-
####
|
|
333
|
+
#### AsyncSelector class
|
|
334
334
|
|
|
335
335
|
```typescript
|
|
336
|
-
|
|
336
|
+
new AsyncSelector();
|
|
337
337
|
```
|
|
338
338
|
|
|
339
339
|
```typescript
|
|
340
|
-
|
|
340
|
+
new AsyncSelector(root);
|
|
341
341
|
```
|
|
342
342
|
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
343
|
+
```typescript
|
|
344
|
+
new AsyncSelector(root, asyncParams);
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
| Parameter | Optional | Description |
|
|
348
|
+
| ------------- | ------------- | -------------------------------------------------- |
|
|
349
|
+
| `root` | yes | The element or shadowRoot from where the query should be performed, it defaults to `document` |
|
|
350
|
+
| `asyncParams` | yes | An object containing the parameters which control the retries |
|
|
347
351
|
|
|
348
352
|
```typescript
|
|
349
353
|
// asyncParams properties
|
|
@@ -353,37 +357,42 @@ buildAsyncSelector(root, asyncParams): AsyncSelectorProxy;
|
|
|
353
357
|
}
|
|
354
358
|
```
|
|
355
359
|
|
|
356
|
-
|
|
360
|
+
The instances of this class have the next properties:
|
|
357
361
|
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
362
|
+
| Property | Type | Description |
|
|
363
|
+
| ---------------- | ---------------------------------------- | ---------------------------------------------------------------- |
|
|
364
|
+
| `element` | `Promise<Element \| ShadowRoot \| null>` | A promise that resolves in the queried element |
|
|
365
|
+
| `all` | `Promise<NodeListOf<Element>>` | A promise that resolves in a Nodelist with all queried elements |
|
|
366
|
+
| `$` | `Promise<ShadowRoot \| null>` | A promise that resolves in the shadowRoot of the queried element |
|
|
367
|
+
| `asyncParams` | Same `asyncParams` previously shown | An object containing the parameters which control the retries |
|
|
368
|
+
|
|
369
|
+
And the next methods:
|
|
370
|
+
|
|
371
|
+
| Method | Return | Description |
|
|
372
|
+
| ------------------------- | -------------------------- | ---------------------------------------------------------------- |
|
|
373
|
+
| `eq(index: number)` | `Promise<Element \| null>` | Returns a promise that resolves in the element in the index position of the queried elements (startig from `0`) |
|
|
374
|
+
| `query(selector: string)` | `AsyncSelector` | Performs a query and returns a new AsyncSelector instance |
|
|
368
375
|
|
|
369
|
-
##### Examples of
|
|
376
|
+
##### Examples of the AsyncSelector class
|
|
370
377
|
|
|
371
378
|
```typescript
|
|
372
|
-
const selector =
|
|
379
|
+
const selector = new AsyncSelector(); // Starting to query in the document with the default asyncParams
|
|
373
380
|
await selector.element === document;
|
|
374
381
|
await selector.all; // Empty Node list
|
|
375
382
|
await selector.$; // null
|
|
383
|
+
await selector.eq(0); // null
|
|
376
384
|
```
|
|
377
385
|
|
|
378
386
|
```typescript
|
|
379
|
-
const selector =
|
|
387
|
+
const selector = AsyncSelector({
|
|
380
388
|
retries: 100,
|
|
381
389
|
delay: 50
|
|
382
|
-
}); //
|
|
383
|
-
await selector.section.$.element === document.querySelector('section').shadowRoot;
|
|
384
|
-
await selector.section.$.all; // Empty Node list
|
|
385
|
-
await selector.section.$.article.all === document.querySelector('section').shadowRoot.querySelectorAll('article');
|
|
386
|
-
selector.section.$.
|
|
390
|
+
}); // Starting to query in the document with custom asyncParams
|
|
391
|
+
await selector.query('section').$.element === document.querySelector('section').shadowRoot;
|
|
392
|
+
await selector.query('section').$.all; // Empty Node list
|
|
393
|
+
await selector.query('section').$.query('article').all === document.querySelector('section').shadowRoot.querySelectorAll('article');
|
|
394
|
+
await selector.query('section').$.query('ul li').eq(1) === document.querySelector('section').shadowRoot.querySelectorAll('ul li')[1];
|
|
395
|
+
selector.query('section').$.query('article').asyncParams; // { retries: 100, delay: 50 }
|
|
387
396
|
```
|
|
388
397
|
|
|
389
398
|
[Shadow DOM]: https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_shadow_DOM
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -2,17 +2,7 @@ interface AsyncParams {
|
|
|
2
2
|
retries?: number;
|
|
3
3
|
delay?: number;
|
|
4
4
|
}
|
|
5
|
-
|
|
6
|
-
_element: Document | Element | ShadowRoot | Promise<NodeListOf<Element> | ShadowRoot | null>;
|
|
7
|
-
asyncParams: AsyncParams;
|
|
8
|
-
};
|
|
9
|
-
type AsyncSelectorInstance = Exclude<AsyncSelectorBase, "_element"> & {
|
|
10
|
-
element: Promise<Document | Element | ShadowRoot | null>;
|
|
11
|
-
all: Promise<NodeListOf<Element>>;
|
|
12
|
-
};
|
|
13
|
-
type AsyncSelectorProxy = AsyncSelectorInstance & {
|
|
14
|
-
[prop: string]: AsyncSelectorProxy;
|
|
15
|
-
};
|
|
5
|
+
declare const SHADOW_ROOT_SELECTOR = "$";
|
|
16
6
|
declare function querySelector<E extends Element = Element>(root: Document | Element, selectors: string): E | null;
|
|
17
7
|
declare function querySelector<E extends Element = Element>(selectors: string): E | null;
|
|
18
8
|
declare function querySelectorAll<E extends Element = Element>(root: Document | Element, selectors: string): NodeListOf<E>;
|
|
@@ -25,7 +15,16 @@ declare function asyncQuerySelectorAll<E extends Element = Element>(root: Docume
|
|
|
25
15
|
declare function asyncQuerySelectorAll<E extends Element = Element>(selectors: string, asyncParams?: AsyncParams): Promise<NodeListOf<E>>;
|
|
26
16
|
declare function asyncShadowRootQuerySelector(root: Document | Element, selectors: string, asyncParams?: AsyncParams): Promise<ShadowRoot | null>;
|
|
27
17
|
declare function asyncShadowRootQuerySelector(selectors: string, asyncParams?: AsyncParams): Promise<ShadowRoot | null>;
|
|
28
|
-
declare
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
18
|
+
declare class AsyncSelector<T extends Document | Element | ShadowRoot> {
|
|
19
|
+
#private;
|
|
20
|
+
constructor(asyncParams?: AsyncParams);
|
|
21
|
+
constructor(root?: T | Promise<T | NodeListOf<Element>>, asyncParams?: AsyncParams);
|
|
22
|
+
get element(): Promise<T | null>;
|
|
23
|
+
get [SHADOW_ROOT_SELECTOR](): AsyncSelector<ShadowRoot>;
|
|
24
|
+
get all(): Promise<NodeListOf<Element>>;
|
|
25
|
+
get asyncParams(): AsyncParams;
|
|
26
|
+
eq(index: number): Promise<Element | null>;
|
|
27
|
+
query(selector: string): AsyncSelector<Element>;
|
|
28
|
+
}
|
|
29
|
+
export { querySelector, querySelectorAll, shadowRootQuerySelector, asyncQuerySelector, asyncQuerySelectorAll, asyncShadowRootQuerySelector, AsyncSelector };
|
|
30
|
+
export type { AsyncParams };
|
package/dist/esm/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var n=function(){return n=Object.assign||function(n){for(var e,t=1,r=arguments.length;t<r;t++)for(var o in e=arguments[t])Object.prototype.hasOwnProperty.call(e,o)&&(n[o]=e[o]);return n},n.apply(this,arguments)};function e(n,e,t,r){return new(t||(t=Promise))((function(o,u){function a(n){try{c(r.next(n))}catch(n){u(n)}}function l(n){try{c(r.throw(n))}catch(n){u(n)}}function c(n){var e;n.done?o(n.value):(e=n.value,e instanceof t?e:new t((function(n){n(e)}))).then(a,l)}c((r=r.apply(n,e||[])).next())}))}function t(n,e){var t,r,o,u,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return u={next:l(0),throw:l(1),return:l(2)},"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function l(l){return function(c){return function(l){if(t)throw new TypeError("Generator is already executing.");for(;u&&(u=0,l[0]&&(a=0)),a;)try{if(t=1,r&&(o=2&l[0]?r.return:l[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,l[1])).done)return o;switch(r=0,o&&(l=[2&l[0],o.value]),l[0]){case 0:case 1:o=l;break;case 4:return a.label++,{value:l[1],done:!1};case 5:a.label++,r=l[1],l=[0];continue;case 7:l=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==l[0]&&2!==l[0])){a=0;continue}if(3===l[0]&&(!o||l[1]>o[0]&&l[1]<o[3])){a.label=l[1];break}if(6===l[0]&&a.label<o[1]){a.label=o[1],o=l;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(l);break}o[2]&&a.ops.pop(),a.trys.pop();continue}l=e.call(n,a)}catch(n){l=[6,n],r=0}finally{t=o=0}if(5&l[0])throw l[1];return{value:l[0]?l[1]:void 0,done:!0}}([l,c])}}}function r(n,e,t){if(t||2===arguments.length)for(var r,o=0,u=e.length;o<u;o++)!r&&o in e||(r||(r=Array.prototype.slice.call(e,0,o)),r[o]=e[o]);return n.concat(r||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError;var o,u="$",a=":host",l="invalid selector",c=10,i=10;!function(n){n.ALL="all",n.ELEMENT="element",n.PARAMS="asyncParams"}(o||(o={}));var s=function(n){var e,t=n[0],r=n[1];return(e=t)&&(e instanceof Document||e instanceof Element)&&"string"==typeof r};function f(n,e){return function(n){return n.split(",").map((function(n){return n.trim()}))}(n).map((function(n){var t=function(n){return n.split(u).map((function(n){return n.trim()}))}(n);return e(t)}))}function h(n,e,t,r,o){return void 0===o&&(o=!1),new Promise((function(u){var a=0,c=function(){var i=o?n.querySelectorAll(e):n.querySelector(e);o&&i.length||!o&&null!==i?u(i):++a<t?setTimeout(c,r):u(o?document.querySelectorAll(l):null)};c()}))}function d(n,e,t){return new Promise((function(r){var o=0,u=function(){var a=n.shadowRoot;a?r(a):++o<e?setTimeout(u,t):r(null)};u()}))}function v(n,e){var t=e?" If you want to select a shadowRoot, use ".concat(e," instead."):"";return"".concat(n," cannot be used with a selector ending in a shadowRoot (").concat(u,").").concat(t)}function y(n,e){return"".concat(n," must be used with a selector ending in a shadowRoot (").concat(u,"). If you don't want to select a shadowRoot, use ").concat(e," instead.")}function w(n){return n instanceof Promise?n:Promise.resolve(n)}function m(){return"You can not select a shadowRoot (".concat(u,") of the document.")}function g(n,e){for(var t,r,o=null,u=n.length,l=0;l<u;l++){if(0===l)if(n[l].length)o=e.querySelector(n[l]);else{if(e instanceof Document)throw new SyntaxError(m());o=(null===(t=e.shadowRoot)||void 0===t?void 0:t.querySelector(n[++l]))||null}else o=(null===(r=o.shadowRoot)||void 0===r?void 0:r.querySelector("".concat(a," ").concat(n[l])))||null;if(null===o)return null}return o}function p(n,e){var t,o=r([],n,!0),u=o.pop();return o.length?(null===(t=g(o,e).shadowRoot)||void 0===t?void 0:t.querySelectorAll("".concat(a," ").concat(u)))||null:e.querySelectorAll(u)}function b(n,e){if(1===n.length&&!n[0].length){if(e instanceof Document)throw new SyntaxError(m());return e.shadowRoot}var t=g(n,e);return(null==t?void 0:t.shadowRoot)||null}function S(n,r,o,u){return e(this,void 0,void 0,(function(){var e,l,c,i,s,f;return t(this,(function(t){switch(t.label){case 0:e=null,l=n.length,c=0,t.label=1;case 1:if(!(c<l))return[3,15];if(0!==c)return[3,8];if(n[c].length)return[3,5];if(r instanceof Document)throw new SyntaxError(m());return r.shadowRoot?[4,h(r.shadowRoot,n[++c],o,u)]:[3,3];case 2:return i=t.sent(),[3,4];case 3:i=null,t.label=4;case 4:return e=i,[3,7];case 5:return[4,h(r,n[c],o,u)];case 6:e=t.sent(),t.label=7;case 7:return[3,13];case 8:return[4,d(e,o,u)];case 9:return(s=t.sent())?[4,h(s,"".concat(a," ").concat(n[c]),o,u)]:[3,11];case 10:return f=t.sent(),[3,12];case 11:f=null,t.label=12;case 12:e=f,t.label=13;case 13:if(null===e)return[2,null];t.label=14;case 14:return c++,[3,1];case 15:return[2,e]}}))}))}function P(n,o,u,l){return e(this,void 0,void 0,(function(){var e,c,i,s,f,v;return t(this,(function(t){switch(t.label){case 0:return e=r([],n,!0),c=e.pop(),e.length?[3,2]:[4,h(o,c,u,l,!0)];case 1:return[2,t.sent()];case 2:return[4,S(e,o,u,l)];case 3:return(i=t.sent())?[4,d(i,u,l)]:[3,5];case 4:return f=t.sent(),[3,6];case 5:f=null,t.label=6;case 6:return(s=f)?[4,h(s,"".concat(a," ").concat(c),u,l,!0)]:[3,8];case 7:return v=t.sent(),[3,9];case 8:v=null,t.label=9;case 9:return[2,v]}}))}))}function E(n,r,o,u){return e(this,void 0,void 0,(function(){var e,a;return t(this,(function(t){switch(t.label){case 0:if(1===n.length&&!n[0].length){if(r instanceof Document)throw new SyntaxError(m());return[2,d(r,o,u)]}return[4,S(n,r,o,u)];case 1:return(e=t.sent())?[4,d(e,o,u)]:[3,3];case 2:return a=t.sent(),[3,4];case 3:a=null,t.label=4;case 4:return[2,a]}}))}))}function R(n,e){for(var t=f(n,(function(n){if(!n[n.length-1].length)throw new SyntaxError(v("querySelector","shadowRootQuerySelector"));return n})),r=t.length,o=0;o<r;o++){var u=g(t[o],e);if(u)return u}return null}function x(n,e){for(var t=f(n,(function(n){if(!n[n.length-1].length)throw new SyntaxError(v("querySelectorAll"));return n})),r=t.length,o=0;o<r;o++){var u=p(t[o],e);if(null==u?void 0:u.length)return u}return document.querySelectorAll(l)}function A(n,e){for(var t=f(n,(function(n){if(n.pop().length)throw new SyntaxError(y("shadowRootQuerySelector","querySelector"));return n})),r=t.length,o=0;o<r;o++){var u=b(t[o],e);if(u)return u}return null}function q(n,r,o,u){return e(this,void 0,void 0,(function(){var e,a,l,c;return t(this,(function(t){switch(t.label){case 0:e=f(n,(function(n){if(!n[n.length-1].length)throw new SyntaxError(v("asyncQuerySelector","asyncShadowRootQuerySelector"));return n})),a=e.length,l=0,t.label=1;case 1:return l<a?[4,S(e[l],r,o,u)]:[3,4];case 2:if(c=t.sent())return[2,c];t.label=3;case 3:return l++,[3,1];case 4:return[2,null]}}))}))}function L(n,r,o,u){return e(this,void 0,void 0,(function(){var e,a,c,i;return t(this,(function(t){switch(t.label){case 0:e=f(n,(function(n){if(!n[n.length-1].length)throw new SyntaxError(v("asyncQuerySelectorAll"));return n})),a=e.length,c=0,t.label=1;case 1:return c<a?[4,P(e[c],r,o,u)]:[3,4];case 2:if(null==(i=t.sent())?void 0:i.length)return[2,i];t.label=3;case 3:return c++,[3,1];case 4:return[2,document.querySelectorAll(l)]}}))}))}function N(n,r,o,u){return e(this,void 0,void 0,(function(){var e,a,l,c;return t(this,(function(t){switch(t.label){case 0:e=f(n,(function(n){if(n.pop().length)throw new SyntaxError(y("asyncShadowRootQuerySelector","asyncQuerySelector"));return n})),a=e.length,l=0,t.label=1;case 1:return l<a?[4,E(e[l],r,o,u)]:[3,4];case 2:if(c=t.sent())return[2,c];t.label=3;case 3:return l++,[3,1];case 4:return[2,null]}}))}))}function _(){for(var n=[],e=0;e<arguments.length;e++)n[e]=arguments[e];var t=n[0],r=n[1];return"string"==typeof t?R(t,document):R(r,t)}function Q(){for(var n=[],e=0;e<arguments.length;e++)n[e]=arguments[e];var t=n[0],r=n[1];return"string"==typeof t?x(t,document):x(r,t)}function D(){for(var n=[],e=0;e<arguments.length;e++)n[e]=arguments[e];var t=n[0],r=n[1];return"string"==typeof t?A(t,document):A(r,t)}function T(){for(var n=[],r=0;r<arguments.length;r++)n[r]=arguments[r];return e(this,void 0,void 0,(function(){var e,r,o,u,a;return t(this,(function(t){switch(t.label){case 0:return s(n)?(e=n[0],r=n[1],o=n[2],[4,q(r,e,(null==o?void 0:o.retries)||c,(null==o?void 0:o.delay)||i)]):[3,2];case 1:case 3:return[2,t.sent()];case 2:return u=n[0],a=n[1],[4,q(u,document,(null==a?void 0:a.retries)||c,(null==a?void 0:a.delay)||i)]}}))}))}function k(){for(var n=[],r=0;r<arguments.length;r++)n[r]=arguments[r];return e(this,void 0,void 0,(function(){var e,r,o,u,a;return t(this,(function(t){switch(t.label){case 0:return s(n)?(e=n[0],r=n[1],o=n[2],[4,L(r,e,(null==o?void 0:o.retries)||c,(null==o?void 0:o.delay)||i)]):[3,2];case 1:return[2,t.sent()];case 2:return u=n[0],a=n[1],[2,L(u,document,(null==a?void 0:a.retries)||c,(null==a?void 0:a.delay)||i)]}}))}))}function M(){for(var n=[],r=0;r<arguments.length;r++)n[r]=arguments[r];return e(this,void 0,void 0,(function(){var e,r,o,u,a;return t(this,(function(t){switch(t.label){case 0:return s(n)?(e=n[0],r=n[1],o=n[2],[4,N(r,e,(null==o?void 0:o.retries)||c,(null==o?void 0:o.delay)||i)]):[3,2];case 1:return[2,t.sent()];case 2:return u=n[0],a=n[1],[2,N(u,document,(null==a?void 0:a.retries)||c,(null==a?void 0:a.delay)||i)]}}))}))}function O(e,t){if(e instanceof Node){var r=n({retries:c,delay:i},t||{});return j({_element:e,asyncParams:r})}var o=n({retries:c,delay:i},e||{});return j({_element:document,asyncParams:o})}var j=function(n){return new Proxy(n,{get:function(n,e){if(e===o.PARAMS)return n[e];if(e===o.ELEMENT)return w(n._element).then((function(n){return n instanceof NodeList?n[0]||null:n}));if(e===o.ALL)return w(n._element).then((function(n){return n instanceof NodeList?n:document.querySelectorAll(l)}));if(e===u){var t=w(n._element).then((function(e){return e instanceof ShadowRoot||null===e||e instanceof NodeList&&0===e.length?null:e instanceof NodeList?d(e[0],n.asyncParams.retries,n.asyncParams.delay):d(e,n.asyncParams.retries,n.asyncParams.delay)}));return j({_element:t,asyncParams:n.asyncParams})}var r=w(n._element).then((function(t){return null===t||t instanceof NodeList&&0===t.length?null:t instanceof NodeList?h(t[0],e,n.asyncParams.retries,n.asyncParams.delay,!0):h(t,e,n.asyncParams.retries,n.asyncParams.delay,!0)}));return j({_element:r,asyncParams:n.asyncParams})}})};export{T as asyncQuerySelector,k as asyncQuerySelectorAll,M as asyncShadowRootQuerySelector,O as buildAsyncSelector,_ as querySelector,Q as querySelectorAll,D as shadowRootQuerySelector};
|
|
1
|
+
function t(t,n,e,o){return new(e||(e=Promise))((function(r,i){function l(t){try{s(o.next(t))}catch(t){i(t)}}function u(t){try{s(o.throw(t))}catch(t){i(t)}}function s(t){var n;t.done?r(t.value):(n=t.value,n instanceof e?n:new e((function(t){t(n)}))).then(l,u)}s((o=o.apply(t,n||[])).next())}))}function n(t,n,e,o){if("a"===e&&!o)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof n?t!==n||!o:!n.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===e?o:"a"===e?o.call(t):o?o.value:n.get(t)}function e(t,n,e,o,r){if("m"===o)throw new TypeError("Private method is not writable");if("a"===o&&!r)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof n?t!==n||!r:!n.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===o?r.call(t,e):r?r.value=e:n.set(t,e),e}"function"==typeof SuppressedError&&SuppressedError;const o="$",r=":host",i="invalid selector",l=10,u=10,s=t=>{const[n,e]=t;return(o=n)&&(o instanceof Document||o instanceof Element)&&"string"==typeof e;var o};function c(t,n){return function(t){return t.split(",").map((t=>t.trim()))}(t).map((t=>{const e=function(t){return t.split(o).map((t=>t.trim()))}(t);return n(e)}))}function f(t,n,e,o,r=!1){return new Promise((l=>{let u=0;const s=()=>{const c=r?t.querySelectorAll(n):t.querySelector(n);r&&c.length||!r&&null!==c?l(c):(u++,u<e?setTimeout(s,o):l(r?document.querySelectorAll(i):null))};s()}))}function a(t,n,e){return new Promise((o=>{let r=0;const i=()=>{const l=t.shadowRoot;l?o(l):(r++,r<n?setTimeout(i,e):o(null))};i()}))}function d(t,n){return`${t} cannot be used with a selector ending in a shadowRoot (${o}).${n?` If you want to select a shadowRoot, use ${n} instead.`:""}`}function h(t,n){return`${t} must be used with a selector ending in a shadowRoot (${o}). If you don't want to select a shadowRoot, use ${n} instead.`}function y(t){return t instanceof Promise?t:Promise.resolve(t)}function w(){return`You can not select a shadowRoot (${o}) of the document.`}function v(t,n){var e,o;let i=null;const l=t.length;for(let u=0;u<l;u++){if(0===u)if(t[u].length)i=n.querySelector(t[u]);else{if(n instanceof Document)throw new SyntaxError(w());i=(null===(e=n.shadowRoot)||void 0===e?void 0:e.querySelector(t[++u]))||null}else i=(null===(o=i.shadowRoot)||void 0===o?void 0:o.querySelector(`${r} ${t[u]}`))||null;if(null===i)return null}return i}function g(t,n){var e;const o=[...t],i=o.pop();if(!o.length)return n.querySelectorAll(i);return(null===(e=v(o,n).shadowRoot)||void 0===e?void 0:e.querySelectorAll(`${r} ${i}`))||null}function m(t,n){if(1===t.length&&!t[0].length){if(n instanceof Document)throw new SyntaxError(w());return n.shadowRoot}const e=v(t,n);return(null==e?void 0:e.shadowRoot)||null}function p(n,e,o,i){return t(this,void 0,void 0,(function*(){let t=null;const l=n.length;for(let u=0;u<l;u++){if(0===u)if(n[u].length)t=yield f(e,n[u],o,i);else{if(e instanceof Document)throw new SyntaxError(w());t=e.shadowRoot?yield f(e.shadowRoot,n[++u],o,i):null}else{const e=yield a(t,o,i);t=e?yield f(e,`${r} ${n[u]}`,o,i):null}if(null===t)return null}return t}))}function S(n,e,o,i){return t(this,void 0,void 0,(function*(){const t=[...n],l=t.pop();if(!t.length)return yield f(e,l,o,i,!0);const u=yield p(t,e,o,i),s=u?yield a(u,o,i):null;return s?yield f(s,`${r} ${l}`,o,i,!0):null}))}function E(n,e,o,r){return t(this,void 0,void 0,(function*(){if(1===n.length&&!n[0].length){if(e instanceof Document)throw new SyntaxError(w());return a(e,o,r)}const t=yield p(n,e,o,r);return t?yield a(t,o,r):null}))}function R(t,n){const e=c(t,(t=>{if(!t[t.length-1].length)throw new SyntaxError(d("querySelector","shadowRootQuerySelector"));return t})),o=e.length;for(let t=0;t<o;t++){const o=v(e[t],n);if(o)return o}return null}function $(t,n){const e=c(t,(t=>{if(!t[t.length-1].length)throw new SyntaxError(d("querySelectorAll"));return t})),o=e.length;for(let t=0;t<o;t++){const o=g(e[t],n);if(null==o?void 0:o.length)return o}return document.querySelectorAll(i)}function q(t,n){const e=c(t,(t=>{if(t.pop().length)throw new SyntaxError(h("shadowRootQuerySelector","querySelector"));return t})),o=e.length;for(let t=0;t<o;t++){const o=m(e[t],n);if(o)return o}return null}function x(n,e,o,r){return t(this,void 0,void 0,(function*(){const t=c(n,(t=>{if(!t[t.length-1].length)throw new SyntaxError(d("asyncQuerySelector","asyncShadowRootQuerySelector"));return t})),i=t.length;for(let n=0;n<i;n++){const i=yield p(t[n],e,o,r);if(i)return i}return null}))}function P(n,e,o,r){return t(this,void 0,void 0,(function*(){const t=c(n,(t=>{if(!t[t.length-1].length)throw new SyntaxError(d("asyncQuerySelectorAll"));return t})),l=t.length;for(let n=0;n<l;n++){const i=yield S(t[n],e,o,r);if(null==i?void 0:i.length)return i}return document.querySelectorAll(i)}))}function b(n,e,o,r){return t(this,void 0,void 0,(function*(){const t=c(n,(t=>{if(t.pop().length)throw new SyntaxError(h("asyncShadowRootQuerySelector","asyncQuerySelector"));return t})),i=t.length;for(let n=0;n<i;n++){const i=yield E(t[n],e,o,r);if(i)return i}return null}))}var A,N;function L(...t){const[n,e]=t;return"string"==typeof n?R(n,document):R(e,n)}function Q(...t){const[n,e]=t;return"string"==typeof n?$(n,document):$(e,n)}function T(...t){const[n,e]=t;return"string"==typeof n?q(n,document):q(e,n)}function D(...n){return t(this,void 0,void 0,(function*(){if(s(n)){const[t,e,o]=n;return yield x(e,t,(null==o?void 0:o.retries)||l,(null==o?void 0:o.delay)||u)}const[t,e]=n;return yield x(t,document,(null==e?void 0:e.retries)||l,(null==e?void 0:e.delay)||u)}))}function j(...n){return t(this,void 0,void 0,(function*(){if(s(n)){const[t,e,o]=n;return yield P(e,t,(null==o?void 0:o.retries)||l,(null==o?void 0:o.delay)||u)}const[t,e]=n;return P(t,document,(null==e?void 0:e.retries)||l,(null==e?void 0:e.delay)||u)}))}function k(...n){return t(this,void 0,void 0,(function*(){if(s(n)){const[t,e,o]=n;return yield b(e,t,(null==o?void 0:o.retries)||l,(null==o?void 0:o.delay)||u)}const[t,e]=n;return b(t,document,(null==e?void 0:e.retries)||l,(null==e?void 0:e.delay)||u)}))}class C{constructor(t,n){A.set(this,void 0),N.set(this,void 0),t instanceof Node||t instanceof Promise?(e(this,A,t,"f"),e(this,N,Object.assign({retries:l,delay:u},n||{}),"f")):(e(this,A,document,"f"),e(this,N,Object.assign({retries:l,delay:u},t||{}),"f"))}get element(){return y(n(this,A,"f")).then((t=>t instanceof NodeList?t[0]||null:t))}get[(A=new WeakMap,N=new WeakMap,o)](){const t=y(n(this,A,"f")).then((t=>t instanceof Document||t instanceof ShadowRoot||null===t||t instanceof NodeList&&0===t.length?null:t instanceof NodeList?a(t[0],n(this,N,"f").retries,n(this,N,"f").delay):a(t,n(this,N,"f").retries,n(this,N,"f").delay)));return new C(t,n(this,N,"f"))}get all(){return y(n(this,A,"f")).then((t=>t instanceof NodeList?t:document.querySelectorAll(i)))}get asyncParams(){return n(this,N,"f")}eq(e){return t(this,void 0,void 0,(function*(){return y(n(this,A,"f")).then((t=>t instanceof NodeList&&t[e]||null))}))}query(t){const e=y(n(this,A,"f")).then((e=>null===e||e instanceof NodeList&&0===e.length?null:e instanceof NodeList?f(e[0],t,n(this,N,"f").retries,n(this,N,"f").delay,!0):f(e,t,n(this,N,"f").retries,n(this,N,"f").delay,!0)));return new C(e,n(this,N,"f"))}}export{C as AsyncSelector,D as asyncQuerySelector,j as asyncQuerySelectorAll,k as asyncShadowRootQuerySelector,L as querySelector,Q as querySelectorAll,T as shadowRootQuerySelector};
|
package/dist/index.d.ts
CHANGED
|
@@ -2,17 +2,7 @@ interface AsyncParams {
|
|
|
2
2
|
retries?: number;
|
|
3
3
|
delay?: number;
|
|
4
4
|
}
|
|
5
|
-
|
|
6
|
-
_element: Document | Element | ShadowRoot | Promise<NodeListOf<Element> | ShadowRoot | null>;
|
|
7
|
-
asyncParams: AsyncParams;
|
|
8
|
-
};
|
|
9
|
-
type AsyncSelectorInstance = Exclude<AsyncSelectorBase, "_element"> & {
|
|
10
|
-
element: Promise<Document | Element | ShadowRoot | null>;
|
|
11
|
-
all: Promise<NodeListOf<Element>>;
|
|
12
|
-
};
|
|
13
|
-
type AsyncSelectorProxy = AsyncSelectorInstance & {
|
|
14
|
-
[prop: string]: AsyncSelectorProxy;
|
|
15
|
-
};
|
|
5
|
+
declare const SHADOW_ROOT_SELECTOR = "$";
|
|
16
6
|
declare function querySelector<E extends Element = Element>(root: Document | Element, selectors: string): E | null;
|
|
17
7
|
declare function querySelector<E extends Element = Element>(selectors: string): E | null;
|
|
18
8
|
declare function querySelectorAll<E extends Element = Element>(root: Document | Element, selectors: string): NodeListOf<E>;
|
|
@@ -25,7 +15,16 @@ declare function asyncQuerySelectorAll<E extends Element = Element>(root: Docume
|
|
|
25
15
|
declare function asyncQuerySelectorAll<E extends Element = Element>(selectors: string, asyncParams?: AsyncParams): Promise<NodeListOf<E>>;
|
|
26
16
|
declare function asyncShadowRootQuerySelector(root: Document | Element, selectors: string, asyncParams?: AsyncParams): Promise<ShadowRoot | null>;
|
|
27
17
|
declare function asyncShadowRootQuerySelector(selectors: string, asyncParams?: AsyncParams): Promise<ShadowRoot | null>;
|
|
28
|
-
declare
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
18
|
+
declare class AsyncSelector<T extends Document | Element | ShadowRoot> {
|
|
19
|
+
#private;
|
|
20
|
+
constructor(asyncParams?: AsyncParams);
|
|
21
|
+
constructor(root?: T | Promise<T | NodeListOf<Element>>, asyncParams?: AsyncParams);
|
|
22
|
+
get element(): Promise<T | null>;
|
|
23
|
+
get [SHADOW_ROOT_SELECTOR](): AsyncSelector<ShadowRoot>;
|
|
24
|
+
get all(): Promise<NodeListOf<Element>>;
|
|
25
|
+
get asyncParams(): AsyncParams;
|
|
26
|
+
eq(index: number): Promise<Element | null>;
|
|
27
|
+
query(selector: string): AsyncSelector<Element>;
|
|
28
|
+
}
|
|
29
|
+
export { querySelector, querySelectorAll, shadowRootQuerySelector, asyncQuerySelector, asyncQuerySelectorAll, asyncShadowRootQuerySelector, AsyncSelector };
|
|
30
|
+
export type { AsyncParams };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).ShadowDomSelector={})}(this,(function(e){"use strict";var n=function(){return n=Object.assign||function(e){for(var n,t=1,r=arguments.length;t<r;t++)for(var o in n=arguments[t])Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o]);return e},n.apply(this,arguments)};function t(e,n,t,r){return new(t||(t=Promise))((function(o,u){function a(e){try{c(r.next(e))}catch(e){u(e)}}function l(e){try{c(r.throw(e))}catch(e){u(e)}}function c(e){var n;e.done?o(e.value):(n=e.value,n instanceof t?n:new t((function(e){e(n)}))).then(a,l)}c((r=r.apply(e,n||[])).next())}))}function r(e,n){var t,r,o,u,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return u={next:l(0),throw:l(1),return:l(2)},"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function l(l){return function(c){return function(l){if(t)throw new TypeError("Generator is already executing.");for(;u&&(u=0,l[0]&&(a=0)),a;)try{if(t=1,r&&(o=2&l[0]?r.return:l[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,l[1])).done)return o;switch(r=0,o&&(l=[2&l[0],o.value]),l[0]){case 0:case 1:o=l;break;case 4:return a.label++,{value:l[1],done:!1};case 5:a.label++,r=l[1],l=[0];continue;case 7:l=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==l[0]&&2!==l[0])){a=0;continue}if(3===l[0]&&(!o||l[1]>o[0]&&l[1]<o[3])){a.label=l[1];break}if(6===l[0]&&a.label<o[1]){a.label=o[1],o=l;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(l);break}o[2]&&a.ops.pop(),a.trys.pop();continue}l=n.call(e,a)}catch(e){l=[6,e],r=0}finally{t=o=0}if(5&l[0])throw l[1];return{value:l[0]?l[1]:void 0,done:!0}}([l,c])}}}function o(e,n,t){if(t||2===arguments.length)for(var r,o=0,u=n.length;o<u;o++)!r&&o in n||(r||(r=Array.prototype.slice.call(n,0,o)),r[o]=n[o]);return e.concat(r||Array.prototype.slice.call(n))}"function"==typeof SuppressedError&&SuppressedError;var u,a="$",l=":host",c="invalid selector",i=10,s=10;!function(e){e.ALL="all",e.ELEMENT="element",e.PARAMS="asyncParams"}(u||(u={}));var f=function(e){var n,t=e[0],r=e[1];return(n=t)&&(n instanceof Document||n instanceof Element)&&"string"==typeof r};function h(e,n){return function(e){return e.split(",").map((function(e){return e.trim()}))}(e).map((function(e){var t=function(e){return e.split(a).map((function(e){return e.trim()}))}(e);return n(t)}))}function d(e,n,t,r,o){return void 0===o&&(o=!1),new Promise((function(u){var a=0,l=function(){var i=o?e.querySelectorAll(n):e.querySelector(n);o&&i.length||!o&&null!==i?u(i):++a<t?setTimeout(l,r):u(o?document.querySelectorAll(c):null)};l()}))}function y(e,n,t){return new Promise((function(r){var o=0,u=function(){var a=e.shadowRoot;a?r(a):++o<n?setTimeout(u,t):r(null)};u()}))}function v(e,n){var t=n?" If you want to select a shadowRoot, use ".concat(n," instead."):"";return"".concat(e," cannot be used with a selector ending in a shadowRoot (").concat(a,").").concat(t)}function w(e,n){return"".concat(e," must be used with a selector ending in a shadowRoot (").concat(a,"). If you don't want to select a shadowRoot, use ").concat(n," instead.")}function m(e){return e instanceof Promise?e:Promise.resolve(e)}function p(){return"You can not select a shadowRoot (".concat(a,") of the document.")}function g(e,n){for(var t,r,o=null,u=e.length,a=0;a<u;a++){if(0===a)if(e[a].length)o=n.querySelector(e[a]);else{if(n instanceof Document)throw new SyntaxError(p());o=(null===(t=n.shadowRoot)||void 0===t?void 0:t.querySelector(e[++a]))||null}else o=(null===(r=o.shadowRoot)||void 0===r?void 0:r.querySelector("".concat(l," ").concat(e[a])))||null;if(null===o)return null}return o}function S(e,n){var t,r=o([],e,!0),u=r.pop();return r.length?(null===(t=g(r,n).shadowRoot)||void 0===t?void 0:t.querySelectorAll("".concat(l," ").concat(u)))||null:n.querySelectorAll(u)}function b(e,n){if(1===e.length&&!e[0].length){if(n instanceof Document)throw new SyntaxError(p());return n.shadowRoot}var t=g(e,n);return(null==t?void 0:t.shadowRoot)||null}function P(e,n,o,u){return t(this,void 0,void 0,(function(){var t,a,c,i,s,f;return r(this,(function(r){switch(r.label){case 0:t=null,a=e.length,c=0,r.label=1;case 1:if(!(c<a))return[3,15];if(0!==c)return[3,8];if(e[c].length)return[3,5];if(n instanceof Document)throw new SyntaxError(p());return n.shadowRoot?[4,d(n.shadowRoot,e[++c],o,u)]:[3,3];case 2:return i=r.sent(),[3,4];case 3:i=null,r.label=4;case 4:return t=i,[3,7];case 5:return[4,d(n,e[c],o,u)];case 6:t=r.sent(),r.label=7;case 7:return[3,13];case 8:return[4,y(t,o,u)];case 9:return(s=r.sent())?[4,d(s,"".concat(l," ").concat(e[c]),o,u)]:[3,11];case 10:return f=r.sent(),[3,12];case 11:f=null,r.label=12;case 12:t=f,r.label=13;case 13:if(null===t)return[2,null];r.label=14;case 14:return c++,[3,1];case 15:return[2,t]}}))}))}function R(e,n,u,a){return t(this,void 0,void 0,(function(){var t,c,i,s,f,h;return r(this,(function(r){switch(r.label){case 0:return t=o([],e,!0),c=t.pop(),t.length?[3,2]:[4,d(n,c,u,a,!0)];case 1:return[2,r.sent()];case 2:return[4,P(t,n,u,a)];case 3:return(i=r.sent())?[4,y(i,u,a)]:[3,5];case 4:return f=r.sent(),[3,6];case 5:f=null,r.label=6;case 6:return(s=f)?[4,d(s,"".concat(l," ").concat(c),u,a,!0)]:[3,8];case 7:return h=r.sent(),[3,9];case 8:h=null,r.label=9;case 9:return[2,h]}}))}))}function A(e,n,o,u){return t(this,void 0,void 0,(function(){var t,a;return r(this,(function(r){switch(r.label){case 0:if(1===e.length&&!e[0].length){if(n instanceof Document)throw new SyntaxError(p());return[2,y(n,o,u)]}return[4,P(e,n,o,u)];case 1:return(t=r.sent())?[4,y(t,o,u)]:[3,3];case 2:return a=r.sent(),[3,4];case 3:a=null,r.label=4;case 4:return[2,a]}}))}))}function E(e,n){for(var t=h(e,(function(e){if(!e[e.length-1].length)throw new SyntaxError(v("querySelector","shadowRootQuerySelector"));return e})),r=t.length,o=0;o<r;o++){var u=g(t[o],n);if(u)return u}return null}function x(e,n){for(var t=h(e,(function(e){if(!e[e.length-1].length)throw new SyntaxError(v("querySelectorAll"));return e})),r=t.length,o=0;o<r;o++){var u=S(t[o],n);if(null==u?void 0:u.length)return u}return document.querySelectorAll(c)}function q(e,n){for(var t=h(e,(function(e){if(e.pop().length)throw new SyntaxError(w("shadowRootQuerySelector","querySelector"));return e})),r=t.length,o=0;o<r;o++){var u=b(t[o],n);if(u)return u}return null}function L(e,n,o,u){return t(this,void 0,void 0,(function(){var t,a,l,c;return r(this,(function(r){switch(r.label){case 0:t=h(e,(function(e){if(!e[e.length-1].length)throw new SyntaxError(v("asyncQuerySelector","asyncShadowRootQuerySelector"));return e})),a=t.length,l=0,r.label=1;case 1:return l<a?[4,P(t[l],n,o,u)]:[3,4];case 2:if(c=r.sent())return[2,c];r.label=3;case 3:return l++,[3,1];case 4:return[2,null]}}))}))}function Q(e,n,o,u){return t(this,void 0,void 0,(function(){var t,a,l,i;return r(this,(function(r){switch(r.label){case 0:t=h(e,(function(e){if(!e[e.length-1].length)throw new SyntaxError(v("asyncQuerySelectorAll"));return e})),a=t.length,l=0,r.label=1;case 1:return l<a?[4,R(t[l],n,o,u)]:[3,4];case 2:if(null==(i=r.sent())?void 0:i.length)return[2,i];r.label=3;case 3:return l++,[3,1];case 4:return[2,document.querySelectorAll(c)]}}))}))}function N(e,n,o,u){return t(this,void 0,void 0,(function(){var t,a,l,c;return r(this,(function(r){switch(r.label){case 0:t=h(e,(function(e){if(e.pop().length)throw new SyntaxError(w("asyncShadowRootQuerySelector","asyncQuerySelector"));return e})),a=t.length,l=0,r.label=1;case 1:return l<a?[4,A(t[l],n,o,u)]:[3,4];case 2:if(c=r.sent())return[2,c];r.label=3;case 3:return l++,[3,1];case 4:return[2,null]}}))}))}var _=function(e){return new Proxy(e,{get:function(e,n){if(n===u.PARAMS)return e[n];if(n===u.ELEMENT)return m(e._element).then((function(e){return e instanceof NodeList?e[0]||null:e}));if(n===u.ALL)return m(e._element).then((function(e){return e instanceof NodeList?e:document.querySelectorAll(c)}));if(n===a){var t=m(e._element).then((function(n){return n instanceof ShadowRoot||null===n||n instanceof NodeList&&0===n.length?null:n instanceof NodeList?y(n[0],e.asyncParams.retries,e.asyncParams.delay):y(n,e.asyncParams.retries,e.asyncParams.delay)}));return _({_element:t,asyncParams:e.asyncParams})}var r=m(e._element).then((function(t){return null===t||t instanceof NodeList&&0===t.length?null:t instanceof NodeList?d(t[0],n,e.asyncParams.retries,e.asyncParams.delay,!0):d(t,n,e.asyncParams.retries,e.asyncParams.delay,!0)}));return _({_element:r,asyncParams:e.asyncParams})}})};e.asyncQuerySelector=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return t(this,void 0,void 0,(function(){var n,t,o,u,a;return r(this,(function(r){switch(r.label){case 0:return f(e)?(n=e[0],t=e[1],o=e[2],[4,L(t,n,(null==o?void 0:o.retries)||i,(null==o?void 0:o.delay)||s)]):[3,2];case 1:case 3:return[2,r.sent()];case 2:return u=e[0],a=e[1],[4,L(u,document,(null==a?void 0:a.retries)||i,(null==a?void 0:a.delay)||s)]}}))}))},e.asyncQuerySelectorAll=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return t(this,void 0,void 0,(function(){var n,t,o,u,a;return r(this,(function(r){switch(r.label){case 0:return f(e)?(n=e[0],t=e[1],o=e[2],[4,Q(t,n,(null==o?void 0:o.retries)||i,(null==o?void 0:o.delay)||s)]):[3,2];case 1:return[2,r.sent()];case 2:return u=e[0],a=e[1],[2,Q(u,document,(null==a?void 0:a.retries)||i,(null==a?void 0:a.delay)||s)]}}))}))},e.asyncShadowRootQuerySelector=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return t(this,void 0,void 0,(function(){var n,t,o,u,a;return r(this,(function(r){switch(r.label){case 0:return f(e)?(n=e[0],t=e[1],o=e[2],[4,N(t,n,(null==o?void 0:o.retries)||i,(null==o?void 0:o.delay)||s)]):[3,2];case 1:return[2,r.sent()];case 2:return u=e[0],a=e[1],[2,N(u,document,(null==a?void 0:a.retries)||i,(null==a?void 0:a.delay)||s)]}}))}))},e.buildAsyncSelector=function(e,t){if(e instanceof Node){var r=n({retries:i,delay:s},t||{});return _({_element:e,asyncParams:r})}var o=n({retries:i,delay:s},e||{});return _({_element:document,asyncParams:o})},e.querySelector=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];var t=e[0],r=e[1];return"string"==typeof t?E(t,document):E(r,t)},e.querySelectorAll=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];var t=e[0],r=e[1];return"string"==typeof t?x(t,document):x(r,t)},e.shadowRootQuerySelector=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];var t=e[0],r=e[1];return"string"==typeof t?q(t,document):q(r,t)}}));
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).ShadowDomSelector={})}(this,(function(t){"use strict";function e(t,e,n,o){return new(n||(n=Promise))((function(r,i){function l(t){try{s(o.next(t))}catch(t){i(t)}}function u(t){try{s(o.throw(t))}catch(t){i(t)}}function s(t){var e;t.done?r(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(l,u)}s((o=o.apply(t,e||[])).next())}))}function n(t,e,n,o){if("a"===n&&!o)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!o:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===n?o:"a"===n?o.call(t):o?o.value:e.get(t)}function o(t,e,n,o,r){if("m"===o)throw new TypeError("Private method is not writable");if("a"===o&&!r)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?t!==e||!r:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===o?r.call(t,n):r?r.value=n:e.set(t,n),n}"function"==typeof SuppressedError&&SuppressedError;const r="$",i=":host",l="invalid selector",u=10,s=10,c=t=>{const[e,n]=t;return(o=e)&&(o instanceof Document||o instanceof Element)&&"string"==typeof n;var o};function f(t,e){return function(t){return t.split(",").map((t=>t.trim()))}(t).map((t=>{const n=function(t){return t.split(r).map((t=>t.trim()))}(t);return e(n)}))}function d(t,e,n,o,r=!1){return new Promise((i=>{let u=0;const s=()=>{const c=r?t.querySelectorAll(e):t.querySelector(e);r&&c.length||!r&&null!==c?i(c):(u++,u<n?setTimeout(s,o):i(r?document.querySelectorAll(l):null))};s()}))}function a(t,e,n){return new Promise((o=>{let r=0;const i=()=>{const l=t.shadowRoot;l?o(l):(r++,r<e?setTimeout(i,n):o(null))};i()}))}function h(t,e){return`${t} cannot be used with a selector ending in a shadowRoot (${r}).${e?` If you want to select a shadowRoot, use ${e} instead.`:""}`}function y(t,e){return`${t} must be used with a selector ending in a shadowRoot (${r}). If you don't want to select a shadowRoot, use ${e} instead.`}function w(t){return t instanceof Promise?t:Promise.resolve(t)}function v(){return`You can not select a shadowRoot (${r}) of the document.`}function g(t,e){var n,o;let r=null;const l=t.length;for(let u=0;u<l;u++){if(0===u)if(t[u].length)r=e.querySelector(t[u]);else{if(e instanceof Document)throw new SyntaxError(v());r=(null===(n=e.shadowRoot)||void 0===n?void 0:n.querySelector(t[++u]))||null}else r=(null===(o=r.shadowRoot)||void 0===o?void 0:o.querySelector(`${i} ${t[u]}`))||null;if(null===r)return null}return r}function m(t,e){var n;const o=[...t],r=o.pop();if(!o.length)return e.querySelectorAll(r);return(null===(n=g(o,e).shadowRoot)||void 0===n?void 0:n.querySelectorAll(`${i} ${r}`))||null}function S(t,e){if(1===t.length&&!t[0].length){if(e instanceof Document)throw new SyntaxError(v());return e.shadowRoot}const n=g(t,e);return(null==n?void 0:n.shadowRoot)||null}function p(t,n,o,r){return e(this,void 0,void 0,(function*(){let e=null;const l=t.length;for(let u=0;u<l;u++){if(0===u)if(t[u].length)e=yield d(n,t[u],o,r);else{if(n instanceof Document)throw new SyntaxError(v());e=n.shadowRoot?yield d(n.shadowRoot,t[++u],o,r):null}else{const n=yield a(e,o,r);e=n?yield d(n,`${i} ${t[u]}`,o,r):null}if(null===e)return null}return e}))}function R(t,n,o,r){return e(this,void 0,void 0,(function*(){const e=[...t],l=e.pop();if(!e.length)return yield d(n,l,o,r,!0);const u=yield p(e,n,o,r),s=u?yield a(u,o,r):null;return s?yield d(s,`${i} ${l}`,o,r,!0):null}))}function q(t,n,o,r){return e(this,void 0,void 0,(function*(){if(1===t.length&&!t[0].length){if(n instanceof Document)throw new SyntaxError(v());return a(n,o,r)}const e=yield p(t,n,o,r);return e?yield a(e,o,r):null}))}function E(t,e){const n=f(t,(t=>{if(!t[t.length-1].length)throw new SyntaxError(h("querySelector","shadowRootQuerySelector"));return t})),o=n.length;for(let t=0;t<o;t++){const o=g(n[t],e);if(o)return o}return null}function $(t,e){const n=f(t,(t=>{if(!t[t.length-1].length)throw new SyntaxError(h("querySelectorAll"));return t})),o=n.length;for(let t=0;t<o;t++){const o=m(n[t],e);if(null==o?void 0:o.length)return o}return document.querySelectorAll(l)}function x(t,e){const n=f(t,(t=>{if(t.pop().length)throw new SyntaxError(y("shadowRootQuerySelector","querySelector"));return t})),o=n.length;for(let t=0;t<o;t++){const o=S(n[t],e);if(o)return o}return null}function b(t,n,o,r){return e(this,void 0,void 0,(function*(){const e=f(t,(t=>{if(!t[t.length-1].length)throw new SyntaxError(h("asyncQuerySelector","asyncShadowRootQuerySelector"));return t})),i=e.length;for(let t=0;t<i;t++){const i=yield p(e[t],n,o,r);if(i)return i}return null}))}function A(t,n,o,r){return e(this,void 0,void 0,(function*(){const e=f(t,(t=>{if(!t[t.length-1].length)throw new SyntaxError(h("asyncQuerySelectorAll"));return t})),i=e.length;for(let t=0;t<i;t++){const i=yield R(e[t],n,o,r);if(null==i?void 0:i.length)return i}return document.querySelectorAll(l)}))}function Q(t,n,o,r){return e(this,void 0,void 0,(function*(){const e=f(t,(t=>{if(t.pop().length)throw new SyntaxError(y("asyncShadowRootQuerySelector","asyncQuerySelector"));return t})),i=e.length;for(let t=0;t<i;t++){const i=yield q(e[t],n,o,r);if(i)return i}return null}))}var P,T;class N{constructor(t,e){P.set(this,void 0),T.set(this,void 0),t instanceof Node||t instanceof Promise?(o(this,P,t,"f"),o(this,T,Object.assign({retries:u,delay:s},e||{}),"f")):(o(this,P,document,"f"),o(this,T,Object.assign({retries:u,delay:s},t||{}),"f"))}get element(){return w(n(this,P,"f")).then((t=>t instanceof NodeList?t[0]||null:t))}get[(P=new WeakMap,T=new WeakMap,r)](){const t=w(n(this,P,"f")).then((t=>t instanceof Document||t instanceof ShadowRoot||null===t||t instanceof NodeList&&0===t.length?null:t instanceof NodeList?a(t[0],n(this,T,"f").retries,n(this,T,"f").delay):a(t,n(this,T,"f").retries,n(this,T,"f").delay)));return new N(t,n(this,T,"f"))}get all(){return w(n(this,P,"f")).then((t=>t instanceof NodeList?t:document.querySelectorAll(l)))}get asyncParams(){return n(this,T,"f")}eq(t){return e(this,void 0,void 0,(function*(){return w(n(this,P,"f")).then((e=>e instanceof NodeList&&e[t]||null))}))}query(t){const e=w(n(this,P,"f")).then((e=>null===e||e instanceof NodeList&&0===e.length?null:e instanceof NodeList?d(e[0],t,n(this,T,"f").retries,n(this,T,"f").delay,!0):d(e,t,n(this,T,"f").retries,n(this,T,"f").delay,!0)));return new N(e,n(this,T,"f"))}}t.AsyncSelector=N,t.asyncQuerySelector=function(...t){return e(this,void 0,void 0,(function*(){if(c(t)){const[e,n,o]=t;return yield b(n,e,(null==o?void 0:o.retries)||u,(null==o?void 0:o.delay)||s)}const[e,n]=t;return yield b(e,document,(null==n?void 0:n.retries)||u,(null==n?void 0:n.delay)||s)}))},t.asyncQuerySelectorAll=function(...t){return e(this,void 0,void 0,(function*(){if(c(t)){const[e,n,o]=t;return yield A(n,e,(null==o?void 0:o.retries)||u,(null==o?void 0:o.delay)||s)}const[e,n]=t;return A(e,document,(null==n?void 0:n.retries)||u,(null==n?void 0:n.delay)||s)}))},t.asyncShadowRootQuerySelector=function(...t){return e(this,void 0,void 0,(function*(){if(c(t)){const[e,n,o]=t;return yield Q(n,e,(null==o?void 0:o.retries)||u,(null==o?void 0:o.delay)||s)}const[e,n]=t;return Q(e,document,(null==n?void 0:n.retries)||u,(null==n?void 0:n.delay)||s)}))},t.querySelector=function(...t){const[e,n]=t;return"string"==typeof e?E(e,document):E(n,e)},t.querySelectorAll=function(...t){const[e,n]=t;return"string"==typeof e?$(e,document):$(n,e)},t.shadowRootQuerySelector=function(...t){const[e,n]=t;return"string"==typeof e?x(e,document):x(n,e)}}));
|
package/package.json
CHANGED