bv-ui-core 2.7.0 → 2.8.1
Sign up to get free protection for your applications and to get access to all the features.
- package/.eslintrc +2 -1
- package/README.md +1 -0
- package/lib/global/index.js +30 -5
- package/lib/queryShadowDom/README.md +50 -0
- package/lib/queryShadowDom/index.js +93 -0
- package/package.json +1 -1
- package/test/unit/global/index.spec.js +9 -0
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/
|
package/lib/global/index.js
CHANGED
@@ -6,11 +6,36 @@
|
|
6
6
|
'use strict'; /* eslint strict:0 */
|
7
7
|
|
8
8
|
var getGlobal = function () {
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
var globalObj = (function () {
|
10
|
+
if (typeof globalThis !== 'undefined') { return globalThis; }
|
11
|
+
if (typeof window !== 'undefined') { return window; }
|
12
|
+
if (typeof self !== 'undefined') { return self; }
|
13
|
+
if (typeof global !== 'undefined') { return global; }
|
14
|
+
throw new Error('unable to locate global object');
|
15
|
+
})()
|
16
|
+
/*
|
17
|
+
The below code was added in case there is pollution in global namespace
|
18
|
+
windows object during transpilation with __esModule being set. Below code would support
|
19
|
+
global import in all bundle use cases
|
20
|
+
*/
|
21
|
+
if (globalObj && globalObj.__esModule) {
|
22
|
+
const override = {
|
23
|
+
get: function (target, prop, receiver) {
|
24
|
+
if (prop === 'default') {
|
25
|
+
return target
|
26
|
+
}
|
27
|
+
|
28
|
+
return Reflect.get(receiver, prop, target)
|
29
|
+
},
|
30
|
+
|
31
|
+
set: function (target, prop, value) {
|
32
|
+
Reflect.set(target, prop, value)
|
33
|
+
},
|
34
|
+
};
|
35
|
+
const proxyGlobal = new Proxy(globalObj, override)
|
36
|
+
return proxyGlobal
|
37
|
+
}
|
38
|
+
return globalObj
|
14
39
|
};
|
15
40
|
|
16
41
|
module.exports = getGlobal();
|
@@ -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
|
+
}
|
package/package.json
CHANGED
@@ -15,4 +15,13 @@ describe('lib/global', function () {
|
|
15
15
|
}
|
16
16
|
});
|
17
17
|
|
18
|
+
it('should export window with default pointing to windows if __esModule is set', function () {
|
19
|
+
// If we have access to the window object, compare against it.
|
20
|
+
if (typeof window !== 'undefined') {
|
21
|
+
window.__esModule = true
|
22
|
+
var global = require('../../../lib/global')
|
23
|
+
expect(global).to.eql(window);
|
24
|
+
}
|
25
|
+
});
|
26
|
+
|
18
27
|
});
|