shadow-dom-selector 1.0.1 → 2.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 CHANGED
@@ -38,9 +38,10 @@ const shadow = document.querySelector('section').shadowRoot.querySelector('artic
38
38
  `shadow-dom-selector` allows you to do the same in the next way:
39
39
 
40
40
  ```javascript
41
+ // $ character at the end of a selector means to select its Shadom DOM
42
+
41
43
  import { querySelector, querySelectorAll, shadowRootQuerySelector } from 'shadow-dom-selector';
42
44
 
43
- // $ character at the end of a selector means to select its Shadom DOM
44
45
  const secondLi querySelector('section$ article$ ul > li');
45
46
  const allLis querySelectorAll('section$ article$ ul > li');
46
47
  const shadow = shadowRootQuerySelector('section$ article$');
@@ -56,12 +57,29 @@ With the same previous DOM tree, if we do this:
56
57
  const element = document.querySelector('article').shadowRoot.querySelector('div').shadowRoot.querySelector('section > h1');
57
58
  ```
58
59
 
59
- It will throw an error, because none of the elements in those queries exists. If you don‘t know if the elements exist or not, you will require to use [optional chaining] or wrap all the code in conditions:
60
+ It will throw an error, because none of the elements in those queries exist. If you don‘t know if the elements exist or not, you will require to wrap all the code in conditions or use [optional chaining] if your target is `ES2015` or greater:
60
61
 
61
62
  ```javascript
63
+ // With conditions
64
+ const article = document.querySelector('article');
65
+ if (article) {
66
+ const articleShadowRoot = article.shadowRoot;
67
+ if (articleShadowRoot) {
68
+ const div = articleShadowRoot.querySelector('div');
69
+ if (div) {
70
+ const shadow = div.shadowRoot;
71
+ if (shadow) {
72
+ const element = shadow.querySelector('section > h1');
73
+ const elements = shadow.querySelectorAll('p');
74
+ }
75
+ }
76
+ }
77
+ }
78
+
79
+ // With optional chaining in ES2015+
80
+ const shadow = document.querySelector('article')?.shadowRoot?.querySelector('div')?.shadowRoot;
62
81
  const element = document.querySelector('article')?.shadowRoot?.querySelector('div')?.shadowRoot?.querySelector('section > h1');
63
82
  const elements = document.querySelector('article')?.shadowRoot?.querySelector('div')?.shadowRoot?.querySelectorAll('p');
64
- const shadow = document.querySelector('article')?.shadowRoot?.querySelector('div')?.shadowRoot;
65
83
  ```
66
84
 
67
85
  Which will return `undefined` if some element doesn‘t exist. With `shadow-dom-selector`, you just need to write the query and it will return the same that is returned by the native `querySelector` and `querySelectorAll` if the query cannot be satisfied.
@@ -69,38 +87,62 @@ Which will return `undefined` if some element doesn‘t exist. With `shadow-dom-
69
87
  ```javascript
70
88
  import { querySelector, querySelectorAll, shadowRootQuerySelector } from 'shadow-dom-selector';
71
89
 
72
- const element = querySelector('article$ div$ section > h1'); // null
73
- const elements = querySelectorAll('article div$ p'); // empty NodeList
74
90
  const shadow = shadowRootQuerySelector('article$ div$'); // null
91
+ const element = querySelector('article$ div$ section > h1'); // null
92
+ const elements = querySelectorAll('article$ div$ p'); // empty NodeList
75
93
  ```
76
94
 
77
95
  ### Async queries
78
96
 
79
- If the elements are not already rendered into the DOM in the moment that the query is made you will receive `null`. `shadow-dom-selector` allows it to wait for the elements to appear, allowing you to decide how many times it will try to query for the element before giving up and returning `null` or an empty `NodeList`.
97
+ If the elements are not already rendered into the DOM in the moment that the query is made you will receive `null`. `shadow-dom-selector` allows you to wait for the elements to appear deciding how many times it will try to query for the element before giving up and returning `null` or an empty `NodeList`.
80
98
 
81
99
  ```javascript
100
+ // Using the async methods
82
101
  import { asyncQuerySelector, asyncQuerySelectorAll, asyncShadowRootQuerySelector } from 'shadow-dom-selector';
83
102
 
84
- const element = asyncQuerySelector('article$ div$ section > h1')
85
- .then((h1) => {
86
- // Do stuff with the h1 element
87
- // If it is not found after all the retries, it will return null
88
- });
89
-
90
- const elements = asyncQuerySelectorAll('article div$ p')
91
- .then((paragraphs) => {
92
- // Do stuff with the paragraphs
93
- // If they are not found after all the retries, it will return an empty NodeList
94
- });
95
-
96
- const shadow = asyncShadowRootQuerySelector('article$ div$')
97
- .then((shadowRoot) => {
98
- // Do stuff with the shadowRoot
99
- // If it is not found after all the retries, it will return null
100
- });
103
+ asyncShadowRootQuerySelector('article$ div$')
104
+ .then((shadowRoot) => {
105
+ // Do stuff with the shadowRoot
106
+ // If it is not found after all the retries, it will return null
107
+ });
108
+
109
+ asyncQuerySelector('article$ div$ section > h1')
110
+ .then((h1) => {
111
+ // Do stuff with the h1 element
112
+ // If it is not found after all the retries, it will return null
113
+ });
114
+
115
+ asyncQuerySelectorAll('article$ div$ p')
116
+ .then((paragraphs) => {
117
+ // Do stuff with the paragraphs
118
+ // If they are not found after all the retries, it will return an empty NodeList
119
+ });
120
+
121
+ // Using async dot notation
122
+ import { AsyncSelector } from 'shadow-dom-selector';
123
+
124
+ const selector = AsyncSelector();
125
+
126
+ selector.article.$.div.$.element
127
+ .then((shadowRoot) => {
128
+ // Do stuff with the shadowRoot
129
+ // If it is not found after all the retries, it will return null
130
+ });
131
+
132
+ selector.article.$.div.$['section > h1'].element
133
+ .then((h1) => {
134
+ // Do stuff with the h1 element
135
+ // If it is not found after all the retries, it will return null
136
+ });
137
+
138
+ selector.article.$.div.$.p.all
139
+ .then((paragraphs) => {
140
+ // Do stuff with the paragraphs
141
+ // If they are not found after all the retries, it will return an empty NodeList
142
+ });
101
143
  ```
102
144
 
103
- All these three functions allow you to to specify the amount of retries and the delay between each one of them. Consult the [API](#api) section for more details.
145
+ Either the async methods or the async dot notation allow you to to specify the amount of retries and the delay between each one of them. Consult the [API](#api) section for more details.
104
146
 
105
147
  ## Install
106
148
 
@@ -142,6 +184,7 @@ ShadowDomSelector.shadowRootQuerySelector;
142
184
  ShadowDomSelector.asyncQuerySelector;
143
185
  ShadowDomSelector.asyncQuerySelectorAll;
144
186
  ShadowDomSelector.asyncShadowRootQuerySelector;
187
+ ShadowDomSelector.AsyncSelector;
145
188
  ```
146
189
 
147
190
  ## API
@@ -149,11 +192,11 @@ ShadowDomSelector.asyncShadowRootQuerySelector;
149
192
  #### querySelector
150
193
 
151
194
  ```typescript
152
- querySelector(selectors);
195
+ querySelector(selectors): Element | null;
153
196
  ```
154
197
 
155
198
  ```typescript
156
- querySelector(root, selectors);
199
+ querySelector(root, selectors): Element | null;
157
200
  ```
158
201
 
159
202
  | Parameter | Optional | Description |
@@ -164,11 +207,11 @@ querySelector(root, selectors);
164
207
  #### querySelectorAll
165
208
 
166
209
  ```typescript
167
- querySelectorAll(selectors);
210
+ querySelectorAll(selectors): NodeListOf<Element>;
168
211
  ```
169
212
 
170
213
  ```typescript
171
- querySelectorAll(root, selectors);
214
+ querySelectorAll(root, selectors): NodeListOf<Element>;
172
215
  ```
173
216
 
174
217
  | Parameter | Optional | Description |
@@ -179,11 +222,11 @@ querySelectorAll(root, selectors);
179
222
  #### shadowRootQuerySelector
180
223
 
181
224
  ```typescript
182
- shadowRootQuerySelector(selectors);
225
+ shadowRootQuerySelector(selectors): ShadowRoot | null;
183
226
  ```
184
227
 
185
228
  ```typescript
186
- shadowRootQuerySelector(root, selectors);
229
+ shadowRootQuerySelector(root, selectors): ShadowRoot | null;
187
230
  ```
188
231
 
189
232
  | Parameter | Optional | Description |
@@ -194,23 +237,19 @@ shadowRootQuerySelector(root, selectors);
194
237
  #### asyncQuerySelector
195
238
 
196
239
  ```typescript
197
- asyncQuerySelector(selectors);
240
+ asyncQuerySelector(selectors): Promise<Element | null>;
198
241
  ```
199
242
 
200
243
  ```typescript
201
- asyncQuerySelector(root, selectors);
244
+ asyncQuerySelector(root, selectors): Promise<Element | null>;
202
245
  ```
203
246
 
204
247
  ```typescript
205
- asyncQuerySelector(root, selectors, asyncParams);
248
+ asyncQuerySelector(selectors, asyncParams): Promise<Element | null>;
206
249
  ```
207
250
 
208
251
  ```typescript
209
- // asyncParams properties
210
- {
211
- retries?: number; // how many retries before giving up (defaults to 10)
212
- delay?: number; // delay between each retry (defaults to 10)
213
- }
252
+ asyncQuerySelector(root, selectors, asyncParams): Promise<Element | null>;
214
253
  ```
215
254
 
216
255
  | Parameter | Optional | Description |
@@ -219,26 +258,30 @@ asyncQuerySelector(root, selectors, asyncParams);
219
258
  | root | yes | The element from where the query should be performed, it defaults to `document` |
220
259
  | asyncParams | yes | An object containing the parameters which control the retries |
221
260
 
261
+ ```typescript
262
+ // asyncParams properties
263
+ {
264
+ retries?: number; // how many retries before giving up (defaults to 10)
265
+ delay?: number; // delay between each retry (defaults to 10)
266
+ }
267
+ ```
268
+
222
269
  #### asyncQuerySelectorAll
223
270
 
224
271
  ```typescript
225
- asyncQuerySelectorAll(selectors);
272
+ asyncQuerySelectorAll(selectors): Promise<NodeListOf<Element>>;
226
273
  ```
227
274
 
228
275
  ```typescript
229
- asyncQuerySelectorAll(root, selectors);
276
+ asyncQuerySelectorAll(root, selectors): Promise<NodeListOf<Element>>;
230
277
  ```
231
278
 
232
279
  ```typescript
233
- asyncQuerySelectorAll(root, selectors, asyncParams);
280
+ asyncQuerySelectorAll(selectors, asyncParams): Promise<NodeListOf<Element>>;
234
281
  ```
235
282
 
236
283
  ```typescript
237
- // asyncParams properties
238
- {
239
- retries?: number; // how many retries before giving up (defaults to 10)
240
- delay?: number; // delay between each retry (defaults to 10)
241
- }
284
+ asyncQuerySelectorAll(root, selectors, asyncParams): Promise<NodeListOf<Element>>;
242
285
  ```
243
286
 
244
287
  | Parameter | Optional | Description |
@@ -247,20 +290,38 @@ asyncQuerySelectorAll(root, selectors, asyncParams);
247
290
  | root | yes | The element from where the query should be performed, it defaults to `document` |
248
291
  | asyncParams | yes | An object containing the parameters which control the retries |
249
292
 
293
+ ```typescript
294
+ // asyncParams properties
295
+ {
296
+ retries?: number; // how many retries before giving up (defaults to 10)
297
+ delay?: number; // delay between each retry (defaults to 10)
298
+ }
299
+ ```
300
+
250
301
  #### asyncShadowRootQuerySelector
251
302
 
252
303
  ```typescript
253
- asyncShadowRootQuerySelector(selectors);
304
+ asyncShadowRootQuerySelector(selectors): Promise<ShadowRoot | null>;
254
305
  ```
255
306
 
256
307
  ```typescript
257
- asyncShadowRootQuerySelector(root, selectors);
308
+ asyncShadowRootQuerySelector(root, selectors): Promise<ShadowRoot | null>;
258
309
  ```
259
310
 
260
311
  ```typescript
261
- asyncShadowRootQuerySelector(root, selectors, asyncParams);
312
+ asyncShadowRootQuerySelector(selectors, asyncParams): Promise<ShadowRoot | null>;
262
313
  ```
263
314
 
315
+ ```typescript
316
+ asyncShadowRootQuerySelector(root, selectors, asyncParams): Promise<ShadowRoot | null>;
317
+ ```
318
+
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
+
264
325
  ```typescript
265
326
  // asyncParams properties
266
327
  {
@@ -269,12 +330,61 @@ asyncShadowRootQuerySelector(root, selectors, asyncParams);
269
330
  }
270
331
  ```
271
332
 
333
+ #### AsyncSelector
334
+
335
+ ```typescript
336
+ AsyncSelector(root): AsyncSelectorProxy;
337
+ ```
338
+
339
+ ```typescript
340
+ AsyncSelector(root, asyncParams): AsyncSelectorProxy;
341
+ ```
342
+
272
343
  | Parameter | Optional | Description |
273
344
  | ------------ | ------------- | -------------------------------------------------- |
274
- | selectors | no | A string containing one or more selectors to match. Selectors must end in a Shadow DOM (`$`) |
275
- | root | yes | The element from where the query should be performed, it defaults to `document` |
345
+ | root | yes | The element or shadowRoot from where the query should be performed, it defaults to `document` |
276
346
  | asyncParams | yes | An object containing the parameters which control the retries |
277
347
 
348
+ ```typescript
349
+ // asyncParams properties
350
+ {
351
+ retries?: number; // how many retries before giving up (defaults to 10)
352
+ delay?: number; // delay between each retry (defaults to 10)
353
+ }
354
+ ```
355
+
356
+ This function returns an object with the next properties:
357
+
358
+ ```typescript
359
+ // AsyncSelectorProxy properties
360
+ {
361
+ element: Promise<Document | Element | ShadowRoot | null>; // A promise that resolves in the first queried element
362
+ all: Promise<NodeListOf<Element>>; // A promise that resolves in all the queried elements
363
+ $: Promise<ShadowRoot | null>; // A promise that resolves in the shadowRoot of the first queried element
364
+ asyncParams: { retries: number; delay: number; } // The asyncParameters being used in the chain
365
+ [any other property]: AsyncSelectorProxy; // Returns another AsyncSelectorProxy pointing to the element queried by the property name
366
+ }
367
+ ```
368
+
369
+ ##### Examples of AsyncSelector
370
+
371
+ ```typescript
372
+ const selector = AsyncSelector(); // AsyncSelectorProxy starting in the document with the default asyncParams
373
+ await selector.element === document;
374
+ await selector.all; // Empty Node list
375
+ await selector.$; // null
376
+ ```
377
+
378
+ ```typescript
379
+ const selector = AsyncSelector({
380
+ retries: 100,
381
+ delay: 50
382
+ }); // AsyncSelectorProxy starting in the document and with custom asyncParams
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.$.article.asyncParams; // { retries: 100, delay: 50 }
387
+ ```
278
388
 
279
389
  [Shadow DOM]: https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_shadow_DOM
280
390
  [optional chaining]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining
@@ -2,6 +2,17 @@ interface AsyncParams {
2
2
  retries?: number;
3
3
  delay?: number;
4
4
  }
5
+ type AsyncSelectorBase = {
6
+ _element: Document | Element | ShadowRoot | Promise<NodeListOf<Element> | ShadowRoot | null>;
7
+ asyncParams: AsyncParams;
8
+ };
9
+ type AsyncSelectorInstance = AsyncSelectorBase & {
10
+ element: Promise<Document | Element | ShadowRoot | null>;
11
+ all: Promise<NodeListOf<Element>>;
12
+ };
13
+ type AsyncSelectorProxy = AsyncSelectorInstance & {
14
+ [prop: string]: AsyncSelectorProxy;
15
+ };
5
16
  declare function querySelector<E extends Element = Element>(root: Document | Element, selectors: string): E | null;
6
17
  declare function querySelector<E extends Element = Element>(selectors: string): E | null;
7
18
  declare function querySelectorAll<E extends Element = Element>(root: Document | Element, selectors: string): NodeListOf<E>;
@@ -14,4 +25,6 @@ declare function asyncQuerySelectorAll<E extends Element = Element>(root: Docume
14
25
  declare function asyncQuerySelectorAll<E extends Element = Element>(selectors: string, asyncParams?: AsyncParams): Promise<NodeListOf<E>>;
15
26
  declare function asyncShadowRootQuerySelector(root: Document | Element, selectors: string, asyncParams?: AsyncParams): Promise<ShadowRoot | null>;
16
27
  declare function asyncShadowRootQuerySelector(selectors: string, asyncParams?: AsyncParams): Promise<ShadowRoot | null>;
17
- export { querySelector, querySelectorAll, shadowRootQuerySelector, asyncQuerySelector, asyncQuerySelectorAll, asyncShadowRootQuerySelector };
28
+ declare function AsyncSelector(asyncParams?: AsyncParams): AsyncSelectorProxy;
29
+ declare function AsyncSelector(root: Document | Element | ShadowRoot, asyncParams?: AsyncParams): AsyncSelectorProxy;
30
+ export { querySelector, querySelectorAll, shadowRootQuerySelector, asyncQuerySelector, asyncQuerySelectorAll, asyncShadowRootQuerySelector, AsyncSelector };
package/dist/esm/index.js CHANGED
@@ -1 +1 @@
1
- function n(n,e,t,r){return new(t||(t=Promise))((function(o,u){function l(n){try{a(r.next(n))}catch(n){u(n)}}function c(n){try{a(r.throw(n))}catch(n){u(n)}}function a(n){var e;n.done?o(n.value):(e=n.value,e instanceof t?e:new t((function(n){n(e)}))).then(l,c)}a((r=r.apply(n,e||[])).next())}))}function e(n,e){var t,r,o,u,l={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return u={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function c(c){return function(a){return function(c){if(t)throw new TypeError("Generator is already executing.");for(;u&&(u=0,c[0]&&(l=0)),l;)try{if(t=1,r&&(o=2&c[0]?r.return:c[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,c[1])).done)return o;switch(r=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return l.label++,{value:c[1],done:!1};case 5:l.label++,r=c[1],c=[0];continue;case 7:c=l.ops.pop(),l.trys.pop();continue;default:if(!(o=l.trys,(o=o.length>0&&o[o.length-1])||6!==c[0]&&2!==c[0])){l=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){l.label=c[1];break}if(6===c[0]&&l.label<o[1]){l.label=o[1],o=c;break}if(o&&l.label<o[2]){l.label=o[2],l.ops.push(c);break}o[2]&&l.ops.pop(),l.trys.pop();continue}c=e.call(n,l)}catch(n){c=[6,n],r=0}finally{t=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,a])}}}function t(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 r="$",o=":host",u="invalid selector",l=10,c=10,a=function(n){var e,t=n[0],r=n[1];return(e=t)&&(e instanceof Document||e instanceof Element)&&"string"==typeof r};function i(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(r).map((function(n){return n.trim()}))}(n);return e(t)}))}function s(n,e,t,r,o){return void 0===o&&(o=!1),new Promise((function(l){var c=0,a=function(){var i=o?n.querySelectorAll(e):n.querySelector(e);o&&i.length||!o&&null!==i?l(i):++c<t?setTimeout(a,r):l(o?document.querySelectorAll(u):null)};a()}))}function f(n,e,t){return new Promise((function(r){var o=0,u=function(){var l=n.shadowRoot;l?r(l):++o<e?setTimeout(u,t):r(null)};u()}))}function h(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(r,").").concat(t)}function d(n,e){return"".concat(n," must be used with a selector ending in a shadowRoot (").concat(r,"). If you don't want to select a shadowRoot, use ").concat(e," instead.")}function v(){return"You can not select a shadowRoot (".concat(r,") of the document.")}function y(n,e){for(var t,r,u=null,l=n.length,c=0;c<l;c++){if(0===c)if(n[c].length)u=e.querySelector(n[c]);else{if(e instanceof Document)throw new SyntaxError(v());u=(null===(t=e.shadowRoot)||void 0===t?void 0:t.querySelector(n[++c]))||null}else u=(null===(r=u.shadowRoot)||void 0===r?void 0:r.querySelector("".concat(o," ").concat(n[c])))||null;if(null===u)return null}return u}function w(n,e){var r,u=t([],n,!0),l=u.pop();return u.length?(null===(r=y(u,e).shadowRoot)||void 0===r?void 0:r.querySelectorAll("".concat(o," ").concat(l)))||null:e.querySelectorAll(l)}function p(n,e){var t=y(n,e);return(null==t?void 0:t.shadowRoot)||null}function g(t,r,u,l){return n(this,void 0,void 0,(function(){var n,c,a,i,h,d;return e(this,(function(e){switch(e.label){case 0:n=null,c=t.length,a=0,e.label=1;case 1:if(!(a<c))return[3,15];if(0!==a)return[3,8];if(t[a].length)return[3,5];if(r instanceof Document)throw new SyntaxError(v());return r.shadowRoot?[4,s(r.shadowRoot,t[++a],u,l)]:[3,3];case 2:return i=e.sent(),[3,4];case 3:i=null,e.label=4;case 4:return n=i,[3,7];case 5:return[4,s(r,t[a],u,l)];case 6:n=e.sent(),e.label=7;case 7:return[3,13];case 8:return[4,f(n,u,l)];case 9:return(h=e.sent())?[4,s(h,"".concat(o," ").concat(t[a]),u,l)]:[3,11];case 10:return d=e.sent(),[3,12];case 11:d=null,e.label=12;case 12:n=d,e.label=13;case 13:if(null===n)return[2,null];e.label=14;case 14:return a++,[3,1];case 15:return[2,n]}}))}))}function b(r,u,l,c){return n(this,void 0,void 0,(function(){var n,a,i,h,d,v;return e(this,(function(e){switch(e.label){case 0:return n=t([],r,!0),a=n.pop(),n.length?[3,2]:[4,s(u,a,l,c,!0)];case 1:return[2,e.sent()];case 2:return[4,g(n,u,l,c)];case 3:return(i=e.sent())?[4,f(i,l,c)]:[3,5];case 4:return d=e.sent(),[3,6];case 5:d=null,e.label=6;case 6:return(h=d)?[4,s(h,"".concat(o," ").concat(a),l,c,!0)]:[3,8];case 7:return v=e.sent(),[3,9];case 8:v=null,e.label=9;case 9:return[2,v]}}))}))}function S(t,r,o,u){return n(this,void 0,void 0,(function(){var n,l;return e(this,(function(e){switch(e.label){case 0:return[4,g(t,r,o,u)];case 1:return(n=e.sent())?[4,f(n,o,u)]:[3,3];case 2:return l=e.sent(),[3,4];case 3:l=null,e.label=4;case 4:return[2,l]}}))}))}function m(n,e){for(var t=i(n,(function(n){if(!n[n.length-1].length)throw new SyntaxError(h("querySelector","shadowRootQuerySelector"));return n})),r=t.length,o=0;o<r;o++){var u=y(t[o],e);if(u)return u}return null}function R(n,e){for(var t=i(n,(function(n){if(!n[n.length-1].length)throw new SyntaxError(h("querySelectorAll"));return n})),r=t.length,o=0;o<r;o++){var l=w(t[o],e);if(null==l?void 0:l.length)return l}return document.querySelectorAll(u)}function x(n,e){for(var t=i(n,(function(n){if(n.pop().length)throw new SyntaxError(d("shadowRootQuerySelector","querySelector"));return n})),r=t.length,o=0;o<r;o++){var u=p(t[o],e);if(u)return u}return null}function q(t,r,o,u){return n(this,void 0,void 0,(function(){var n,l,c,a;return e(this,(function(e){switch(e.label){case 0:n=i(t,(function(n){if(!n[n.length-1].length)throw new SyntaxError(h("asyncQuerySelector","asyncShadowRootQuerySelector"));return n})),l=n.length,c=0,e.label=1;case 1:return c<l?[4,g(n[c],r,o,u)]:[3,4];case 2:if(a=e.sent())return[2,a];e.label=3;case 3:return c++,[3,1];case 4:return[2,null]}}))}))}function E(t,r,o,l){return n(this,void 0,void 0,(function(){var n,c,a,s;return e(this,(function(e){switch(e.label){case 0:n=i(t,(function(n){if(!n[n.length-1].length)throw new SyntaxError(h("asyncQuerySelectorAll"));return n})),c=n.length,a=0,e.label=1;case 1:return a<c?[4,b(n[a],r,o,l)]:[3,4];case 2:if(null==(s=e.sent())?void 0:s.length)return[2,s];e.label=3;case 3:return a++,[3,1];case 4:return[2,document.querySelectorAll(u)]}}))}))}function A(t,r,o,u){return n(this,void 0,void 0,(function(){var n,l,c,a;return e(this,(function(e){switch(e.label){case 0:n=i(t,(function(n){if(n.pop().length)throw new SyntaxError(d("asyncShadowRootQuerySelector","asyncQuerySelector"));return n})),l=n.length,c=0,e.label=1;case 1:return c<l?[4,S(n[c],r,o,u)]:[3,4];case 2:if(a=e.sent())return[2,a];e.label=3;case 3:return c++,[3,1];case 4:return[2,null]}}))}))}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?m(t,document):m(r,t)}function k(){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 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?x(t,document):x(r,t)}function P(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return n(this,void 0,void 0,(function(){var n,r,o,u,i;return e(this,(function(e){switch(e.label){case 0:return a(t)?(n=t[0],r=t[1],o=t[2],[4,q(r,n,(null==o?void 0:o.retries)||l,(null==o?void 0:o.delay)||c)]):[3,2];case 1:case 3:return[2,e.sent()];case 2:return u=t[0],i=t[1],[4,q(u,document,(null==i?void 0:i.retries)||l,(null==i?void 0:i.delay)||c)]}}))}))}function T(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return n(this,void 0,void 0,(function(){var n,r,o,u,i;return e(this,(function(e){switch(e.label){case 0:return a(t)?(n=t[0],r=t[1],o=t[2],[4,E(r,n,(null==o?void 0:o.retries)||l,(null==o?void 0:o.delay)||c)]):[3,2];case 1:return[2,e.sent()];case 2:return u=t[0],i=t[1],[2,E(u,document,(null==i?void 0:i.retries)||l,(null==i?void 0:i.delay)||c)]}}))}))}function I(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return n(this,void 0,void 0,(function(){var n,r,o,u,i;return e(this,(function(e){switch(e.label){case 0:return a(t)?(n=t[0],r=t[1],o=t[2],[4,A(r,n,(null==o?void 0:o.retries)||l,(null==o?void 0:o.delay)||c)]):[3,2];case 1:return[2,e.sent()];case 2:return u=t[0],i=t[1],[2,A(u,document,(null==i?void 0:i.retries)||l,(null==i?void 0:i.delay)||c)]}}))}))}export{P as asyncQuerySelector,T as asyncQuerySelectorAll,I as asyncShadowRootQuerySelector,Q as querySelector,k as querySelectorAll,D as shadowRootQuerySelector};
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{O as AsyncSelector,T as asyncQuerySelector,k as asyncQuerySelectorAll,M as asyncShadowRootQuerySelector,_ as querySelector,Q as querySelectorAll,D as shadowRootQuerySelector};
package/dist/index.d.ts CHANGED
@@ -2,6 +2,17 @@ interface AsyncParams {
2
2
  retries?: number;
3
3
  delay?: number;
4
4
  }
