Skip to content

Instantly share code, notes, and snippets.

@shanewholloway
Created October 13, 2021 17:12
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 shanewholloway/0b5df53acb93b0787f87b2344d7ad495 to your computer and use it in GitHub Desktop.
Save shanewholloway/0b5df53acb93b0787f87b2344d7ad495 to your computer and use it in GitHub Desktop.
DOM parsing and serializing
export function parse_html(raw_html_string) {
return new DOMParser()
.parseFromString(raw_html_string, 'text/html') }
export function dom_to_html_blob(dom) {
let html_doc = new XMLSerializer()
.serializeToString(dom)
return new Blob([html_doc], {type: 'text/html'}) }
export function dom_to_html_blob_url(dom) {
return URL.createObjectURL(dom_to_html_blob(dom)) }
export function dom_to_iframe(iframe, dom_document) {
let dest_doc = iframe.contentDocument
dest_doc.documentElement.replaceWith(
dest_doc.adoptNode(
dom_document.documentElement)) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment