Skip to content

Commit

Permalink
fix: [#1380] Fix a bug in createTextNode when the data argument is a …
Browse files Browse the repository at this point in the history
…non-string
  • Loading branch information
odanado committed Apr 5, 2024
1 parent f456def commit 6f6a7ab
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/happy-dom/src/nodes/document/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,7 @@ export default class Document extends Node {
* @returns Text node.
*/
public createTextNode(data?: string): Text {
return NodeFactory.createNode<Text>(this, this[PropertySymbol.ownerWindow].Text, data);
return NodeFactory.createNode<Text>(this, this[PropertySymbol.ownerWindow].Text, String(data));
}

/**
Expand Down
11 changes: 11 additions & 0 deletions packages/happy-dom/test/nodes/document/Document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()', () => {
Expand Down

0 comments on commit 6f6a7ab

Please sign in to comment.