Created
April 8, 2020 23:55
-
-
Save jonathantneal/bac9124b25fa49f4f5cb35a40195d236 to your computer and use it in GitHub Desktop.
Closest, but with an extra argument to specify the ancestor to look up until
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
closest = (function (Object, error) { | |
return function (targetElement, selectors, untilElement) { | |
let document = Object(Object(targetElement).ownerDocument) | |
let rootElement = Object(document.documentElement) | |
let Element = Object(document.defaultView).Element | |
untilElement = untilElement || rootElement | |
if (typeof Element === 'function' && targetElement instanceof Element && untilElement instanceof Element) { | |
while (untilElement.contains(targetElement)) { | |
if (targetElement.matches(selectors)) { | |
return targetElement | |
} | |
targetElement = targetElement.parentElement | |
} | |
return null | |
} else { | |
throw new TypeError('Illegal invocation') | |
} | |
} | |
})(Object) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment