debian-mirror-gitlab/app/assets/javascripts/commons/polyfills/element.js

75 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-04-08 14:13:33 +05:30
/**
* Polyfill
* @what Element.classList
* @why In order to align browser features
* @browsers Internet Explorer 11
* @see https://caniuse.com/#feat=classlist
*/
2018-12-05 23:21:45 +05:30
import 'classlist-polyfill';
2020-04-08 14:13:33 +05:30
/**
* Polyfill
* @what Element.closest
* @why In order to align browser features
* @browsers Internet Explorer 11
* @see https://caniuse.com/#feat=element-closest
*/
2018-12-05 23:21:45 +05:30
Element.prototype.closest =
Element.prototype.closest ||
2017-08-17 22:00:37 +05:30
function closest(selector, selectedElement = this) {
if (!selectedElement) return null;
2018-12-05 23:21:45 +05:30
return selectedElement.matches(selector)
? selectedElement
: Element.prototype.closest(selector, selectedElement.parentElement);
2017-08-17 22:00:37 +05:30
};
2020-04-08 14:13:33 +05:30
/**
* Polyfill
* @what Element.matches
* @why In order to align browser features
* @browsers Internet Explorer 11
* @see https://caniuse.com/#feat=mdn-api_element_matches
*/
2018-12-05 23:21:45 +05:30
Element.prototype.matches =
Element.prototype.matches ||
2017-08-17 22:00:37 +05:30
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;
2018-12-05 23:21:45 +05:30
while (i >= 0 && elms.item(i) !== this) {
i -= 1;
}
2017-08-17 22:00:37 +05:30
return i > -1;
};
2018-03-17 18:26:18 +05:30
2020-04-08 14:13:33 +05:30
/**
* Polyfill
* @what ChildNode.remove, Element.remove, CharacterData.remove, DocumentType.remove
* @why In order to align browser features
* @browsers Internet Explorer 11
* @see https://caniuse.com/#feat=childnode-remove
*
* From the polyfill on MDN, https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/remove#Polyfill
*/
2018-12-05 23:21:45 +05:30
(arr => {
arr.forEach(item => {
2018-03-17 18:26:18 +05:30
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]);