@squirrel-forge/ui-util
Version:
A collection of utilities, classes, functions and abstracts made for the browser and babel compatible.
21 lines (20 loc) • 639 B
JavaScript
/**
* Convert string to html
* @param {string} str - HTML source string
* @param {boolean} multiple - Contains multiple nodes, default: true
* @return {null|HTMLElement|NodeList} - Element or collection of elements
*/
export function str2node( str, multiple = true ) {
str = str.trim();
if ( str.length ) {
const template = document.createElement( 'template' );
if ( 'content' in template ) {
template.innerHTML = str;
if ( multiple ) {
return template.content.childNodes;
}
return template.content.firstChild;
}
}
return null;
}