shadow-dom-selector 1.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/LICENSE +201 -0
- package/README.md +280 -0
- package/dist/esm/index.d.ts +17 -0
- package/dist/esm/index.js +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +1 -0
- package/package.json +68 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
# shadow-dom-selector
|
|
2
|
+
|
|
3
|
+
A very small JavaScript utility to query DOM elements through the [Shadow DOM] subtrees in a sync or an async way.
|
|
4
|
+
|
|
5
|
+
[](https://github.com/elchininet/shadow-dom-selector/actions/workflows/deploy.yaml) [](https://coveralls.io/github/elchininet/shadow-dom-selector?branch=master) [](https://badge.fury.io/js/shadow-dom-selector)
|
|
6
|
+
|
|
7
|
+
## Introduction
|
|
8
|
+
|
|
9
|
+
Having a DOM tree formed of Shadow DOM subtrees like the next one:
|
|
10
|
+
|
|
11
|
+
```html
|
|
12
|
+
<body>
|
|
13
|
+
<section>
|
|
14
|
+
#shadow-root (open)
|
|
15
|
+
<h1>Title</h1>
|
|
16
|
+
<article>
|
|
17
|
+
#shadow-root (open)
|
|
18
|
+
<h2>Subtitle</h2>
|
|
19
|
+
<ul>
|
|
20
|
+
<li>Item 1</li>
|
|
21
|
+
<li>Item 2</li>
|
|
22
|
+
<li>Item 1</li>
|
|
23
|
+
</ul>
|
|
24
|
+
</article>
|
|
25
|
+
</section>
|
|
26
|
+
</body>
|
|
27
|
+
```
|
|
28
|
+
If one wants to query for the `li` elements, it is required to do this:
|
|
29
|
+
|
|
30
|
+
```javascript
|
|
31
|
+
const secondLi = document.querySelector('section').shadowRoot.querySelector('article').shadowRoot.querySelector('ul > li');
|
|
32
|
+
|
|
33
|
+
const allLis = document.querySelector('section').shadowRoot.querySelector('article').shadowRoot.querySelectorAll('ul > li');
|
|
34
|
+
|
|
35
|
+
const shadow = document.querySelector('section').shadowRoot.querySelector('article').shadowRoot;
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`shadow-dom-selector` allows you to do the same in the next way:
|
|
39
|
+
|
|
40
|
+
```javascript
|
|
41
|
+
import { querySelector, querySelectorAll, shadowRootQuerySelector } from 'shadow-dom-selector';
|
|
42
|
+
|
|
43
|
+
// $ character at the end of a selector means to select its Shadom DOM
|
|
44
|
+
const secondLi querySelector('section$ article$ ul > li');
|
|
45
|
+
const allLis querySelectorAll('section$ article$ ul > li');
|
|
46
|
+
const shadow = shadowRootQuerySelector('section$ article$');
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
It will traverse all the Shadow DOM subtrees removing all the hassle of writing long concatenated queries.
|
|
50
|
+
|
|
51
|
+
### It checks for the existence of elements
|
|
52
|
+
|
|
53
|
+
With the same previous DOM tree, if we do this:
|
|
54
|
+
|
|
55
|
+
```javascript
|
|
56
|
+
const element = document.querySelector('article').shadowRoot.querySelector('div').shadowRoot.querySelector('section > h1');
|
|
57
|
+
```
|
|
58
|
+
|
|
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
|
+
|
|
61
|
+
```javascript
|
|
62
|
+
const element = document.querySelector('article')?.shadowRoot?.querySelector('div')?.shadowRoot?.querySelector('section > h1');
|
|
63
|
+
const elements = document.querySelector('article')?.shadowRoot?.querySelector('div')?.shadowRoot?.querySelectorAll('p');
|
|
64
|
+
const shadow = document.querySelector('article')?.shadowRoot?.querySelector('div')?.shadowRoot;
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
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.
|
|
68
|
+
|
|
69
|
+
```javascript
|
|
70
|
+
import { querySelector, querySelectorAll, shadowRootQuerySelector } from 'shadow-dom-selector';
|
|
71
|
+
|
|
72
|
+
const element = querySelector('article$ div$ section > h1'); // null
|
|
73
|
+
const elements = querySelectorAll('article div$ p'); // empty NodeList
|
|
74
|
+
const shadow = shadowRootQuerySelector('article$ div$'); // null
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Async queries
|
|
78
|
+
|
|
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`.
|
|
80
|
+
|
|
81
|
+
```javascript
|
|
82
|
+
import { asyncQuerySelector, asyncQuerySelectorAll, asyncShadowRootQuerySelector } from 'shadow-dom-selector';
|
|
83
|
+
|
|
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
|
+
});
|
|
101
|
+
```
|
|
102
|
+
|
|
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.
|
|
104
|
+
|
|
105
|
+
## Install
|
|
106
|
+
|
|
107
|
+
#### npm
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
npm install shadow-dom-selector
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
#### yarn
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
yarn add shadow-dom-selector
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
#### PNPM
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
pnpm add shadow-dom-selector
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
#### In the browser
|
|
126
|
+
|
|
127
|
+
It is possible to include a compiled version of the package directly in an HTML file. It will create a global `ShadowDomSelector` object containing all the exported functions that can be accessed from anywhere in your JavaScript code.
|
|
128
|
+
|
|
129
|
+
1. Copy the JavaScript file `index.js`, located in the root of the `dist/` folder
|
|
130
|
+
2. Put it in the folder that you prefer in your web server
|
|
131
|
+
3. Include it in your HTML file
|
|
132
|
+
|
|
133
|
+
```html
|
|
134
|
+
<script src="wherever/you/want/to/place/index.js"></script>
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
```javascript
|
|
138
|
+
/* There will be a global variable named ShadowDomSelector containing all the functions */
|
|
139
|
+
ShadowDomSelector.querySelector;
|
|
140
|
+
ShadowDomSelector.querySelectorAll;
|
|
141
|
+
ShadowDomSelector.shadowRootQuerySelector;
|
|
142
|
+
ShadowDomSelector.asyncQuerySelector;
|
|
143
|
+
ShadowDomSelector.asyncQuerySelectorAll;
|
|
144
|
+
ShadowDomSelector.asyncShadowRootQuerySelector;
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## API
|
|
148
|
+
|
|
149
|
+
#### querySelector
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
querySelector(selectors);
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
querySelector(root, selectors);
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
| Parameter | Optional | Description |
|
|
160
|
+
| ------------ | ------------- | -------------------------------------------------- |
|
|
161
|
+
| selectors | no | A string containing one or more selectors to match. Selectors cannot end in a Shadow DOM (`$`) |
|
|
162
|
+
| root | yes | The element from where the query should be performed, it defaults to `document` |
|
|
163
|
+
|
|
164
|
+
#### querySelectorAll
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
querySelectorAll(selectors);
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
querySelectorAll(root, selectors);
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
| Parameter | Optional | Description |
|
|
175
|
+
| ------------ | ------------- | -------------------------------------------------- |
|
|
176
|
+
| selectors | no | A string containing one or more selectors to match. Selectors cannot end in a Shadow DOM (`$`) |
|
|
177
|
+
| root | yes | The element from where the query should be performed, it defaults to `document` |
|
|
178
|
+
|
|
179
|
+
#### shadowRootQuerySelector
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
shadowRootQuerySelector(selectors);
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
shadowRootQuerySelector(root, selectors);
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
| Parameter | Optional | Description |
|
|
190
|
+
| ------------ | ------------- | -------------------------------------------------- |
|
|
191
|
+
| selectors | no | A string containing one or more selectors to match. Selectors must end in a Shadow DOM (`$`) |
|
|
192
|
+
| root | yes | The element from where the query should be performed, it defaults to `document` |
|
|
193
|
+
|
|
194
|
+
#### asyncQuerySelector
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
asyncQuerySelector(selectors);
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
asyncQuerySelector(root, selectors);
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
```typescript
|
|
205
|
+
asyncQuerySelector(root, selectors, asyncParams);
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
```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
|
+
}
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
| Parameter | Optional | Description |
|
|
217
|
+
| ------------ | ------------- | -------------------------------------------------- |
|
|
218
|
+
| selectors | no | A string containing one or more selectors to match. Selectors cannot end in a Shadow DOM (`$`) |
|
|
219
|
+
| root | yes | The element from where the query should be performed, it defaults to `document` |
|
|
220
|
+
| asyncParams | yes | An object containing the parameters which control the retries |
|
|
221
|
+
|
|
222
|
+
#### asyncQuerySelectorAll
|
|
223
|
+
|
|
224
|
+
```typescript
|
|
225
|
+
asyncQuerySelectorAll(selectors);
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
```typescript
|
|
229
|
+
asyncQuerySelectorAll(root, selectors);
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
```typescript
|
|
233
|
+
asyncQuerySelectorAll(root, selectors, asyncParams);
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
```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
|
+
}
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
| Parameter | Optional | Description |
|
|
245
|
+
| ------------ | ------------- | -------------------------------------------------- |
|
|
246
|
+
| selectors | no | A string containing one or more selectors to match. Selectors cannot end in a Shadow DOM (`$`) |
|
|
247
|
+
| root | yes | The element from where the query should be performed, it defaults to `document` |
|
|
248
|
+
| asyncParams | yes | An object containing the parameters which control the retries |
|
|
249
|
+
|
|
250
|
+
#### asyncShadowRootQuerySelector
|
|
251
|
+
|
|
252
|
+
```typescript
|
|
253
|
+
asyncShadowRootQuerySelector(selectors);
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
```typescript
|
|
257
|
+
asyncShadowRootQuerySelector(root, selectors);
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
```typescript
|
|
261
|
+
asyncShadowRootQuerySelector(root, selectors, asyncParams);
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
```typescript
|
|
265
|
+
// asyncParams properties
|
|
266
|
+
{
|
|
267
|
+
retries?: number; // how many retries before giving up (defaults to 10)
|
|
268
|
+
delay?: number; // delay between each retry (defaults to 10)
|
|
269
|
+
}
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
| Parameter | Optional | Description |
|
|
273
|
+
| ------------ | ------------- | -------------------------------------------------- |
|
|
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` |
|
|
276
|
+
| asyncParams | yes | An object containing the parameters which control the retries |
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
[Shadow DOM]: https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_shadow_DOM
|
|
280
|
+
[optional chaining]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
interface AsyncParams {
|
|
2
|
+
retries?: number;
|
|
3
|
+
delay?: number;
|
|
4
|
+
}
|
|
5
|
+
declare function querySelector<E extends Element = Element>(root: Document | Element, selectors: string): E | null;
|
|
6
|
+
declare function querySelector<E extends Element = Element>(selectors: string): E | null;
|
|
7
|
+
declare function querySelectorAll<E extends Element = Element>(root: Document | Element, selectors: string): NodeListOf<E>;
|
|
8
|
+
declare function querySelectorAll<E extends Element = Element>(selectors: string): NodeListOf<E>;
|
|
9
|
+
declare function shadowRootQuerySelector(root: Document | Element, selectors: string): ShadowRoot | null;
|
|
10
|
+
declare function shadowRootQuerySelector(selectors: string): ShadowRoot | null;
|
|
11
|
+
declare function asyncQuerySelector<E extends Element = Element>(root: Document | Element, selectors: string, asyncParams?: AsyncParams): Promise<E | null>;
|
|
12
|
+
declare function asyncQuerySelector<E extends Element = Element>(selectors: string, asyncParams?: AsyncParams): Promise<E | null>;
|
|
13
|
+
declare function asyncQuerySelectorAll<E extends Element = Element>(root: Document | Element, selectors: string, asyncParams?: AsyncParams): Promise<NodeListOf<E>>;
|
|
14
|
+
declare function asyncQuerySelectorAll<E extends Element = Element>(selectors: string, asyncParams?: AsyncParams): Promise<NodeListOf<E>>;
|
|
15
|
+
declare function asyncShadowRootQuerySelector(root: Document | Element, selectors: string, asyncParams?: AsyncParams): Promise<ShadowRoot | null>;
|
|
16
|
+
declare function asyncShadowRootQuerySelector(selectors: string, asyncParams?: AsyncParams): Promise<ShadowRoot | null>;
|
|
17
|
+
export { querySelector, querySelectorAll, shadowRootQuerySelector, asyncQuerySelector, asyncQuerySelectorAll, asyncShadowRootQuerySelector };
|
|
@@ -0,0 +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};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
interface AsyncParams {
|
|
2
|
+
retries?: number;
|
|
3
|
+
delay?: number;
|
|
4
|
+
}
|
|
5
|
+
declare function querySelector<E extends Element = Element>(root: Document | Element, selectors: string): E | null;
|
|
6
|
+
declare function querySelector<E extends Element = Element>(selectors: string): E | null;
|
|
7
|
+
declare function querySelectorAll<E extends Element = Element>(root: Document | Element, selectors: string): NodeListOf<E>;
|
|
8
|
+
declare function querySelectorAll<E extends Element = Element>(selectors: string): NodeListOf<E>;
|
|
9
|
+
declare function shadowRootQuerySelector(root: Document | Element, selectors: string): ShadowRoot | null;
|
|
10
|
+
declare function shadowRootQuerySelector(selectors: string): ShadowRoot | null;
|
|
11
|
+
declare function asyncQuerySelector<E extends Element = Element>(root: Document | Element, selectors: string, asyncParams?: AsyncParams): Promise<E | null>;
|
|
12
|
+
declare function asyncQuerySelector<E extends Element = Element>(selectors: string, asyncParams?: AsyncParams): Promise<E | null>;
|
|
13
|
+
declare function asyncQuerySelectorAll<E extends Element = Element>(root: Document | Element, selectors: string, asyncParams?: AsyncParams): Promise<NodeListOf<E>>;
|
|
14
|
+
declare function asyncQuerySelectorAll<E extends Element = Element>(selectors: string, asyncParams?: AsyncParams): Promise<NodeListOf<E>>;
|
|
15
|
+
declare function asyncShadowRootQuerySelector(root: Document | Element, selectors: string, asyncParams?: AsyncParams): Promise<ShadowRoot | null>;
|
|
16
|
+
declare function asyncShadowRootQuerySelector(selectors: string, asyncParams?: AsyncParams): Promise<ShadowRoot | null>;
|
|
17
|
+
export { querySelector, querySelectorAll, shadowRootQuerySelector, asyncQuerySelector, asyncQuerySelectorAll, asyncShadowRootQuerySelector };
|
package/dist/index.js
ADDED
|
@@ -0,0 +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)}}));
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "shadow-dom-selector",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A very small JavaScript utility to query DOM elements through the Shadow Dom subtrees in a sync or an async way",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"query-selector",
|
|
7
|
+
"dom",
|
|
8
|
+
"dom-query",
|
|
9
|
+
"dom-selector",
|
|
10
|
+
"shadow-dom",
|
|
11
|
+
"shadow-root",
|
|
12
|
+
"shadow-root-selector",
|
|
13
|
+
"shadow-root-query",
|
|
14
|
+
"query-selector-all",
|
|
15
|
+
"javascript-library",
|
|
16
|
+
"library"
|
|
17
|
+
],
|
|
18
|
+
"main": "dist/index.js",
|
|
19
|
+
"module": "dist/esm/index.js",
|
|
20
|
+
"types": "dist/index.d.ts",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"require": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"default": "./dist/index.js"
|
|
26
|
+
},
|
|
27
|
+
"import": {
|
|
28
|
+
"types": "./dist/esm/index.d.ts",
|
|
29
|
+
"default": "./dist/esm/index.js"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist/**/*"
|
|
35
|
+
],
|
|
36
|
+
"author": "ElChiniNet",
|
|
37
|
+
"license": "Apache-2.0",
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "yarn clean && rollup --config rollup.config.js --bundleConfigAsCjs",
|
|
40
|
+
"clean": "rm -rf dist .nyc_output coverage || true",
|
|
41
|
+
"demo": "rollup --config rollup.test.config.js --bundleConfigAsCjs -w",
|
|
42
|
+
"coverage:report": "nyc report --reporter=lcov --reporter=text-summary",
|
|
43
|
+
"lint": "eslint \"src/**/*.ts\" \"demo/**/*.ts\"",
|
|
44
|
+
"test": "cypress run",
|
|
45
|
+
"test:ts": "tsc --noEmit",
|
|
46
|
+
"test:open": "cypress open",
|
|
47
|
+
"test:ci": "start-server-and-test demo http://localhost:3000 test && yarn coverage:report",
|
|
48
|
+
"prepare": "yarn build",
|
|
49
|
+
"prepublishOnly": "yarn lint && yarn test:ts && yarn test:ci",
|
|
50
|
+
"version": "git add .",
|
|
51
|
+
"postversion": "git push && git push --tags"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@cypress/code-coverage": "^3.12.9",
|
|
55
|
+
"@types/eslint": "^8.44.7",
|
|
56
|
+
"@typescript-eslint/eslint-plugin": "^6.10.0",
|
|
57
|
+
"@typescript-eslint/parser": "^6.10.0",
|
|
58
|
+
"cypress": "^13.5.0",
|
|
59
|
+
"eslint": "^8.53.0",
|
|
60
|
+
"rollup": "^4.3.1",
|
|
61
|
+
"rollup-plugin-istanbul": "^5.0.0",
|
|
62
|
+
"rollup-plugin-serve": "^2.0.2",
|
|
63
|
+
"rollup-plugin-terser": "^7.0.2",
|
|
64
|
+
"rollup-plugin-ts": "^3.4.5",
|
|
65
|
+
"start-server-and-test": "^2.0.2",
|
|
66
|
+
"typescript": "^5.2.2"
|
|
67
|
+
}
|
|
68
|
+
}
|