bv-ui-core 2.7.0 → 2.8.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/.eslintrc +2 -1
- package/README.md +1 -0
- package/lib/queryShadowDom/README.md +50 -0
- package/lib/queryShadowDom/index.js +93 -0
- package/package.json +1 -1
package/.eslintrc
CHANGED
package/README.md
CHANGED
@@ -56,6 +56,7 @@ module's directory.
|
|
56
56
|
- [pixelsDisplayed](./lib/pixelsDisplayed)
|
57
57
|
- [staticAssetLoader](./lib/staticAssetLoader)
|
58
58
|
- [waitForBody](./lib/waitForBody)
|
59
|
+
- [queryShadowDom](./lib/queryShadowDom)
|
59
60
|
|
60
61
|
[1]: ./CONTRIBUTING.md
|
61
62
|
[2]: https://nodejs.org/download/
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# Query Shadow DOM Element
|
2
|
+
|
3
|
+
This file exposes multiple functions that can be used to query elements within the ShadowDOM.
|
4
|
+
|
5
|
+
```javascript
|
6
|
+
shadowQuerySelectorAll
|
7
|
+
// Returns Nodes matching with match selector.
|
8
|
+
```
|
9
|
+
| Parameter | Type | Description |
|
10
|
+
| :-------- | :------- | :------------------------- |
|
11
|
+
| `node` | `DOM Element` | **Required**. Root node to query for particular elements. |
|
12
|
+
| `matchSelector` | `string` | **Required**. Criteria to select the elements in the dom starting with node as root element. |
|
13
|
+
|
14
|
+
|
15
|
+
```javascript
|
16
|
+
findShadowRoots
|
17
|
+
// Returns a list of all the shadow roots found in the given root element and its descendants.
|
18
|
+
```
|
19
|
+
| Parameter | Type | Description |
|
20
|
+
| :-------- | :------- | :------------------------- |
|
21
|
+
| `node` | `DOM Element` | **Required**. The root node from which to start searching for shadow roots. |
|
22
|
+
|
23
|
+
```javascript
|
24
|
+
filterShadowHosts
|
25
|
+
// Returns NodeFilter.
|
26
|
+
```
|
27
|
+
| Parameter | Type | Description |
|
28
|
+
| :-------- | :------- | :------------------------- |
|
29
|
+
| `node` | `DOM Element` | **Required**. Document node. |
|
30
|
+
|
31
|
+
```javascript
|
32
|
+
findElementsInShadowDomByMatchSector
|
33
|
+
// returns a NodeList of all elements within the shadow DOM of the shadowElement parameter that match the matchSelector parameter.
|
34
|
+
```
|
35
|
+
| Parameter | Type | Description |
|
36
|
+
| :-------- | :------- | :------------------------- |
|
37
|
+
| `node` | `DOM Element` | **Required**. A DOM node that has a shadow root. |
|
38
|
+
| `matchSelector` | `string` | **Required**. Criteria to select the elements in the dom starting with node as root element. |
|
39
|
+
|
40
|
+
## Usage
|
41
|
+
|
42
|
+
```javascript
|
43
|
+
|
44
|
+
|
45
|
+
const { shadowQuerySelectorAll } = require('bv-ui-core/lib/queryShadowDomElement');
|
46
|
+
|
47
|
+
const nodeList = shadowQuerySelectorAll(document, '[data-bv-show='reviews']')
|
48
|
+
|
49
|
+
// ['div', 'div']
|
50
|
+
```
|
@@ -0,0 +1,93 @@
|
|
1
|
+
/**
|
2
|
+
* Function to deterimine whether given node is shadow host.
|
3
|
+
* @param {*} node Document node.
|
4
|
+
* @returns NodeFilter
|
5
|
+
*/
|
6
|
+
function filterShadowHosts (node) {
|
7
|
+
if (node && node.shadowRoot) {
|
8
|
+
return NodeFilter.FILTER_ACCEPT;
|
9
|
+
}
|
10
|
+
return NodeFilter.FILTER_SKIP;
|
11
|
+
}
|
12
|
+
|
13
|
+
/**
|
14
|
+
* The function finds all the shadow roots in a given root element and returns a list of them.
|
15
|
+
* @param node - The root element from which to start searching for shadow roots.
|
16
|
+
* @returns a list of all the shadow roots found in the given root element and its descendants.
|
17
|
+
*/
|
18
|
+
function findShadowRoots (node) {
|
19
|
+
|
20
|
+
if (!node) {
|
21
|
+
return;
|
22
|
+
}
|
23
|
+
|
24
|
+
const walker = document.createTreeWalker(node, NodeFilter.SHOW_ELEMENT, filterShadowHosts);
|
25
|
+
let listOfShadowRoots = [];
|
26
|
+
|
27
|
+
// Tree walker will not determine the node.
|
28
|
+
if (node.shadowRoot) {
|
29
|
+
listOfShadowRoots.push(node);
|
30
|
+
listOfShadowRoots = listOfShadowRoots.concat(findShadowRoots(node.shadowRoot));
|
31
|
+
}
|
32
|
+
|
33
|
+
// Traverse all the nodes.
|
34
|
+
while (walker.nextNode()) {
|
35
|
+
listOfShadowRoots.push(walker.currentNode);
|
36
|
+
|
37
|
+
if (walker.currentNode.shadowRoot) {
|
38
|
+
listOfShadowRoots = listOfShadowRoots.concat(findShadowRoots(walker.currentNode.shadowRoot));
|
39
|
+
}
|
40
|
+
|
41
|
+
}
|
42
|
+
return listOfShadowRoots;
|
43
|
+
}
|
44
|
+
|
45
|
+
/**
|
46
|
+
* This function finds elements within a shadow DOM that match a given selector.
|
47
|
+
* @param node - A DOM element that has a shadow root.
|
48
|
+
* @param matchSelector - Criteria to select the elements in the dom starting with element as root element.
|
49
|
+
* @returns The function `findElementsInShadowDomByMatchSector` returns a NodeList of all elements
|
50
|
+
* within the shadow DOM of the `shadowElement` parameter that match the `matchSelector` parameter.
|
51
|
+
*/
|
52
|
+
|
53
|
+
function findElementsInShadowDomByMatchSector (node, matchSelector) {
|
54
|
+
if (node && node.shadowRoot && matchSelector) {
|
55
|
+
return node.shadowRoot.querySelectorAll(matchSelector)
|
56
|
+
}
|
57
|
+
}
|
58
|
+
|
59
|
+
/**
|
60
|
+
* Finds elements in the DOM including open shadowDOM.
|
61
|
+
* @param {*} node Root element to query for particular elements
|
62
|
+
* @param {*} matchSelector Criteria to select the elements in the dom starting with element as root element.
|
63
|
+
* @returns Nodes matching with match selector.
|
64
|
+
*/
|
65
|
+
function shadowQuerySelectorAll (node, matchSelector) {
|
66
|
+
|
67
|
+
/* Checks if both `element` and `matchSelector` are truthy values. If either of
|
68
|
+
them is falsy, it throws an error. */
|
69
|
+
if (!(node && matchSelector)) {
|
70
|
+
throw new Error(`Unable to find ${node} or ${matchSelector}`)
|
71
|
+
}
|
72
|
+
|
73
|
+
// Get the elements by query selector all.
|
74
|
+
const nodeList = node.querySelectorAll(matchSelector);
|
75
|
+
const shadowRoots = findShadowRoots(node);
|
76
|
+
let elements = [];
|
77
|
+
|
78
|
+
for (const rootElement of shadowRoots) {
|
79
|
+
const filteredElements = findElementsInShadowDomByMatchSector(rootElement, matchSelector);
|
80
|
+
if (filteredElements.length > 0) {
|
81
|
+
elements.push(...filteredElements);
|
82
|
+
}
|
83
|
+
}
|
84
|
+
|
85
|
+
return [...nodeList, ...elements];
|
86
|
+
}
|
87
|
+
|
88
|
+
module.exports = {
|
89
|
+
findShadowRoots,
|
90
|
+
filterShadowHosts,
|
91
|
+
shadowQuerySelectorAll,
|
92
|
+
findElementsInShadowDomByMatchSector
|
93
|
+
}
|