Skip to content

Commit

Permalink
fix: [#1566] Fixes incorrect serialization of special characters in r…
Browse files Browse the repository at this point in the history
…aw text elements such as <script> and <style> (#1566)

Co-authored-by: David Ortner <david@ortner.se>
  • Loading branch information
BenjaminAster and capricorn86 authored Nov 6, 2024
1 parent 32f7510 commit c80a0e9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
7 changes: 7 additions & 0 deletions packages/happy-dom/src/xml-serializer/XMLSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ export default class XMLSerializer {
// TODO: Add support for processing instructions.
return `<!--?${(<ProcessingInstruction>root).target} ${root.textContent}?-->`;
case NodeTypeEnum.textNode:
const parentElement = root.parentElement;
if (parentElement) {
const parentConfig = HTMLElementConfig[parentElement[PropertySymbol.localName]];
if (parentConfig?.contentModel === HTMLElementConfigContentModelEnum.rawText) {
return root.textContent;
}
}
return Entities.escapeText(root.textContent);
case NodeTypeEnum.documentTypeNode:
const doctype = <DocumentType>root;
Expand Down
2 changes: 1 addition & 1 deletion packages/happy-dom/test/dom-parser/DOMParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('DOMParser', () => {
`<html>
<body>
<script>
var test = {className:"meta",begin:/&lt;![a-z]/,end:/&gt;/,contains:[t,i,l,c]};
var test = {className:"meta",begin:/<![a-z]/,end:/>/,contains:[t,i,l,c]};
</script>
</body>
</html>`.replace(/\s/g, '')
Expand Down
15 changes: 15 additions & 0 deletions packages/happy-dom/test/nodes/element/Element.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,21 @@ describe('Element', () => {

expect(element.innerHTML).toBe('EXPECTED_HTML');
});

it('Returns HTML of a mixture of normal and raw text elements as a concatenated string.', () => {
const container = document.createElement('div');
const testString = `/* &<>\xA0 */`;
const div = document.createElement('div');
div.textContent = testString;
const script = document.createElement('script');
script.textContent = testString;
const style = document.createElement('style');
style.textContent = testString;
container.append(div, script, style);
expect(container.innerHTML).toBe(
'<div>/* &amp;&lt;&gt;&nbsp; */</div><script>/* &<>\xA0 */</script><style>/* &<>\xA0 */</style>'
);
});
});

describe('set innerHTML()', () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/happy-dom/test/xml-parser/XMLParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,9 @@ describe('XMLParser', () => {

expect(new XMLSerializer().serializeToString(root)).toBe(
`<div>
<script>if(1&lt;Math['random']()){}else if(Math['random']()&gt;1){console.log("1")}</script>
<script>&lt;b&gt;&lt;/b&gt;</script>
<style>&lt;b&gt;&lt;/b&gt;</style>
<script>if(1<Math['random']()){}else if(Math['random']()>1){console.log("1")}</script>
<script><b></b></script>
<style><b></b></style>
</div>`
);

Expand Down

0 comments on commit c80a0e9

Please sign in to comment.