@jjwesterkamp/event-delegation
Version:
Event delegation for browser DOM events. Flexible, cross-browser compatible and Typescript-focused.
34 lines (33 loc) • 1.2 kB
JavaScript
import { isFunction } from './assertions';
/**
* Tells whether given element matches a given CSS selector.
*
* @param element
* @param selector
*/
export function matches(element, selector) {
if (!(element instanceof Element)) {
throw new Error('cannot match a non-element against a selector');
}
if (isFunction(element.matches))
return element.matches(selector);
if (isFunction(element.matchesSelector))
return element.matchesSelector(selector);
if (isFunction(element.mozMatchesSelector))
return element.mozMatchesSelector(selector);
if (isFunction(element.msMatchesSelector))
return element.msMatchesSelector(selector);
if (isFunction(element.oMatchesSelector))
return element.oMatchesSelector(selector);
/* istanbul ignore else */
if (isFunction(element.webkitMatchesSelector))
return element.webkitMatchesSelector(selector);
/* istanbul ignore next */
{
var matches_1 = (element.document || element.ownerDocument).querySelectorAll(selector);
var i = matches_1.length;
while (--i >= 0 && matches_1.item(i) !== element) {
}
return i > -1;
}
}