5
+ type AsyncSelectorBase = {
6
+ _element: Document | Element | ShadowRoot | Promise<NodeListOf<Element> | ShadowRoot | null>;
7
+ asyncParams: AsyncParams;
8
+ };
9
+ type AsyncSelectorInstance = AsyncSelectorBase & {
10
+ element: Promise<Document | Element | ShadowRoot | null>;
11
+ all: Promise<NodeListOf<Element>>;
12
+ };
13
+ type AsyncSelectorProxy = AsyncSelectorInstance & {
14
+ [prop: string]: AsyncSelectorProxy;
15
+ };
5
16
  declare function querySelector<E extends Element = Element>(root: Document | Element, selectors: string): E | null;
6
17
  declare function querySelector<E extends Element = Element>(selectors: string): E | null;
7
18
  declare function querySelectorAll<E extends Element = Element>(root: Document | Element, selectors: string): NodeListOf<E>;
@@ -14,4 +25,6 @@ declare function asyncQuerySelectorAll<E extends Element = Element>(root: Docume
14
25
  declare function asyncQuerySelectorAll<E extends Element = Element>(selectors: string, asyncParams?: AsyncParams): Promise<NodeListOf<E>>;
15
26
  declare function asyncShadowRootQuerySelector(root: Document | Element, selectors: string, asyncParams?: AsyncParams): Promise<ShadowRoot | null>;
16
27
  declare function asyncShadowRootQuerySelector(selectors: string, asyncParams?: AsyncParams): Promise<ShadowRoot | null>;
17
- export { querySelector, querySelectorAll, shadowRootQuerySelector, asyncQuerySelector, asyncQuerySelectorAll, asyncShadowRootQuerySelector };
28
+ declare function AsyncSelector(asyncParams?: AsyncParams): AsyncSelectorProxy;
29
+ declare function AsyncSelector(root: Document | Element | ShadowRoot, asyncParams?: AsyncParams): AsyncSelectorProxy;
30
+ export { querySelector, querySelectorAll, shadowRootQuerySelector, asyncQuerySelector, asyncQuerySelectorAll, asyncShadowRootQuerySelector, AsyncSelector };
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";function n(e,n,t,r){return new(t||(t=Promise))((function(o,u){function l(e){try{a(r.next(e))}catch(e){u(e)}}function c(e){try{a(r.throw(e))}catch(e){u(e)}}function a(e){var n;e.done?o(e.value):(n=e.value,n instanceof t?n:new t((function(e){e(n)}))).then(l,c)}a((r=r.apply(e,n||[])).next())}))}function t(e,n){var t,r,o,u,l={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return u={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function c(c){return function(a){return function(c){if(t)throw new TypeError("Generator is already executing.");for(;u&&(u=0,c[0]&&(l=0)),l;)try{if(t=1,r&&(o=2&c[0]?r.return:c[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,c[1])).done)return o;switch(r=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return l.label++,{value:c[1],done:!1};case 5:l.label++,r=c[1],c=[0];continue;case 7:c=l.ops.pop(),l.trys.pop();continue;default:if(!(o=l.trys,(o=o.length>0&&o[o.length-1])||6!==c[0]&&2!==c[0])){l=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){l.label=c[1];break}if(6===c[0]&&l.label<o[1]){l.label=o[1],o=c;break}if(o&&l.label<o[2]){l.label=o[2],l.ops.push(c);break}o[2]&&l.ops.pop(),l.trys.pop();continue}c=n.call(e,l)}catch(e){c=[6,e],r=0}finally{t=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,a])}}}function r(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 o="$",u=":host",l="invalid selector",c=10,a=10,i=function(e){var n,t=e[0],r=e[1];return(n=t)&&(n instanceof Document||n instanceof Element)&&"string"==typeof r};function s(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(o).map((function(e){return e.trim()}))}(e);return n(t)}))}function f(e,n,t,r,o){return void 0===o&&(o=!1),new Promise((function(u){var c=0,a=function(){var i=o?e.querySelectorAll(n):e.querySelector(n);o&&i.length||!o&&null!==i?u(i):++c<t?setTimeout(a,r):u(o?document.querySelectorAll(l):null)};a()}))}function h(e,n,t){return new Promise((function(r){var o=0,u=function(){var l=e.shadowRoot;l?r(l):++o<n?setTimeout(u,t):r(null)};u()}))}function d(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(o,").").concat(t)}function v(e,n){return"".concat(e," must be used with a selector ending in a shadowRoot (").concat(o,"). If you don't want to select a shadowRoot, use ").concat(n," instead.")}function y(){return"You can not select a shadowRoot (".concat(o,") of the document.")}function w(e,n){for(var t,r,o=null,l=e.length,c=0;c<l;c++){if(0===c)if(e[c].length)o=n.querySelector(e[c]);else{if(n instanceof Document)throw new SyntaxError(y());o=(null===(t=n.shadowRoot)||void 0===t?void 0:t.querySelector(e[++c]))||null}else o=(null===(r=o.shadowRoot)||void 0===r?void 0:r.querySelector("".concat(u," ").concat(e[c])))||null;if(null===o)return null}return o}function p(e,n){var t,o=r([],e,!0),l=o.pop();return o.length?(null===(t=w(o,n).shadowRoot)||void 0===t?void 0:t.querySelectorAll("".concat(u," ").concat(l)))||null:n.querySelectorAll(l)}function g(e,n){var t=w(e,n);return(null==t?void 0:t.shadowRoot)||null}function b(e,r,o,l){return n(this,void 0,void 0,(function(){var n,c,a,i,s,d;return t(this,(function(t){switch(t.label){case 0:n=null,c=e.length,a=0,t.label=1;case 1:if(!(a<c))return[3,15];if(0!==a)return[3,8];if(e[a].length)return[3,5];if(r instanceof Document)throw new SyntaxError(y());return r.shadowRoot?[4,f(r.shadowRoot,e[++a],o,l)]:[3,3];case 2:return i=t.sent(),[3,4];case 3:i=null,t.label=4;case 4:return n=i,[3,7];case 5:return[4,f(r,e[a],o,l)];case 6:n=t.sent(),t.label=7;case 7:return[3,13];case 8:return[4,h(n,o,l)];case 9:return(s=t.sent())?[4,f(s,"".concat(u," ").concat(e[a]),o,l)]:[3,11];case 10:return d=t.sent(),[3,12];case 11:d=null,t.label=12;case 12:n=d,t.label=13;case 13:if(null===n)return[2,null];t.label=14;case 14:return a++,[3,1];case 15:return[2,n]}}))}))}function S(e,o,l,c){return n(this,void 0,void 0,(function(){var n,a,i,s,d,v;return t(this,(function(t){switch(t.label){case 0:return n=r([],e,!0),a=n.pop(),n.length?[3,2]:[4,f(o,a,l,c,!0)];case 1:return[2,t.sent()];case 2:return[4,b(n,o,l,c)];case 3:return(i=t.sent())?[4,h(i,l,c)]:[3,5];case 4:return d=t.sent(),[3,6];case 5:d=null,t.label=6;case 6:return(s=d)?[4,f(s,"".concat(u," ").concat(a),l,c,!0)]:[3,8];case 7:return v=t.sent(),[3,9];case 8:v=null,t.label=9;case 9:return[2,v]}}))}))}function m(e,r,o,u){return n(this,void 0,void 0,(function(){var n,l;return t(this,(function(t){switch(t.label){case 0:return[4,b(e,r,o,u)];case 1:return(n=t.sent())?[4,h(n,o,u)]:[3,3];case 2:return l=t.sent(),[3,4];case 3:l=null,t.label=4;case 4:return[2,l]}}))}))}function R(e,n){for(var t=s(e,(function(e){if(!e[e.length-1].length)throw new SyntaxError(d("querySelector","shadowRootQuerySelector"));return e})),r=t.length,o=0;o<r;o++){var u=w(t[o],n);if(u)return u}return null}function x(e,n){for(var t=s(e,(function(e){if(!e[e.length-1].length)throw new SyntaxError(d("querySelectorAll"));return e})),r=t.length,o=0;o<r;o++){var u=p(t[o],n);if(null==u?void 0:u.length)return u}return document.querySelectorAll(l)}function q(e,n){for(var t=s(e,(function(e){if(e.pop().length)throw new SyntaxError(v("shadowRootQuerySelector","querySelector"));return e})),r=t.length,o=0;o<r;o++){var u=g(t[o],n);if(u)return u}return null}function A(e,r,o,u){return n(this,void 0,void 0,(function(){var n,l,c,a;return t(this,(function(t){switch(t.label){case 0:n=s(e,(function(e){if(!e[e.length-1].length)throw new SyntaxError(d("asyncQuerySelector","asyncShadowRootQuerySelector"));return e})),l=n.length,c=0,t.label=1;case 1:return c<l?[4,b(n[c],r,o,u)]:[3,4];case 2:if(a=t.sent())return[2,a];t.label=3;case 3:return c++,[3,1];case 4:return[2,null]}}))}))}function E(e,r,o,u){return n(this,void 0,void 0,(function(){var n,c,a,i;return t(this,(function(t){switch(t.label){case 0:n=s(e,(function(e){if(!e[e.length-1].length)throw new SyntaxError(d("asyncQuerySelectorAll"));return e})),c=n.length,a=0,t.label=1;case 1:return a<c?[4,S(n[a],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 a++,[3,1];case 4:return[2,document.querySelectorAll(l)]}}))}))}function Q(e,r,o,u){return n(this,void 0,void 0,(function(){var n,l,c,a;return t(this,(function(t){switch(t.label){case 0:n=s(e,(function(e){if(e.pop().length)throw new SyntaxError(v("asyncShadowRootQuerySelector","asyncQuerySelector"));return e})),l=n.length,c=0,t.label=1;case 1:return c<l?[4,m(n[c],r,o,u)]:[3,4];case 2:if(a=t.sent())return[2,a];t.label=3;case 3:return c++,[3,1];case 4:return[2,null]}}))}))}e.asyncQuerySelector=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return n(this,void 0,void 0,(function(){var n,r,o,u,l;return t(this,(function(t){switch(t.label){case 0:return i(e)?(n=e[0],r=e[1],o=e[2],[4,A(r,n,(null==o?void 0:o.retries)||c,(null==o?void 0:o.delay)||a)]):[3,2];case 1:case 3:return[2,t.sent()];case 2:return u=e[0],l=e[1],[4,A(u,document,(null==l?void 0:l.retries)||c,(null==l?void 0:l.delay)||a)]}}))}))},e.asyncQuerySelectorAll=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return n(this,void 0,void 0,(function(){var n,r,o,u,l;return t(this,(function(t){switch(t.label){case 0:return i(e)?(n=e[0],r=e[1],o=e[2],[4,E(r,n,(null==o?void 0:o.retries)||c,(null==o?void 0:o.delay)||a)]):[3,2];case 1:return[2,t.sent()];case 2:return u=e[0],l=e[1],[2,E(u,document,(null==l?void 0:l.retries)||c,(null==l?void 0:l.delay)||a)]}}))}))},e.asyncShadowRootQuerySelector=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return n(this,void 0,void 0,(function(){var n,r,o,u,l;return t(this,(function(t){switch(t.label){case 0:return i(e)?(n=e[0],r=e[1],o=e[2],[4,Q(r,n,(null==o?void 0:o.retries)||c,(null==o?void 0:o.delay)||a)]):[3,2];case 1:return[2,t.sent()];case 2:return u=e[0],l=e[1],[2,Q(u,document,(null==l?void 0:l.retries)||c,(null==l?void 0:l.delay)||a)]}}))}))},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?R(t,document):R(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(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.AsyncSelector=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.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.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)}}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shadow-dom-selector",
3
- "version": "1.0.1",
3
+ "version": "2.0.0",
4
4
  "description": "A very small JavaScript utility to query DOM elements through the Shadow Dom subtrees in a sync or an async way",
