-
-
Notifications
You must be signed in to change notification settings - Fork 778
/
Copy pathdom.js
48 lines (41 loc) · 941 Bytes
/
dom.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
/**
* Finds siblings nodes of the passed node.
*
* @param {Element} node
* @return {Array}
*/
export function siblings (node) {
if (node && node.parentNode) {
let n = node.parentNode.firstChild
const matched = []
for (; n; n = n.nextSibling) {
if (n.nodeType === 1 && n !== node) {
matched.push(n)
}
}
return matched
}
return []
}
/**
* Checks if passed node exist and is a valid element.
*
* @param {Element} node
* @return {Boolean}
*/
export function exist (node) {
// We are usine duck-typing here because we can't use `instanceof` since if we're in an iframe the class instance will be different.
if (node && node.appendChild && node.isConnected) {
return true
}
return false
}
/**
* Coerces a NodeList to an Array.
*
* @param {NodeList} nodeList
* @return {Array}
*/
export function toArray (nodeList) {
return Array.prototype.slice.call(nodeList)
}