Created
November 20, 2017 15:13
-
-
Save andreasvirkus/fabbe12ab95002c9958e1d32afcbbebd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Determine if an element matches a selector | |
export const matches = (el, selector) => { | |
if (!isElement(el)) { | |
return false | |
} | |
// https://developer.mozilla.org/en-US/docs/Web/API/Element/matches#Polyfill | |
// Prefer native implementations over polyfill function | |
const proto = Element.prototype | |
const Matches = proto.matches || | |
proto.matchesSelector || | |
proto.mozMatchesSelector || | |
proto.msMatchesSelector || | |
proto.oMatchesSelector || | |
proto.webkitMatchesSelector || | |
function (sel) { | |
const element = this | |
const m = selectAll(sel, element.document || element.ownerDocument) | |
let i = m.length | |
// eslint-disable-next-line no-empty | |
while (--i >= 0 && m.item(i) !== element) {} | |
return i > -1 | |
} | |
return Matches.call(el, selector) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment