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
31 lines (30 loc) • 1.15 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var inDOM_1 = __importDefault(require("./inDOM"));
function fallback(selector) {
var matches = document.querySelectorAll(selector);
var 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)
var ElmProto = Element.prototype;
var _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 (selector === void 0) { selector = ''; }
if (!(0, inDOM_1.default)(elm)) {
return false;
}
return _match.call(elm, selector);
}
exports.default = matches;