b-fuze / deno-dom

Browser DOM & HTML parser in Deno
https://jsr.io/@b-fuze/deno-dom
MIT License
423 stars 47 forks source link

DOM tree doesn't update on reassigning a queried element's outerHTML #150

Closed chaosharmonic closed 1 year ago

chaosharmonic commented 1 year ago

To elaborate on the title -- if I try to take a set of queried elements, and loop over them, as below:

// given an existing string, html
const doc = new DOMParser().parseFromString(html, 'text/html')
const nonSemanticAsides = [...document.querySelector('div.sidebar')]

nonSemanticAsides.forEach(e => {
  e.outerHTML = `<aside>${e.innerHTML}</aside>`
})

Nothing happens. Whereas if I do this in a browser, this is mutable; the page will update, and I can get a modified HTML string out of document.body.innerHTML. (Incidentally, I can get modified results if I reassign e.innerHTML, but not if I reassign e.parentNode.innerHTML, which I tried as a workaround to the above.)

For reference, this behavior isn't in any way related to spreading this from a NodeList into an Array; a plain for loop also doesn't work, and elsewhere in the script I'm using this for (basically, scraping publicly available technical writing and writing the contents to Markdown) the same pattern as above does work for removing nodes using e.remove().

b-fuze commented 1 year ago

Setting .outerHTML was implemented in Deno DOM v0.1.43