bv-ui-core 2.6.0 → 2.8.0
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/catalog-info.yaml +30 -0
- package/karma.conf.js +12 -3
- package/lib/README.md +61 -0
- package/lib/messageFormat/README.md +10 -0
- package/lib/messageFormat/index.js +42 -0
- package/lib/performance/getEntriesByName.js +1 -0
- package/lib/queryShadowDom/README.md +50 -0
- package/lib/queryShadowDom/index.js +93 -0
- package/mkdocs.yaml +5 -0
- package/package.json +3 -3
- package/test/unit/messageFormat/index.spec.js +31 -0
- package/test/unit/performance/getEntriesByName.spec.js +6 -1
- package/test/unit/performance/getEntriesByType.spec.js +3 -2
- package/test/unit/performance/measure.spec.js +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,30 @@
|
|
1
|
+
apiVersion: backstage.io/v1alpha1
|
2
|
+
kind: Component
|
3
|
+
metadata:
|
4
|
+
name: bv-ui-core
|
5
|
+
github.com/project-slug: bazaarvoice/bv-ui-core
|
6
|
+
description: This project provides a central location for common Bazaarvoice UI code. It is intended to be installed into a project via NPM. Individual modules are authored as CommonJS modules, to be consumed by Webpack or another build tool that can ingest CommonJS.
|
7
|
+
annotations:
|
8
|
+
backstage.io/techdocs-ref: dir:.
|
9
|
+
github.com/project-slug: bazaarvoice/bv-ui-core
|
10
|
+
links:
|
11
|
+
- url: https://bazaarvoice.slack.com/app_redirect?channel=sme-swat
|
12
|
+
title: Slack channel
|
13
|
+
icon: send
|
14
|
+
- url: https://sonarqube.qa.bazaarvoice.com/dashboard?id=bazaarvoice_bv-ui-core
|
15
|
+
title: SonarQube
|
16
|
+
icon: dashboard
|
17
|
+
labels:
|
18
|
+
tech: javascript
|
19
|
+
tags:
|
20
|
+
- javascript
|
21
|
+
- nodejs
|
22
|
+
spec:
|
23
|
+
type: library
|
24
|
+
system: swat
|
25
|
+
lifecycle: production
|
26
|
+
owner: swat
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
|
package/karma.conf.js
CHANGED
@@ -20,12 +20,10 @@ module.exports = function (config) {
|
|
20
20
|
ui: 'bdd'
|
21
21
|
}
|
22
22
|
},
|
23
|
-
|
24
23
|
// list of files / patterns to load in the browser.
|
25
24
|
files: [
|
26
25
|
// We need the polyfill for testing es6 modules.
|
27
26
|
'node_modules/babel-polyfill/dist/polyfill.js',
|
28
|
-
|
29
27
|
// Loaded into the browser test page.
|
30
28
|
'test/unit/mochaInit.js',
|
31
29
|
'test/unit/**/*.spec.js',
|
@@ -109,10 +107,21 @@ module.exports = function (config) {
|
|
109
107
|
// Enable / disable watching file and executing tests whenever any file
|
110
108
|
// changes.
|
111
109
|
autoWatch: true,
|
110
|
+
customLaunchers: {
|
111
|
+
ChromeHeadless: {
|
112
|
+
base: 'Chrome',
|
113
|
+
flags: [
|
114
|
+
'--no-sandbox',
|
115
|
+
'--headless',
|
116
|
+
'--disable-gpu',
|
117
|
+
'--remote-debugging-port=9222'
|
118
|
+
]
|
119
|
+
}
|
120
|
+
},
|
112
121
|
|
113
122
|
// Start these browsers.
|
114
123
|
// See: https://npmjs.org/browse/keyword/karma-launcher
|
115
|
-
browsers: ['
|
124
|
+
browsers: ['ChromeHeadless'],
|
116
125
|
|
117
126
|
// Continuous Integration mode. If true, Karma captures browsers, runs the
|
118
127
|
// tests and exits.
|
package/lib/README.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
![](https://travis-ci.org/bazaarvoice/bv-ui-core.svg)
|
2
|
+
|
3
|
+
# bv-ui-core
|
4
|
+
|
5
|
+
This project provides a central location for common Bazaarvoice UI code. It is
|
6
|
+
intended to be installed into a project via NPM. Individual modules are authored
|
7
|
+
as CommonJS modules, to be consumed by Webpack or another build tool that can
|
8
|
+
ingest CommonJS.
|
9
|
+
|
10
|
+
**This is a public repository.** Please see the [contribution guidelines][1] for
|
11
|
+
details on contributing to this repo.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
You will need NPM to add this to your project; it is installed when you install
|
16
|
+
Node, so it is likely that you already have it. If not, you can install Node
|
17
|
+
using an [installer][2], or by using your favorite package manager (such as
|
18
|
+
Homebrew).
|
19
|
+
|
20
|
+
Once you have NPM, you can install bv-ui-core as follows:
|
21
|
+
|
22
|
+
```bash
|
23
|
+
npm install --save bv-ui-core
|
24
|
+
```
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
Require bv-ui-core modules into your code:
|
29
|
+
|
30
|
+
```javascript
|
31
|
+
var someModule = require('bv-ui-core/lib/someModule');
|
32
|
+
someModule.doThings();
|
33
|
+
```
|
34
|
+
|
35
|
+
For details on how to use a specific module, see the README document in the
|
36
|
+
module's directory.
|
37
|
+
|
38
|
+
## Modules
|
39
|
+
|
40
|
+
- [body](./lib/body)
|
41
|
+
- [checkHighContrast](./lib/checkHighContrast)
|
42
|
+
- [cookie](./lib/cookie)
|
43
|
+
- [cookieConsent](./lib/cookieConsent)
|
44
|
+
- [cssLoadChecker](./lib/cssLoadChecker)
|
45
|
+
- [date.now](./lib/date.now)
|
46
|
+
- [domainPolice](./lib/domainPolice)
|
47
|
+
- [evented](./lib/evented)
|
48
|
+
- [getOriginalConstructor](./lib/getOriginalConstructor)
|
49
|
+
- [global](./lib/global)
|
50
|
+
- [ie](./lib/ie)
|
51
|
+
- [loader](./lib/loader)
|
52
|
+
- [logger](./lib/logger)
|
53
|
+
- [namespacer](./lib/namespacer)
|
54
|
+
- [performance](./lib/performance)
|
55
|
+
- [polyfills](./lib/polyfills)
|
56
|
+
- [pixelsDisplayed](./lib/pixelsDisplayed)
|
57
|
+
- [staticAssetLoader](./lib/staticAssetLoader)
|
58
|
+
- [waitForBody](./lib/waitForBody)
|
59
|
+
|
60
|
+
[1]: ./CONTRIBUTING.md
|
61
|
+
[2]: https://nodejs.org/download/
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# messageFormat
|
2
|
+
|
3
|
+
Uses `INTL.DisplayNames` and uses a polyfill from `https://cdn.polyfill.io/v3/polyfill.min.js?features=Intl.DisplayNames`
|
4
|
+
|
5
|
+
# Usage
|
6
|
+
|
7
|
+
```javascript
|
8
|
+
var messageFormat = require('bv-ui-core/lib/messageFormat');
|
9
|
+
messageFormat.generateCountryFromLocale('en_US') // United States
|
10
|
+
```
|
@@ -0,0 +1,42 @@
|
|
1
|
+
/**
|
2
|
+
* @fileOverview
|
3
|
+
* messageFormat provides utility functions to which use the INTL API which provides
|
4
|
+
* language sensitive string comparison, number formatting, and date and time formatting.
|
5
|
+
*
|
6
|
+
* messageFormat can have functions which are used in multiple places
|
7
|
+
*
|
8
|
+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl
|
9
|
+
*/
|
10
|
+
|
11
|
+
|
12
|
+
var global = require('../global');
|
13
|
+
/**
|
14
|
+
* Generates the country name associated with the given locale
|
15
|
+
* @param {string} locale The locale code (e.g., 'en_US')
|
16
|
+
* @returns {string} The name of the country associated with the locale, or an empty string if the locale is not supported by Intl.DisplayNames
|
17
|
+
*/
|
18
|
+
function generateCountryFromLocale (locale) {
|
19
|
+
if (!locale || locale.length === 0) {
|
20
|
+
return '';
|
21
|
+
}
|
22
|
+
|
23
|
+
// Split the locale code into language and region components
|
24
|
+
try {
|
25
|
+
var localeArr = locale.split('_');
|
26
|
+
if (localeArr.length < 2) {
|
27
|
+
return '';
|
28
|
+
}
|
29
|
+
// Get the display name for the region (i.e., the country name)
|
30
|
+
var regionNames = new global.Intl.DisplayNames(localeArr[0], { type: 'region' });
|
31
|
+
var countryName = regionNames.of(localeArr[1]);
|
32
|
+
}
|
33
|
+
catch (error) {
|
34
|
+
return '';
|
35
|
+
}
|
36
|
+
// Return the country name, or an empty string if the region is not valid
|
37
|
+
return countryName
|
38
|
+
}
|
39
|
+
|
40
|
+
module.exports = {
|
41
|
+
generateCountryFromLocale: generateCountryFromLocale
|
42
|
+
}
|
@@ -53,6 +53,7 @@ module.exports = {
|
|
53
53
|
getEntriesByName: function (name, entryType) {
|
54
54
|
// Note: because the implementation functions explicitly check their number of arguments,
|
55
55
|
// we want to preserve the calls exactly as they came in
|
56
|
+
|
56
57
|
var hasArguments = (arguments.length > 0);
|
57
58
|
var func = (isNativeSupported ? nativeImplementation : polyfillImplementation);
|
58
59
|
return (hasArguments ? func(name, entryType) : func());
|
@@ -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/mkdocs.yaml
ADDED
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "bv-ui-core",
|
3
|
-
"version": "2.
|
3
|
+
"version": "2.8.0",
|
4
4
|
"license": "Apache 2.0",
|
5
5
|
"description": "Bazaarvoice UI-related JavaScript",
|
6
6
|
"repository": {
|
@@ -12,7 +12,7 @@
|
|
12
12
|
},
|
13
13
|
"scripts": {
|
14
14
|
"lint": "eslint karma.conf.js lib test",
|
15
|
-
"test": "./node_modules/karma/bin/karma start --single-run --browsers
|
15
|
+
"test": "./node_modules/karma/bin/karma start --single-run --browsers ChromeHeadless",
|
16
16
|
"dev": "./node_modules/karma/bin/karma start --browsers Chrome"
|
17
17
|
},
|
18
18
|
"devDependencies": {
|
@@ -28,7 +28,7 @@
|
|
28
28
|
"json-loader": "^0.5.7",
|
29
29
|
"karma": "^3.0.0",
|
30
30
|
"karma-chai": "^0.1.0",
|
31
|
-
"karma-chrome-launcher": "2.0.0",
|
31
|
+
"karma-chrome-launcher": "^2.0.0",
|
32
32
|
"karma-coverage": "^1.1.2",
|
33
33
|
"karma-firefox-launcher": "^1.1.0",
|
34
34
|
"karma-htmlfile-reporter": "^0.3.6",
|
@@ -0,0 +1,31 @@
|
|
1
|
+
/**
|
2
|
+
* @fileOverview
|
3
|
+
* Unit tests for the messageFormat module
|
4
|
+
*/
|
5
|
+
var messageFormat = require('../../../lib/messageFormat');
|
6
|
+
|
7
|
+
describe('lib/messageFormat', function () {
|
8
|
+
it('messageFormat.generateCountryFromLocale', function () {
|
9
|
+
function test1 () {
|
10
|
+
return messageFormat.generateCountryFromLocale('en_US');
|
11
|
+
}
|
12
|
+
|
13
|
+
expect(test1()).to.be.equal('United States');
|
14
|
+
|
15
|
+
function test2 () {
|
16
|
+
return messageFormat.generateCountryFromLocale('en_DE');
|
17
|
+
}
|
18
|
+
|
19
|
+
expect(test2()).to.be.equal('Germany')
|
20
|
+
|
21
|
+
function test3 () {
|
22
|
+
return messageFormat.generateCountryFromLocale('de_DE');
|
23
|
+
}
|
24
|
+
expect(test3()).to.be.equal('Deutschland')
|
25
|
+
|
26
|
+
function test4 () {
|
27
|
+
return messageFormat.generateCountryFromLocale()
|
28
|
+
}
|
29
|
+
expect(test4()).to.be.empty
|
30
|
+
});
|
31
|
+
});
|
@@ -76,8 +76,13 @@ describe('lib/performance/getEntriesByName', function () {
|
|
76
76
|
expect(result.length).to.equal(0);
|
77
77
|
});
|
78
78
|
|
79
|
+
|
79
80
|
it('throws an error if no arguments are passed', function () {
|
80
|
-
|
81
|
+
//name is passed to the nativeImplementation which is undefined and returns an empty array
|
82
|
+
function test () {
|
83
|
+
return perfGetEntriesByName.getEntriesByName()
|
84
|
+
}
|
85
|
+
expect(test()).to.be.empty;
|
81
86
|
});
|
82
87
|
|
83
88
|
});
|
@@ -9,13 +9,14 @@ var perfMark = require('../../../lib/performance/mark.js');
|
|
9
9
|
|
10
10
|
describe('lib/performance/getEntriesByType', function () {
|
11
11
|
before(function () {
|
12
|
+
//Clearing all the marks as marks from other tests were being added
|
13
|
+
performance.clearMarks()
|
12
14
|
perfMark.mark('test-by-name1');
|
13
15
|
perfMark.mark('test-by-name2');
|
14
16
|
});
|
15
17
|
|
16
18
|
it('returns an array of all matching performance marks by type', function () {
|
17
19
|
var result = perfGetEntriesByType.getEntriesByType('mark');
|
18
|
-
|
19
20
|
// Test 1: it returns an array.
|
20
21
|
expect(result).to.be.an('array');
|
21
22
|
|
@@ -41,7 +42,7 @@ describe('lib/performance/getEntriesByType', function () {
|
|
41
42
|
});
|
42
43
|
|
43
44
|
it('throws an error if no arguments are passed', function () {
|
44
|
-
expect(
|
45
|
+
expect(perfGetEntriesByType.getEntriesByType()).to.be.empty;
|
45
46
|
});
|
46
47
|
|
47
48
|
});
|
@@ -32,7 +32,7 @@ describe('lib/performance/measure', function () {
|
|
32
32
|
});
|
33
33
|
|
34
34
|
it('throws an error if the measure name is omitted', function () {
|
35
|
-
expect(
|
35
|
+
expect(perfMeasure.measure()).to.be.empty;
|
36
36
|
});
|
37
37
|
|
38
38
|
it('throws an error if the startMark or endMark name is supplied, but is invalid', function () {
|