Repository: capricorn86/happy-dom (ts)

Issue:
Serializer escapes attributes too much
I tried using happy-dom with Vitest and when switching from jsdom I got a failing test:

![image](https://user-images.githubusercontent.com/2339406/150375626-9222cbc2-cfe2-47d1-b9b4-b0c976630346.png)
  
![image](https://user-images.githubusercontent.com/2339406/150375278-4d61320c-caf7-4464-96af-771a173cc134.png)
 
happy-dom is escaping characters in html attributes that are allowed. These is not need to use `encode` from `he`, `escape` is enough.

Pull request:
Task/335 fix xml serializer
Fixes #335

Patch:
diff --git a/packages/happy-dom/src/xml-serializer/XMLSerializer.ts b/packages/happy-dom/src/xml-serializer/XMLSerializer.ts
index cbb47440..e3630f94 100644
--- a/packages/happy-dom/src/xml-serializer/XMLSerializer.ts
+++ b/packages/happy-dom/src/xml-serializer/XMLSerializer.ts
@@ -3,7 +3,7 @@ import Node from '../nodes/node/Node';
 import SelfClosingElements from '../config/SelfClosingElements';
 import UnclosedElements from '../config/UnclosedElements';
 import DocumentType from '../nodes/document-type/DocumentType';
-import { encode } from 'he';
+import { escape } from 'he';
 import INode from '../nodes/node/INode';
 import IElement from '../nodes/element/IElement';
 
@@ -70,7 +70,7 @@ export default class XMLSerializer {
 		let attributeString = '';
 		for (const attribute of Object.values((<Element>element)._attributes)) {
 			if (attribute.value !== null) {
-				attributeString += ' ' + attribute.name + '="' + encode(attribute.value) + '"';
+				attributeString += ' ' + attribute.name + '="' + escape(attribute.value) + '"';
 			}
 		}
 		return attributeString;


Test patch:
diff --git a/packages/happy-dom/test/xml-serializer/XMLSerializer.test.ts b/packages/happy-dom/test/xml-serializer/XMLSerializer.test.ts
index c4f37cf0..94526f67 100644
--- a/packages/happy-dom/test/xml-serializer/XMLSerializer.test.ts
+++ b/packages/happy-dom/test/xml-serializer/XMLSerializer.test.ts
@@ -100,5 +100,17 @@ describe('XMLSerializer', () => {
 				'<div><custom-element attr1="value1" attr2="value2" attr3=""></custom-element></div>'
 			);
 		});
+
+		it('Does not escape unicode attributes.', () => {
+			const div = document.createElement('div');
+
+			div.setAttribute('attr1', 'Hello \u{2068}John\u{2069}');
+			div.setAttribute('attr2', '<span> test');
+			div.setAttribute('attr3', '');
+
+			expect(xmlSerializer.serializeToString(div)).toBe(
+				'<div attr1="Hello \u{2068}John\u{2069}" attr2="&lt;span&gt; test" attr3=""></div>'
+			);
+		});
 	});
 });