Skip to content

Instantly share code, notes, and snippets.

@andreasvirkus
Created February 16, 2021 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreasvirkus/fbea20af025463ee1fd9d684ee5900ce to your computer and use it in GitHub Desktop.
Save andreasvirkus/fbea20af025463ee1fd9d684ee5900ce to your computer and use it in GitHub Desktop.
export function stringToHtml(html: string): ChildNode {
var template = document.createElement('template')
html = html.trim() // Never return a text node of whitespace as the result
template.innerHTML = html
return template.content.firstChild
}
// usage:
const div = stringToHtml('<div><span>nested</span> <span>stuff</span></div>')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment