From 6f6a7ab78c4723246d03087ed942a33561e0219d Mon Sep 17 00:00:00 2001 From: odan Date: Sat, 6 Apr 2024 01:28:08 +0900 Subject: [PATCH 1/2] fix: [#1380] Fix a bug in createTextNode when the data argument is a non-string --- packages/happy-dom/src/nodes/document/Document.ts | 2 +- .../happy-dom/test/nodes/document/Document.test.ts | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/happy-dom/src/nodes/document/Document.ts b/packages/happy-dom/src/nodes/document/Document.ts index 75922cf9..b21f31cc 100644 --- a/packages/happy-dom/src/nodes/document/Document.ts +++ b/packages/happy-dom/src/nodes/document/Document.ts @@ -1166,7 +1166,7 @@ export default class Document extends Node { * @returns Text node. */ public createTextNode(data?: string): Text { - return NodeFactory.createNode(this, this[PropertySymbol.ownerWindow].Text, data); + return NodeFactory.createNode(this, this[PropertySymbol.ownerWindow].Text, String(data)); } /** diff --git a/packages/happy-dom/test/nodes/document/Document.test.ts b/packages/happy-dom/test/nodes/document/Document.test.ts index cec5f5c1..27bdf91a 100644 --- a/packages/happy-dom/test/nodes/document/Document.test.ts +++ b/packages/happy-dom/test/nodes/document/Document.test.ts @@ -1045,6 +1045,17 @@ describe('Document', () => { const textNode = document.createTextNode(); expect(textNode.data).toBe(''); }); + + it('Creates a text node with non string content.', () => { + const inputs = [1, -1, true, false, null, undefined, {}, []]; + const outputs = ['1', '-1', 'true', 'false', 'null', 'undefined', '[object Object]', '']; + + for (let i = 0; i < inputs.length; i++) { + // @ts-ignore + const textNode = document.createTextNode(inputs[i]); + expect(textNode.data).toBe(outputs[i]); + } + }); }); describe('createComment()', () => { From 06dde91f4adba14ca98b9845a538b95de4ff5d92 Mon Sep 17 00:00:00 2001 From: odan Date: Sat, 6 Apr 2024 01:56:43 +0900 Subject: [PATCH 2/2] fix: [#1380] Fix behavior when no argument is specified --- packages/happy-dom/src/nodes/document/Document.ts | 7 ++++++- packages/happy-dom/test/nodes/document/Document.test.ts | 8 ++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/happy-dom/src/nodes/document/Document.ts b/packages/happy-dom/src/nodes/document/Document.ts index b21f31cc..3e11866c 100644 --- a/packages/happy-dom/src/nodes/document/Document.ts +++ b/packages/happy-dom/src/nodes/document/Document.ts @@ -1165,7 +1165,12 @@ export default class Document extends Node { * @param [data] Text data. * @returns Text node. */ - public createTextNode(data?: string): Text { + public createTextNode(data: string): Text { + if (arguments.length < 1) { + throw new TypeError( + `Failed to execute 'createTextNode' on 'Document': 1 argument required, but only ${arguments.length} present.` + ); + } return NodeFactory.createNode(this, this[PropertySymbol.ownerWindow].Text, String(data)); } diff --git a/packages/happy-dom/test/nodes/document/Document.test.ts b/packages/happy-dom/test/nodes/document/Document.test.ts index 27bdf91a..6b9332b4 100644 --- a/packages/happy-dom/test/nodes/document/Document.test.ts +++ b/packages/happy-dom/test/nodes/document/Document.test.ts @@ -1042,8 +1042,12 @@ describe('Document', () => { }); it('Creates a text node without content.', () => { - const textNode = document.createTextNode(); - expect(textNode.data).toBe(''); + // @ts-ignore + expect(() => document.createTextNode()).toThrow( + new TypeError( + `Failed to execute 'createTextNode' on 'Document': 1 argument required, but only 0 present.` + ) + ); }); it('Creates a text node with non string content.', () => {