Repository: capricorn86/happy-dom
Pull request #98 (2020-10-06 23:53:44)

#97@patch: Nodes that are outside of the html tag should be added to the body tag


Patch:
diff --git a/packages/happy-dom/src/dom-parser/DOMParser.ts b/packages/happy-dom/src/dom-parser/DOMParser.ts
index f1f71eae..9b84e740 100644
--- a/packages/happy-dom/src/dom-parser/DOMParser.ts
+++ b/packages/happy-dom/src/dom-parser/DOMParser.ts
@@ -33,6 +33,12 @@ export default class DOMParser {
 
 		if (documentElement) {
 			newDocument.appendChild(documentElement);
+			const body = newDocument.querySelector('body');
+			if (body) {
+				for (const child of root.childNodes.slice()) {
+					body.appendChild(child);
+				}
+			}
 		} else {
 			const documentElement = newDocument.createElement('html');
 			const bodyElement = newDocument.createElement('body');
diff --git a/packages/happy-dom/src/nodes/basic/document/Document.ts b/packages/happy-dom/src/nodes/basic/document/Document.ts
index 38c49b11..21f569e1 100644
--- a/packages/happy-dom/src/nodes/basic/document/Document.ts
+++ b/packages/happy-dom/src/nodes/basic/document/Document.ts
@@ -103,6 +103,12 @@ export default class Document extends DocumentFragment {
 
 			if (documentElement) {
 				this.appendChild(documentElement);
+				const body = this.querySelector('body');
+				if (body) {
+					for (const child of root.childNodes.slice()) {
+						body.appendChild(child);
+					}
+				}
 			} else {
 				const documentElement = this.createElement('html');
 				const bodyElement = this.createElement('body');


Test patch:
diff --git a/packages/happy-dom/test/dom-parser/DOMParser.test.ts b/packages/happy-dom/test/dom-parser/DOMParser.test.ts
index 277454d4..a1a66674 100644
--- a/packages/happy-dom/test/dom-parser/DOMParser.test.ts
+++ b/packages/happy-dom/test/dom-parser/DOMParser.test.ts
@@ -14,14 +14,42 @@ describe('DOMParser', () => {
 			const newDocument = domParser.parseFromString(DOMParserHTML, 'text/html');
 			expect(newDocument.documentElement.outerHTML).toBe(DOMParserHTML);
 		});
-	});
 
-	describe('parseFromString()', () => {
 		test('Parses HTML and returns a new document.', () => {
 			const newDocument = domParser.parseFromString('Test', 'text/html');
 			expect(newDocument.documentElement.outerHTML).toBe(
 				'<html><head></head><body>Test</body></html>'
 			);
 		});
+
+		test('Adds elements outside of the <html> tag to the <body> tag.', () => {
+			const newDocument = domParser.parseFromString(
+				`
+				<html>
+					<head>
+						<title>Title</title>
+					</head>
+					<body>
+						<span>Body</span>
+					</body>
+				</html>
+				<div>Should be added to body</div>
+			`,
+				'text/html'
+			);
+			expect(newDocument.documentElement.outerHTML.replace(/[\s]/gm, '')).toBe(
+				`
+				<html>
+					<head>
+						<title>Title</title>
+					</head>
+					<body>
+						<span>Body</span>
+						<div>Should be added to body</div>
+					</body>
+				</html>
+				`.replace(/[\s]/gm, '')
+			);
+		});
 	});
 });
diff --git a/packages/happy-dom/test/nodes/basic/document/Document.test.ts b/packages/happy-dom/test/nodes/basic/document/Document.test.ts
index 80c46444..f0d36b28 100644
--- a/packages/happy-dom/test/nodes/basic/document/Document.test.ts
+++ b/packages/happy-dom/test/nodes/basic/document/Document.test.ts
@@ -51,6 +51,34 @@ describe('Document', () => {
 				`.replace(/[\s]/gm, '')
 			);
 		});
+
+		test('Adds elements outside of the <html> tag to the <body> tag.', () => {
+			const html = `
+				<html>
+					<head>
+						<title>Title</title>
+					</head>
+					<body>
+						<span>Body</span>
+					</body>
+				</html>
+				<div>Should be added to body</div>
+			`;
+			document.write(html);
+			expect(document.documentElement.outerHTML.replace(/[\s]/gm, '')).toBe(
+				`
+				<html>
+					<head>
+						<title>Title</title>
+					</head>
+					<body>
+						<span>Body</span>
+						<div>Should be added to body</div>
+					</body>
+				</html>
+				`.replace(/[\s]/gm, '')
+			);
+		});
 	});
 
 	describe('open()', () => {