UNPKG

@qntm-code/utils

Version:

A collection of useful utility functions with associated TypeScript types. All functions have been unit tested.

21 lines (20 loc) 594 B
import { isNullOrUndefined } from '../type-predicates/isNullOrUndefined.js'; /** * Gets all the elements that a given element is nested within */ export function getAncestors(element) { return _getAncestors(element); } /** * Internal function for building the array and navigating up the tree */ function _getAncestors(element, result = []) { const parent = element.parentNode; if (!isNullOrUndefined(parent) && parent.nodeType === parent.ELEMENT_NODE) { result.push(parent); return _getAncestors(parent, result); } else { return result; } }