Open aralroca opened 6 months ago
After doing this:
const doc = document.implementation.createHTMLDocument(); doc.open(); doc.write('<html>') doc.write('<head>') doc.write('<title>Title</title>') doc.write('</head>') doc.write('<body>') doc.write('<span>Body</span>') doc.write('</body>') doc.write('</html>') doc.close(); console.log(doc.documentElement.outerHTML)
I expect:
<html> <head> <title>Title</title> </head> <body> <span>Body</span> </body> </html>
(Same behavior in Chrome, Safari and Firefox)
But I'm getting this error:
DOMException: Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node.
I see that is related with this code:
https://github.com/aralroca/happy-dom/blob/d7c7d45b9190663ca545aa6184a28eb812ab46ca/packages/happy-dom/src/nodes/document/Document.ts#L897-L904
Here is creating the body twice, and then in the head is using the body that is not appended. However, changing this:
body
head
-documentElement.appendChild(this.createElement('body')); +documentElement.appendChild(body);
Resolves that it does not crash, but the behavior is different and I don't know how to solve it (please, some help). Now the HTML generated is this one:
<html> <head> </head> <body> <head> </head> <title>Title</title> <span>Body</span> </body> </html>
I think this is because of the doc.open() not behaving properly. So we can reproduce the same HTML in browsers removing the doc.open():
doc.open()
const doc = document.implementation.createHTMLDocument(); -doc.open(); doc.write('<html>') doc.write('<head>') doc.write('<title>Title</title>') doc.write('</head>') doc.write('<body>') doc.write('<span>Body</span>') doc.write('</body>') doc.write('</html>') -doc.close(); console.log(doc.documentElement.outerHTML)
But this is not what we want. This is necessary to process HTML in streaming in a separate document and to be able to parse the nodes as they come in.
[!NOTE] Same error happens with document.write
[!NOTE]
Same error happens with document.write
document.write
Any idea how to solve it? Thanks
After doing this:
I expect:
(Same behavior in Chrome, Safari and Firefox)
But I'm getting this error:
I see that is related with this code:
https://github.com/aralroca/happy-dom/blob/d7c7d45b9190663ca545aa6184a28eb812ab46ca/packages/happy-dom/src/nodes/document/Document.ts#L897-L904
Here is creating the
body
twice, and then in thehead
is using the body that is not appended. However, changing this:Resolves that it does not crash, but the behavior is different and I don't know how to solve it (please, some help). Now the HTML generated is this one:
I think this is because of the
doc.open()
not behaving properly. So we can reproduce the same HTML in browsers removing thedoc.open()
:But this is not what we want. This is necessary to process HTML in streaming in a separate document and to be able to parse the nodes as they come in.
Any idea how to solve it? Thanks