Debian Codegraph @

delegate/src/closest.js

raw
1 var DOCUMENT_NODE_TYPE = 9;
2
3 /**
4 * A polyfill for Element.matches()
5 */
6 if (typeof Element !== 'undefined' && !Element.prototype.matches) {
7 var proto = Element.prototype;
8
9 proto.matches = proto.matchesSelector ||
10 proto.mozMatchesSelector ||
11 proto.msMatchesSelector ||
12 proto.oMatchesSelector ||
13 proto.webkitMatchesSelector;
14 }
15
16 /**
17 * Finds the closest parent that matches a selector.
18 *
19 * @param {Element} element
20 * @param {String} selector
21 * @return {Function}
22 */
23 function closest (element, selector) {
24 while (element && element.nodeType !== DOCUMENT_NODE_TYPE) {
25 if (typeof element.matches === 'function' &&
26 element.matches(selector)) {
27 return element;
28 }
29 element = element.parentNode;
30 }
31 }
32
33 module.exports = closest;