You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
rsvp-reader/components/generic/ParentsIterator.js

29 lines
620 B
JavaScript

5 years ago
/**
* Returns the parent of a node.
* Breaks through shadow roots.
* Returns null if node has no parent.
* @param {Node} node
*/
function getParent(node) {
let parent = node.parentNode
if (!parent) return null
return parent.host ? parent.host : parent
}
/**
* Returns an Iterator that walks the DOM tree upwards.
* @param {Node} node
*/
export function createParentsIterator(node) {
let curNode = node
return {
next: function() {
curNode = getParent(curNode)
return curNode ? { value: curNode } : { done: true }
},
[Symbol.iterator]: function() {
return this
}
}
}