Last active
January 14, 2019 18:24
-
-
Save CITguy/62effdf02888b380bdd5abc8dffef5f3 to your computer and use it in GitHub Desktop.
Web API Polyfills
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
}, | |
}); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import './ChildNode.js' | |
import './Element.js' | |
import './Node.js' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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