add findClosestAncestor

Function to find the closest ancestor of an element that matches the selection criterion
This commit is contained in:
JeLuF 2022-12-24 00:58:52 +01:00 committed by GitHub
parent c5d343750c
commit 21108650f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,6 +20,19 @@ function getNextSibling(elem, selector) {
} }
} }
function findClosestAncestor(element, selector) {
if (!element || !element.parentNode) {
// reached the top of the DOM tree, return null
return null;
} else if (element.parentNode.matches(selector)) {
// found an ancestor that matches the selector, return it
return element.parentNode;
} else {
// continue searching upwards
return findClosestAncestor(element.parentNode, selector);
}
}
/* Panel Stuff */ /* Panel Stuff */