50 lines
1007 B
JavaScript
50 lines
1007 B
JavaScript
'use strict';
|
|
|
|
/**
|
|
* Determine if a DOM element matches a CSS selector
|
|
*
|
|
* @param {Element} elem
|
|
* @param {String} selector
|
|
* @return {Boolean}
|
|
* @api public
|
|
*/
|
|
|
|
function matches(elem, selector) {
|
|
// Vendor-specific implementations of `Element.prototype.matches()`.
|
|
var proto = window.Element.prototype;
|
|
var nativeMatches = proto.matches ||
|
|
proto.mozMatchesSelector ||
|
|
proto.msMatchesSelector ||
|
|
proto.oMatchesSelector ||
|
|
proto.webkitMatchesSelector;
|
|
|
|
if (!elem || elem.nodeType !== 1) {
|
|
return false;
|
|
}
|
|
|
|
var parentElem = elem.parentNode;
|
|
|
|
// use native 'matches'
|
|
if (nativeMatches) {
|
|
return nativeMatches.call(elem, selector);
|
|
}
|
|
|
|
// native support for `matches` is missing and a fallback is required
|
|
var nodes = parentElem.querySelectorAll(selector);
|
|
var len = nodes.length;
|
|
|
|
for (var i = 0; i < len; i++) {
|
|
if (nodes[i] === elem) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Expose `matches`
|
|
*/
|
|
|
|
module.exports = matches;
|