Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOMParser does not recognise BODY and thus creates it twice #1615

Open
OlaviSau opened this issue Nov 18, 2024 · 1 comment · May be fixed by #1617
Open

DOMParser does not recognise BODY and thus creates it twice #1615

OlaviSau opened this issue Nov 18, 2024 · 1 comment · May be fixed by #1617
Labels
bug Something isn't working

Comments

@OlaviSau
Copy link
Contributor

OlaviSau commented Nov 18, 2024

Describe the bug
DOMParser is not compliant with the browser implementation. This breaks frameworks like Angular from setting the innerHTML as an attribute, breaking most renders using Angular.
This occurs because BODY is not recognised and this causes body to be created as an extra element in HappyDOM.

To Reproduce
(new window.DOMParser()).parseFromString("<body><x></x>Example Text", "text/html").body.innerHTML
HappyDOM: <body><x></x>Example Text</body>
Expected behavior
Chrome: <x></x>Example Text

Additional context
tagName BODY should not be created as a standard node.

const bodyElement = newDocument.createElement('body');

bodyElement.appendChild(root[PropertySymbol.nodeArray][0]);

@capricorn86

@OlaviSau OlaviSau added the bug Something isn't working label Nov 18, 2024
@OlaviSau OlaviSau changed the title DOMParser does not recognise body as a valid root node DOMParser does not recognise body Nov 18, 2024
@OlaviSau OlaviSau changed the title DOMParser does not recognise body DOMParser does not recognise BODY thus creating it twice Nov 18, 2024
OlaviSau added a commit to OlaviSau/happy-dom that referenced this issue Nov 18, 2024
OlaviSau added a commit to OlaviSau/happy-dom that referenced this issue Nov 18, 2024
@OlaviSau
Copy link
Contributor Author

OlaviSau commented Nov 18, 2024

@capricorn86 I have created a naive fix for this, I did not handle all the cases - like what happens if there are multiple body elements as siblings / children of body and such. These cases will still fail and would need some rework on how HappyDOM appends / parses the html.

The sibling case could be handled by adding the following to the appendChild part of DOMParser, but that wouldn't solve the child case so perhaps it's better to do it in the XMLParser, but at that point it's not an XMLParser, but instead an HTMLParser as XML does not specify that it should not have multiple BODY tags.
It should not be handled in appendChild as browsers allow appending multiple BODY tags into the html. Thus it is a bit complicated to solve in a way that doesn't compromise on performance and still remains correct.

If performance is not a concern then a naive solution could walk through the output of XMLParser root document tree and remove every child with the tagName body.
If performance is a concern then either a HTMLParser could be created that ignores secondary body tags or a specification option could be added to the XMLParser. Personally I think creating an HTMLParser would make more sense since browsers implement many optimisations and odd rules for parsing HTML.

if (root[PropertySymbol.nodeArray][0]['tagName'] === "BODY") {
	root.removeChild(root[PropertySymbol.nodeArray][0]);
	continue;
}

#1616

@OlaviSau OlaviSau changed the title DOMParser does not recognise BODY thus creating it twice DOMParser does not recognise BODY thus creates it twice Nov 18, 2024
@OlaviSau OlaviSau changed the title DOMParser does not recognise BODY thus creates it twice DOMParser does not recognise BODY and thus creates it twice Nov 18, 2024
capricorn86 added a commit that referenced this issue Nov 19, 2024
capricorn86 added a commit that referenced this issue Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
1 participant