@squirrel-forge/ui-util
Version:
A collection of utilities, classes, functions and abstracts made for the browser and babel compatible.
24 lines (20 loc) • 626 B
JavaScript
/**
* Unwrap
* Removes the given element and preserves any children
* @param {HTMLElement} element - Element wrapper to remove
* @throws Error
* @return {void}
*/
export function unwrap( element ) {
// Check for a valid element
if ( !( element instanceof HTMLElement || !element.isConnected ) ) {
throw new Error( 'unwrap() Argument element must be a valid HTMLElement' );
}
// Unwrap children if available
if ( element.hasChildNodes() ) {
element.replaceWith( ...element.childNodes );
} else {
// Remove element if it has no children
element.remove();
}
}