carbon-components
Version:
Carbon Components is a component library for IBM Cloud
43 lines (42 loc) • 1.51 kB
JavaScript
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(['exports'], factory);
} else if (typeof exports !== "undefined") {
factory(exports);
} else {
var mod = {
exports: {}
};
factory(mod.exports);
global.eventMatches = mod.exports;
}
})(this, function (exports) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = eventMatches;
/**
* @param {Event} event The event.
* @param {string} selector The selector.
* @returns {Element}
* The closest ancestor of the event target (or the event target itself) which matches the selectors given in parameter.
*/
function eventMatches(event, selector) {
// <svg> in IE does not have `Element#msMatchesSelector()` (that should be copied to `Element#matches()` by a polyfill).
// Also a weird behavior is seen in IE where DOM tree seems broken when `event.target` is on <svg>.
// Therefore this function simply returns `undefined` when `event.target` is on <svg>.
if (typeof event.target.matches === 'function') {
if (event.target.matches(selector)) {
// If event target itself matches the given selector, return it
return event.target;
} else if (event.target.matches(selector + ' *')) {
var closest = event.target.closest(selector);
if (event.currentTarget.contains(closest)) {
return closest;
}
}
}
return undefined;
}
});