Skip to content

Instantly share code, notes, and snippets.

@CITguy
Last active January 14, 2019 18:24
Show Gist options
  • Save CITguy/62effdf02888b380bdd5abc8dffef5f3 to your computer and use it in GitHub Desktop.
Save CITguy/62effdf02888b380bdd5abc8dffef5f3 to your computer and use it in GitHub Desktop.
Web API Polyfills
// Modified from https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/remove#Polyfill
[
Element.prototype,
CharacterData.prototype,
DocumentType.prototype,
].forEach(function (proto) {
if (!proto.hasOwnProperty('remove')) {
Object.defineProperty(proto, 'remove', {
configurable: true,
enumerable: true,
writable: true,
value: function () {
if (this.parentNode !== null) {
this.parentNode.removeChild(this);
}
},
});
}
});
// Modified from https://developer.mozilla.org/en-US/docs/Web/API/Element/closest#Polyfill
/*
* If browser supports a variation of matches(), (IE9+)
* normalize to 'matches' on the prototype.
*/
if (!Element.prototype.matches) {
Element.prototype.matches = Element.prototype.msMatchesSelector ||
Element.prototype.webkitMatchesSelector;
}
if (!Element.prototype.closest) {
Element.prototype.closest = function (selectors) {
var el = this;
// fail fast if element isn't attached to the document
if (!document.documentElement.contains(el)) {
return null;
}
// Check if any ancestors match selectors
while (el !== null && el.nodeType === 1) {
if (el.matches(selectors)) {
return el;
} else {
el = el.parentElement || el.parentNode;
}
}
// Return null if no ancestors match
return null;
};
}
import './ChildNode.js'
import './Element.js'
import './Node.js'
var proto = Node.prototype;
if (!proto.hasOwnProperty('isConnected')) {
Object.defineProperty(proto, 'isConnected', {
enumerable: true,
get: function () {
return (this.getRootNode().nodeType === Node.DOCUMENT_NODE);
},
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment