This repository has been archived on 2024-10-26. You can view files and clone it, but cannot push or open issues or pull requests.
staxman-old/static/scripts/node-utils.ts

16 lines
446 B
TypeScript
Raw Permalink Normal View History

2023-11-30 09:14:32 +00:00
/**
* Find nearest parent that satisfies a requirement
* @param node Node to start from
* @param findFn Matching function
* @returns Found element, or null if no parent matches
*/
export function findNearestParent(node: Node | Element, findFn: (el: Element) => boolean): HTMLElement | null {
let parent = node.parentElement;
while (parent) {
if (findFn(parent)) {
return parent;
}
parent = parent.parentElement;
}
return null;
}