@cypress/unique-selector 2.0.1 → 2.1.1
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 +6 -0
- package/lib/getTag.js +24 -1
- package/lib/index.js +8 -3
- package/lib/isUnique.js +10 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -15,6 +15,7 @@ This is particularly useful when tracking in custom variables in analytics:
|
|
|
15
15
|
_gaq.push(['_trackEvent', 'Engagement', 'Click', selector]);
|
|
16
16
|
}, false);
|
|
17
17
|
|
|
18
|
+
Selector uniqueness is determined based on the given element's root node. Elements rendered within Shadow DOM will derive a selector unique within the associated ShadowRoot context. Otherwise, a selector unique within an element's owning document will be derived.
|
|
18
19
|
|
|
19
20
|
Installation
|
|
20
21
|
------------
|
|
@@ -74,6 +75,11 @@ Eric Clemmons : [@ericclemmons](https://twitter.com/ericclemmons)
|
|
|
74
75
|
|
|
75
76
|
Releases
|
|
76
77
|
--------
|
|
78
|
+
|
|
79
|
+
- v2.1.0
|
|
80
|
+
|
|
81
|
+
-
|
|
82
|
+
|
|
77
83
|
- v0.1.0
|
|
78
84
|
|
|
79
85
|
- Big refactor/rewrite using es6
|
package/lib/getTag.js
CHANGED
|
@@ -4,6 +4,10 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.getTag = getTag;
|
|
7
|
+
function isString(value) {
|
|
8
|
+
return typeof value === 'string';
|
|
9
|
+
}
|
|
10
|
+
|
|
7
11
|
/**
|
|
8
12
|
* Returns the Tag of the element
|
|
9
13
|
* @param { Object } element
|
|
@@ -11,7 +15,26 @@ exports.getTag = getTag;
|
|
|
11
15
|
* @return { String }
|
|
12
16
|
*/
|
|
13
17
|
function getTag(el, filter) {
|
|
14
|
-
var tagName = el.tagName
|
|
18
|
+
var tagName = el.tagName;
|
|
19
|
+
|
|
20
|
+
// If the tagName attribute has been overridden, we should
|
|
21
|
+
// check the nodeName property instead. If the nodeName property
|
|
22
|
+
// is also not a string, we should return null and ignore tagName
|
|
23
|
+
// for selector generation.
|
|
24
|
+
//
|
|
25
|
+
// This can happen when a <form> element contains an <input>
|
|
26
|
+
// with an id of `tagName`. In this case, the form element's
|
|
27
|
+
// tagName property is a reference to the input element, not
|
|
28
|
+
// a string.
|
|
29
|
+
if (!isString(tagName)) {
|
|
30
|
+
tagName = el.nodeName;
|
|
31
|
+
|
|
32
|
+
if (!isString(tagName)) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
tagName = tagName.toLowerCase().replace(/:/g, '\\:');
|
|
15
38
|
|
|
16
39
|
if (filter && !filter('tag', 'tag', tagName)) {
|
|
17
40
|
return null;
|
package/lib/index.js
CHANGED
|
@@ -244,7 +244,9 @@ function getUniqueSelector(element, selectorTypes, attributesToIgnore, filter) {
|
|
|
244
244
|
}
|
|
245
245
|
|
|
246
246
|
/**
|
|
247
|
-
* Generate unique CSS selector for given DOM element
|
|
247
|
+
* Generate unique CSS selector for given DOM element. Selector uniqueness is determined based on the given element's root node.
|
|
248
|
+
* Elements rendered within Shadow DOM will derive a selector that is unique within the associated ShadowRoot context.
|
|
249
|
+
* Otherwise, a selector that is unique within the element's owning document will be derived.
|
|
248
250
|
*
|
|
249
251
|
* @param {Element} el
|
|
250
252
|
* @param {Object} options (optional) Customize various behaviors of selector generation
|
|
@@ -256,7 +258,6 @@ function getUniqueSelector(element, selectorTypes, attributesToIgnore, filter) {
|
|
|
256
258
|
* @return {String}
|
|
257
259
|
* @api private
|
|
258
260
|
*/
|
|
259
|
-
|
|
260
261
|
function unique(el) {
|
|
261
262
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
262
263
|
var _options$selectorType = options.selectorTypes,
|
|
@@ -301,7 +302,11 @@ function unique(el) {
|
|
|
301
302
|
if (isUniqueSelector) {
|
|
302
303
|
return maybeUniqueSelector;
|
|
303
304
|
}
|
|
304
|
-
|
|
305
|
+
|
|
306
|
+
// Using parentElement here (rather than parentNode) to
|
|
307
|
+
// filter out any document/document fragment nodes that may
|
|
308
|
+
// be ancestors to elements within Shadow DOM trees.
|
|
309
|
+
currentElement = currentElement.parentElement;
|
|
305
310
|
}
|
|
306
311
|
|
|
307
312
|
return null;
|
package/lib/isUnique.js
CHANGED
|
@@ -5,15 +5,22 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.isUnique = isUnique;
|
|
7
7
|
/**
|
|
8
|
-
* Checks if the selector is unique
|
|
8
|
+
* Checks if the selector is unique. The selector is unique
|
|
9
|
+
* if the elements root node (either its owner document, or a shadow root)
|
|
10
|
+
* has exactly one element matching the selector.
|
|
11
|
+
*
|
|
9
12
|
* @param { Object } element
|
|
10
13
|
* @param { String } selector
|
|
11
|
-
* @return {
|
|
14
|
+
* @return { Boolean }
|
|
12
15
|
*/
|
|
13
16
|
function isUnique(el, selector) {
|
|
14
17
|
if (!Boolean(selector)) return false;
|
|
15
18
|
try {
|
|
16
|
-
|
|
19
|
+
// Using getRootNode here to scope checks to any parent
|
|
20
|
+
// ShadowRoot. getRootNode will otherwise return the
|
|
21
|
+
// document associated to the elements page/frame (like
|
|
22
|
+
// the ownerDocument property would).
|
|
23
|
+
var elems = el.getRootNode().querySelectorAll(selector);
|
|
17
24
|
return elems.length === 1 && elems[0] === el;
|
|
18
25
|
} catch (e) {
|
|
19
26
|
return false;
|
package/package.json
CHANGED