This commit is contained in:
Max Lynch
2015-09-02 15:34:53 -05:00
parent dc7999d4c8
commit 400bec2a29
6 changed files with 164 additions and 113 deletions

View File

@@ -194,3 +194,27 @@ export function hasFocusedTextInput() {
}
return false;
}
export function closest(el, selector) {
var matchesFn;
// find vendor prefix
['matches','webkitMatchesSelector','mozMatchesSelector','msMatchesSelector','oMatchesSelector'].some(function(fn) {
if (typeof document.body[fn] == 'function') {
matchesFn = fn;
return true;
}
return false;
})
// traverse parents
while (el!==null) {
parent = el.parentElement;
if (parent!==null && parent[matchesFn](selector)) {
return parent;
}
el = parent;
}
return null;
}