Skip to content

Instantly share code, notes, and snippets.

@andreasvirkus
Created November 20, 2017 15:13
Show Gist options
  • Save andreasvirkus/fabbe12ab95002c9958e1d32afcbbebd to your computer and use it in GitHub Desktop.
Save andreasvirkus/fabbe12ab95002c9958e1d32afcbbebd to your computer and use it in GitHub Desktop.
// 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