vanillajs-browser-helpers
Version:
Collection of convenience code snippets (helpers) that aims to make it a little easier to work with vanilla JS in the browser
30 lines (29 loc) • 1.08 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const inDOM_1 = __importDefault(require("./inDOM"));
function fallback(selector) {
const matches = document.querySelectorAll(selector);
let i = matches.length;
while (--i >= 0 && matches.item(i) !== this) { } // eslint-disable-line no-empty
return i > -1;
}
// Determine the supported method of 'matches' (with or without prefixes)
const ElmProto = Element.prototype;
const _match = ElmProto.matches || ElmProto.webkitMatchesSelector || fallback;
/**
* Determines whether or not a DOM element matches a given CSS query selector
*
* @param elm - DOM element to test
* @param selector - CSS selector {elm} should match
* @return Whether or not {elm} matched the selector
*/
function matches(elm, selector = '') {
if (!inDOM_1.default(elm)) {
return false;
}
return _match.call(elm, selector);
}
exports.default = matches;