summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/commons/polyfills/element.js
blob: b593bde6aa2b8cd641bf3d762135bc6d5be30d6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Element.prototype.closest = Element.prototype.closest ||
  function closest(selector, selectedElement = this) {
    if (!selectedElement) return null;
    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 matches(selector) {
    const elms = (this.document || this.ownerDocument).querySelectorAll(selector);
    let i = elms.length - 1;
    while (i >= 0 && elms.item(i) !== this) { i -= 1; }
    return i > -1;
  };

// From the polyfill on MDN, https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/remove#Polyfill
((arr) => {
  arr.forEach((item) => {
    if (Object.prototype.hasOwnProperty.call(item, 'remove')) {
      return;
    }
    Object.defineProperty(item, 'remove', {
      configurable: true,
      enumerable: true,
      writable: true,
      value: function remove() {
        if (this.parentNode !== null) {
          this.parentNode.removeChild(this);
        }
      },
    });
  });
})([Element.prototype, CharacterData.prototype, DocumentType.prototype]);