-
-
Notifications
You must be signed in to change notification settings - Fork 202
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
Fix incorrect handling of non-node items inserted into the DOM #1533
Conversation
Alright, so apparently Happy-DOM uses the Anyways, I'll look into this this probably tomorrow; don't merge this yet. |
Thank you for contributing @BenjaminAster! 🙂 Looking forward for the fix! I believe that |
@capricorn86 Ok, I have now modified the tests and the Btw, unrelated to this:
, which I can circumvent by running |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your contribution @BenjaminAster! 🌟
Not sure how this got implemented wrong, but great that it got fixed.
The failing unit test was because of some hiccup. It loads real websites such as github.com and npmjs.com into Happy DOM, which fails randomly from time to time. |
Thank you! And no worries, it's easy to get confused with all the hundreds of DOM methods and what they do exactly, and this error probably stayed undetected for a while because it's very rare to insert text that contains HTML tags. |
Happy-DOM's current handling of inserting items into the DOM that are not nodes via
.prepend()
,.append()
,.before()
,.after()
,.replaceWith()
or.replaceChildren()
is incorrect and might even allow for XSS vulnerabilities:If an item is a string, Happy-DOM parses the string as HTML and inserts the parsed HTML. However, the actual behavior should be to add text nodes with the string as the text content, which is then automatically sanitized.
Additionally, if an item is neither a string nor a node, the browser converts it to a string automatically, but Happy-DOM errors.
Note that the extra
String()
wrapper before passing it todocument.createTextNode
is necessary due to the special behavior ofundefined
in the DOM methods vs. thenew Text()
constructor.This PR (hopefully) fixes this to match the actual behavior of browsers. Let me know if I'm missing something and if I should add tests.