@cypress/unique-selector 0.4.4 → 1.0.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/lib/getName.js ADDED
@@ -0,0 +1,19 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getName = getName;
7
+ /**
8
+ * Returns the `name` attribute of the element (if one exists)
9
+ * @param { Object } element
10
+ * @return { String }
11
+ */
12
+ function getName(el) {
13
+ var name = el.getAttribute('name');
14
+
15
+ if (name !== null && name !== '') {
16
+ return '[name="' + name + '"]';
17
+ }
18
+ return null;
19
+ }
package/lib/index.js CHANGED
@@ -13,6 +13,8 @@ var _getCombinations = require('./getCombinations');
13
13
 
14
14
  var _getAttributes = require('./getAttributes');
15
15
 
16
+ var _getName = require('./getName');
17
+
16
18
  var _getNthChild = require('./getNthChild');
17
19
 
18
20
  var _getTag = require('./getTag');
@@ -42,7 +44,8 @@ function getAllSelectors(el, selectors, attributesToIgnore) {
42
44
  return (0, _getAttributes.getAttributes)(elem, attributesToIgnore);
43
45
  },
44
46
  'class': _getClasses.getClassSelectors,
45
- 'id': _getID.getID
47
+ 'id': _getID.getID,
48
+ 'name': _getName.getName
46
49
  };
47
50
 
