mirror of
https://github.com/a2nt/webpack-bootstrap-ui-kit.git
synced 2024-06-14 16:41:35 +02:00
36 lines
851 B
JavaScript
36 lines
851 B
JavaScript
const funcs = {};
|
|
|
|
/*!
|
|
* Get all of an element's parent elements up the DOM tree
|
|
* (c) 2019 Chris Ferdinandi, MIT License, https://gomakethings.com
|
|
* @param {Node} elem The element
|
|
* @param {String} selector Selector to match against [optional]
|
|
* @return {Array} The parent elements
|
|
*/
|
|
|
|
funcs.getParents = (elem, selector) => {
|
|
// Setup parents array
|
|
const parents = [];
|
|
let el = elem;
|
|
// Get matching parent elements
|
|
while (el && el !== document) {
|
|
// If using a selector, add matching parents to array
|
|
// Otherwise, add all parents
|
|
if (selector) {
|
|
if (el.matches(selector)) {
|
|
parents.push(el);
|
|
}
|
|
} else {
|
|
parents.push(el);
|
|
}
|
|
|
|
// Jump to the next parent node
|
|
el = el.parentNode;
|
|
}
|
|
|
|
return parents;
|
|
};
|
|
|
|
module.exports = funcs;
|
|
module.exports.default = funcs;
|