12 lines
379 B
JavaScript
12 lines
379 B
JavaScript
|
import matches from './matches';
|
||
|
export default function closest(node, selector, stopAt) {
|
||
|
if (node.closest && !stopAt) node.closest(selector);
|
||
|
var nextNode = node;
|
||
|
|
||
|
do {
|
||
|
if (matches(nextNode, selector)) return nextNode;
|
||
|
nextNode = nextNode.parentElement;
|
||
|
} while (nextNode && nextNode !== stopAt && nextNode.nodeType === document.ELEMENT_NODE);
|
||
|
|
||
|
return null;
|
||
|
}
|