Skip to content

Commit

Permalink
Merge pull request #1384 from odanado/fix/1380
Browse files Browse the repository at this point in the history
Fix a bug in createTextNode when the data argument is a non-string
  • Loading branch information
capricorn86 authored Apr 5, 2024
2 parents 5c745d4 + c0b005b commit 86c9166
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
9 changes: 7 additions & 2 deletions packages/happy-dom/src/nodes/document/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1165,8 +1165,13 @@ export default class Document extends Node {
* @param [data] Text data.
* @returns Text node.
*/
public createTextNode(data?: string): Text {
return NodeFactory.createNode<Text>(this, this[PropertySymbol.ownerWindow].Text, data);
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<Text>(this, this[PropertySymbol.ownerWindow].Text, String(data));
}

/**
Expand Down
19 changes: 17 additions & 2 deletions packages/happy-dom/test/nodes/document/Document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1042,8 +1042,23 @@ 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.', () => {
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]);
}
});
});

Expand Down

0 comments on commit 86c9166

Please sign in to comment.