@cypress/unique-selector 2.0.0 → 2.1.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/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/getClasses.js CHANGED
@@ -22,7 +22,7 @@ function getClasses(el, filter) {
22
22
 
23
23
  try {
24
24
  return Array.prototype.slice.call(el.classList).filter(function (cls) {
25
- return !filter || filter('attribute', 'class', cls);
25
+ return !filter || filter('class', 'class', cls);
26
26
  });
27
27
  } catch (e) {
28
28
  var className = el.getAttribute('class');
package/lib/index.js CHANGED
@@ -35,7 +35,7 @@ var attrRegex = /^attribute:(.+)/m;
35
35
  /**
36
36
  * @typedef Filter
37
37
  * @type {Function}
38
- * @param {string} type - the trait being considered ('attribute', 'tag', 'nth-child').
38
+ * @param {string} type - the trait being considered ('attribute', 'tag', 'nth-child'). As a special case, the `class` attribute is split on whitespace and each token passed individually with a `class` type.
39
39
  * @param {string} key - your trait key (for 'attribute' will be the attribute name, for others will typically be the same as 'type').
40
40
  * @param {string} value - the trait value.
41
41
  * @returns {boolean} whether this trait can be used when building the selector (true = allow). Defaults to 'true' if no value returned.
@@ -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
- currentElement = currentElement.parentNode;
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 { Array }
14
+ * @return { Boolean }
12
15
  */
13
16
  function isUnique(el, selector) {
14
17
  if (!Boolean(selector)) return false;
15
18
  try {
16
- var elems = el.ownerDocument.querySelectorAll(selector);
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cypress/unique-selector",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "Given a DOM node, return a unique CSS selector matching only that element",
5
5
  "main": "./lib/index.js",
6
6
  "jsnext:main": "./src/index.js",