aboutsummaryrefslogtreecommitdiff
path: root/node_modules/delegate/src/closest.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/delegate/src/closest.js')
-rw-r--r--node_modules/delegate/src/closest.js33
1 files changed, 33 insertions, 0 deletions
diff --git a/node_modules/delegate/src/closest.js b/node_modules/delegate/src/closest.js
new file mode 100644
index 00000000..842ea074
--- /dev/null
+++ b/node_modules/delegate/src/closest.js
@@ -0,0 +1,33 @@
+var DOCUMENT_NODE_TYPE = 9;
+
+/**
+ * A polyfill for Element.matches()
+ */
+if (typeof Element !== 'undefined' && !Element.prototype.matches) {
+ var proto = Element.prototype;
+
+ proto.matches = proto.matchesSelector ||
+ proto.mozMatchesSelector ||
+ proto.msMatchesSelector ||
+ proto.oMatchesSelector ||
+ proto.webkitMatchesSelector;
+}
+
+/**
+ * Finds the closest parent that matches a selector.
+ *
+ * @param {Element} element
+ * @param {String} selector
+ * @return {Function}
+ */
+function closest (element, selector) {
+ while (element && element.nodeType !== DOCUMENT_NODE_TYPE) {
+ if (typeof element.matches === 'function' &&
+ element.matches(selector)) {
+ return element;
+ }
+ element = element.parentNode;
+ }
+}
+
+module.exports = closest;