From 140603456cbbdb7ffddd2f77d5232a18d405def9 Mon Sep 17 00:00:00 2001 From: Angus MacDonald Date: Thu, 3 Dec 2020 13:04:20 -0600 Subject: [PATCH] Add getMatchingParentElement() --- src/dom/get-matching-parent-element.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/dom/get-matching-parent-element.ts diff --git a/src/dom/get-matching-parent-element.ts b/src/dom/get-matching-parent-element.ts new file mode 100644 index 0000000000..d51d168d58 --- /dev/null +++ b/src/dom/get-matching-parent-element.ts @@ -0,0 +1,16 @@ +/** + * Returns a parent element of the given element that returns true for the + * given test function. + */ +export function getMatchingParentElement( + element: HTMLElement, testFunction: (element: HTMLElement) => boolean +): HTMLElement { + let candidate = element.parentElement; + while (candidate) { + if (testFunction(candidate)) { + return candidate; + } + candidate = candidate.parentElement; + } + return undefined; +}