48
51
  return selectors.filter(function (selector) {
@@ -62,8 +65,12 @@ function getAllSelectors(el, selectors, attributesToIgnore) {
62
65
  function testUniqueness(element, selector) {
63
66
  var parentNode = element.parentNode;
64
67
 
65
- var elements = parentNode.querySelectorAll(selector);
66
- return elements.length === 1 && elements[0] === element;
68
+ try {
69
+ var elements = parentNode.querySelectorAll(selector);
70
+ return elements.length === 1 && elements[0] === element;
71
+ } catch (e) {
72
+ return false;
73
+ }
67
74
  }
68
75
 
69
76
  /**
@@ -144,6 +151,7 @@ function getUniqueSelector(element, selectorTypes, attributesToIgnore) {
144
151
  switch (selectorType) {
145
152
  case 'data':
146
153
  case 'id':
154
+ case 'name':
147
155
  case 'tag':
148
156
  if (testUniqueness(element, selector)) {
149
157
  return selector;
@@ -195,69 +203,39 @@ function getUniqueSelector(element, selectorTypes, attributesToIgnore) {
195
203
  function unique(el) {
196
204
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
197
205
  var _options$selectorType = options.selectorTypes,
198
- selectorTypes = _options$selectorType === undefined ? ['id', 'class', 'tag', 'nth-child'] : _options$selectorType,
206
+ selectorTypes = _options$selectorType === undefined ? ['id', 'name', 'class', 'tag', 'nth-child'] : _options$selectorType,
199
207
  _options$attributesTo = options.attributesToIgnore,
200
- attributesToIgnore = _options$attributesTo === undefined ? ['id', 'class', 'length'] : _options$attributesTo;
208
+ attributesToIgnore = _options$attributesTo === undefined ? ['id', 'class', 'length'] : _options$attributesTo,
209
+ selectorCache = options.selectorCache,
210
+ isUniqueCache = options.isUniqueCache;
201
211
 
202
212
  var allSelectors = [];
203
- var parents = (0, _getParents.getParents)(el);
204
213
 
205
- var _iteratorNormalCompletion2 = true;
206
- var _didIteratorError2 = false;
207
- var _iteratorError2 = undefined;
214
+ var currentElement = el;
215
+ while (currentElement) {
216
+ var selector = selectorCache ? selectorCache.get(currentElement) : undefined;
208
217
 
209
- try {
210
- for (var _iterator2 = parents[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
211
- var elem = _step2.value;
212
-
213
- var selector = getUniqueSelector(elem, selectorTypes, attributesToIgnore);
214
- if (Boolean(selector)) {
215
- allSelectors.push(selector);
216
- }
217
- }
218
- } catch (err) {
219
- _didIteratorError2 = true;
220
- _iteratorError2 = err;
221
- } finally {
222
- try {
223
- if (!_iteratorNormalCompletion2 && _iterator2.return) {
224
- _iterator2.return();
225
- }
226
- } finally {
227
- if (_didIteratorError2) {
228
- throw _iteratorError2;
218
+ if (!selector) {
219
+ selector = getUniqueSelector(currentElement, selectorTypes, attributesToIgnore);
220
+ if (selectorCache) {
221
+ selectorCache.set(currentElement, selector);
229
222
  }
230
223
  }
231
- }
232
-
233
- var selectors = [];
234
- var _iteratorNormalCompletion3 = true;
235
- var _didIteratorError3 = false;
236
- var _iteratorError3 = undefined;
237
-
238
- try {
239
- for (var _iterator3 = allSelectors[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
240
- var it = _step3.value;
241
224
 
242
- selectors.unshift(it);
243
- var _selector = selectors.join(' > ');
244
- if ((0, _isUnique.isUnique)(el, _selector)) {
245
- return _selector;
225
+ allSelectors.unshift(selector);
226
+ var maybeUniqueSelector = allSelectors.join(' > ');
227
+ var isUniqueSelector = isUniqueCache ? isUniqueCache.get(maybeUniqueSelector) : undefined;
228
+ if (isUniqueSelector === undefined) {
229
+ isUniqueSelector = (0, _isUnique.isUnique)(el, maybeUniqueSelector);
230
+ if (isUniqueCache) {
231
+ isUniqueCache.set(maybeUniqueSelector, isUniqueSelector);
246
232
  }
247
233
  }
248
- } catch (err) {
249
- _didIteratorError3 = true;
250
- _iteratorError3 = err;
251
- } finally {
252
- try {
253
- if (!_iteratorNormalCompletion3 && _iterator3.return) {
254
- _iterator3.return();
255
- }
256
- } finally {
257
- if (_didIteratorError3) {
258
- throw _iteratorError3;
259
- }
234
+
235
+ if (isUniqueSelector) {
236
+ return maybeUniqueSelector;
260
237
  }
238
+ currentElement = currentElement.parentNode;
261
239
  }
262
240
 
263
241
  return null;
package/lib/isUnique.js CHANGED
@@ -12,6 +12,10 @@ exports.isUnique = isUnique;
12
12
  */
13
13
  function isUnique(el, selector) {
14
14
  if (!Boolean(selector)) return false;
15
- var elems = el.ownerDocument.querySelectorAll(selector);
16
- return elems.length === 1 && elems[0] === el;
15
+ try {
16
+ var elems = el.ownerDocument.querySelectorAll(selector);
17
+ return elems.length === 1 && elems[0] === el;
18
+ } catch (e) {
19
+ return false;
20
+ }
17
21
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cypress/unique-selector",
3
- "version": "0.4.4",
3
+ "version": "1.0.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",
@@ -21,7 +21,7 @@
21
21
  },
22
22
  "repository": {
23
23
  "type": "git",
24
- "url": "git://github.com/ericclemmons/unique-selector.git"
24
+ "url": "git://github.com/cypress-io/unique-selector.git"
25
25
  },
26
26
  "keywords": [
27
27
  "dom",
@@ -30,13 +30,7 @@
30
30
  "selector",
31
31
  "event"
32
32
  ],
33
- "author": "Eric Clemmons <eric@smarterspam.com>",
34
- "contributors": [
35
- {
36
- "name": "Avraam Mavridis",
37
- "email": "avr.mav@gmail.com"
38
- }
39
- ],
33
+ "author": "Cypress",
40
34
  "license": "MIT",
41
35
  "devDependencies": {
42
36
  "babel-cli": "^6.26.0",