-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
61 lines (53 loc) · 2.03 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
49
50
51
52
53
54
55
56
57
58
59
60
61
module.exports = {
/**
* Normalizes a collection of elements into a native array of elements.
*
* @param {HTMLElement|Array|NodeList|String} elements One or more HTMLElements or a CSS selector
* @return {HTMLElement} An array of HTMLElements
*/
'toElementArray' : function(elements) {
if(typeof elements === 'string') {
return this.toElementArray(document.querySelectorAll(elements));
} else if(elements instanceof HTMLElement) {
return [elements];
} else {
return ((elements !== null) && elements.length)
? Array.prototype.slice.call(elements)
: [];
}
},
/**
* Finds ancestor elements of a given node.
*
* @param {HTMLElement} element An HTMLElement
* @param {Integer|Null} limit The number of ancestor elements to return, or NULL to return all
* @return {Array} The ancestor node, or null if no common ancestor could be found
*/
'getAncestry' : function(element, limit) {
var ancestry = [];
while((element = element.parentElement) && (!limit || (ancestry.length < limit))) {
ancestry.push(element);
}
return ancestry;
},
/**
* Given a list of nodes, finds the common ancestor element.
*
* @param {HTMLElement|Array|NodeList|String} elements One or more HTMLElements or a CSS selector
* @return {HTMLElement|Null} The ancestor node, or null if no common ancestor could be found
*/
'getCommonAncestor' : function(elements) {
elements = this.toElementArray(elements);
var first = elements[0],
rest = elements.slice(1);
while(first) {
if(rest.every(function(other) {
return first.contains(other);
})) {
return first;
}
first = first.parentElement;
}
return null;
}
};