summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/extensions/element.js.es6
blob: 3f12ad9ff9f1c933995e17514b39ae5191b1afa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* global Element */
/* eslint-disable consistent-return, max-len, no-empty, no-plusplus, func-names */

Element.prototype.closest = Element.prototype.closest || function closest(selector, selectedElement = this) {
  if (!selectedElement) return;
  return selectedElement.matches(selector) ? selectedElement : Element.prototype.closest(selector, selectedElement.parentElement);
};

Element.prototype.matches = Element.prototype.matches ||
  Element.prototype.matchesSelector ||
  Element.prototype.mozMatchesSelector ||
  Element.prototype.msMatchesSelector ||
  Element.prototype.oMatchesSelector ||
  Element.prototype.webkitMatchesSelector ||
  function (s) {
    const matches = (this.document || this.ownerDocument).querySelectorAll(s);
    let i = matches.length;
    while (--i >= 0 && matches.item(i) !== this) {}
    return i > -1;
  };