Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug in createTextNode when the data argument is a non-string #1384

Merged
merged 3 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In TypeScript lib.dom.d.ts, the createTextNode argument is not optional.

 2024-04-06 1 57 31

I have changed the type and implementation to mimic browser behavior.

Safari Firefox Chrome
 2024-04-06 1 50 46  2024-04-06 1 51 00  2024-04-06 1 50 53

Could you help me determine whether this change is considered a 'bug fix' or a 'breaking change'?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would consider this as a bug fix as it is very unlikely to affect a consumer, but of course it would be great if this fix could be added as part of a major release. I think it is fine for this case.

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