Skip to content

Instantly share code, notes, and snippets.

@justsml
Forked from eligrey/insertAdjacentHTML.js
Last active February 26, 2021 10:24
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save justsml/0baf7172f8f5cf5d21e65e276ea7ed8b to your computer and use it in GitHub Desktop.
Save justsml/0baf7172f8f5cf5d21e65e276ea7ed8b to your computer and use it in GitHub Desktop.
insertAdjacentHTML polyfill
/*
* 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;
}
@BananaAcid
Copy link

ESLint errors: next is never used, switch is missing a default case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment