patrick-steele-idem / morphdom

Fast and lightweight DOM diffing/patching (no virtual DOM needed)
MIT License
3.21k stars 129 forks source link

Morphing `document.body` into "<body></body>" throws an error. #184

Closed utkarshkukreti closed 4 years ago

utkarshkukreti commented 4 years ago

Code:

<script src="https://unpkg.com/morphdom@2.5.12/dist/morphdom-umd.js"></script>
<body>
<script>
  morphdom(document.body, "<body></body>");
</script>

Error:

Uncaught TypeError: Cannot read property 'nodeType' of undefined
    at morphdom (morphdom-umd.js:676)
    at a.html:4

Adding content to <body> e.g. <body>Hello</body> works fine.

I'll look into this as soon as I can; opening an issue so I don't forget.

snewcomer commented 4 years ago

The main issue here is that a document-fragment is meant to be a lightweight version of a document. So html and body are not allowed as children in a document-fragment. This of it as a div and trying to wrap <body></body>. That would be invalid HTML.

However I think we can fix this. https://github.com/patrick-steele-idem/morphdom/pull/185

Let me know what you think!