Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tomhodgins/067966548544960d47ce9f62e7fc2988 to your computer and use it in GitHub Desktop.
Save tomhodgins/067966548544960d47ce9f62e7fc2988 to your computer and use it in GitHub Desktop.
// Deno Support
if ('Deno' in globalThis) {
globalThis.DOMParser = (await import('https://deno.land/x/deno_dom/deno-dom-wasm.ts')).DOMParser
}
// Node Support
if ('process' in globalThis) {
const jsdom = (await import('jsdom')).default.JSDOM
globalThis.DOMParser = class {
parseFromString(string = '', mode = '') {
return (new jsdom(string)).window.document
}
}
}
console.log(
new DOMParser()
.parseFromString(`one<p><b>two<p>three</b>`, 'text/html')
.body
.innerHTML
)
// Should log: one<p><b>two</b></p><p><b>three</b></p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment