-
-
Save justsml/0baf7172f8f5cf5d21e65e276ea7ed8b to your computer and use it in GitHub Desktop.
insertAdjacentHTML polyfill
This file contains 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
/* | |
* Updated w/ insertAdjacentElement | |
* @author Dan Levy @justsml | |
* 2016-06-23 | |
* | |
* Credit: @lyleunderwood - afterend patch/fix | |
* | |
* ```js | |
* import { insertAdjacentElement } from './libs/insertAdjacentHTML.js'; | |
* Use either: | |
* HTMLElement.prototype.insertAdjacentElement = HTMLElement.prototype.insertAdjacentElement || insertAdjacentElement; | |
* // Or, polyfill based on the current correct DOM3 API: | |
* Node.prototype.insertAdjacentElement = Node.prototype.insertAdjacentElement || insertAdjacentElement; | |
* ``` | |
* | |
* ======== | |
* insertAdjacentHTML.js | |
* Cross-browser full HTMLElement.insertAdjacentHTML implementation. | |
* | |
* 2011-10-10 | |
* | |
* By Eli Grey, http://eligrey.com | |
* Public Domain. | |
* NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. | |
*/ | |
export function insertAdjacentElement(position, elem) { | |
let _this = this, | |
parent = _this.parentNode, | |
node, first, next; | |
switch (position.toLowerCase()) { | |
case 'beforebegin': | |
while ((node = elem.firstChild)) { | |
parent.insertBefore(node, _this); | |
} | |
break; | |
case 'afterbegin': | |
first = _this.firstChild; | |
while ((node = elem.lastChild)) { | |
first = _this.insertBefore(node, first); | |
} | |
break; | |
case 'beforeend': | |
while ((node = elem.firstChild)) { | |
_this.appendChild(node); | |
} | |
break; | |
case 'afterend': | |
parent.insertBefore(elem, _this.nextSibling); | |
break; | |
} | |
return elem; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ESLint errors:
next
is never used, switch is missing a default case