-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (41 loc) · 1.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
var DOMUtils = require('dom-utils'),
SelectorUtils = require('./selector-utils'),
SelectorGenerator = require('./selector-generator');
/**
* An interface for generating selectors for a set of elements.
*
* @class Selector
* @param {HTMLElement|Array|NodeList|String} includes One or more HTMLElements or a CSS selector
* @constructor
*/
module.exports = function Selector(elements) {
this.elements = DOMUtils.toElementArray(elements);
this.ancestor = DOMUtils.getCommonAncestor(elements);
this.generate = function() {
if(this.elements.length === 1) {
return SelectorUtils.bestFit(this.elements[0]);
}
var ancestorSelector = this.ancestor ? SelectorUtils.bestFit(this.ancestor) : '',
remaining = this.elements,
selectors = [],
filtered = false,
generator,
selector;
// Generate branch selectors
while(remaining.length) {
filtered = false,
generator = SelectorGenerator(remaining[0], this.ancestor);
while(!filtered && (selector = generator.next().value)) {
filtered = SelectorUtils.filter(selector, remaining, this.ancestor);
}
selectors.push(selector);
remaining = filtered;
}
return selectors
.map(function(selector) {
return SelectorUtils.makeTbodyOptional(ancestorSelector + ' ' + selector);
})
.join(", \n")
.trim();
}
}