5
5
  "keywords": [
6
6
  "query-selector",
@@ -36,15 +36,16 @@
36
36
  "author": "ElChiniNet",
37
37
  "license": "Apache-2.0",
38
38
  "scripts": {
39
- "build": "yarn clean && rollup --config rollup.config.js --bundleConfigAsCjs",
39
+ "build": "rollup --config rollup.config.js --bundleConfigAsCjs",
40
40
  "clean": "rm -rf dist .nyc_output coverage || true",
41
41
  "demo": "rollup --config rollup.test.config.js --bundleConfigAsCjs -w",
42
42
  "coverage:report": "nyc report --reporter=lcov --reporter=text-summary",
43
- "lint": "eslint \"src/**/*.ts\" \"demo/**/*.ts\"",
43
+ "lint": "eslint \"src/**/*.ts\" \"demo/**/*.ts\" \"cypress/**/*.ts\"",
44
+ "lint:fix": "yarn lint --fix",
44
45
  "test": "cypress run",
45
46
  "test:ts": "tsc --noEmit",
46
- "test:open": "cypress open",
47
- "test:ci": "start-server-and-test demo http://localhost:3000 test && yarn coverage:report",
47
+ "test:open": "yarn clean && cypress open",
48
+ "test:ci": "yarn clean && start-server-and-test demo http://localhost:3000 test && yarn coverage:report",
48
49
  "prepare": "yarn build",
49
50
  "prepublishOnly": "yarn lint && yarn test:ts && yarn test:ci",
50
51
  "version": "git add .",