Skip to content

Commit

Permalink
Fixes an issue where '0' as textContent does not work. Closes github#223
Browse files Browse the repository at this point in the history
  • Loading branch information
Havunen committed Oct 10, 2023
1 parent 152ed3d commit 760594b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.nyc_output
coverage/
node_modules/
.idea
2 changes: 1 addition & 1 deletion cjs/interface/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class Element extends ParentNode {

set textContent(text) {
this.replaceChildren();
if (text)
if (text != null)
this.appendChild(new Text(this.ownerDocument, text));
}

Expand Down
2 changes: 1 addition & 1 deletion esm/interface/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export class Element extends ParentNode {

set textContent(text) {
this.replaceChildren();
if (text)
if (text != null)
this.appendChild(new Text(this.ownerDocument, text));
}

Expand Down
16 changes: 16 additions & 0 deletions test/interface/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ node.firstChild.textContent = '<test>';
assert(node.toString(), '<div><p>&lt;test&gt;</p></div>', 'before after not affected');
node.firstChild.textContent = '';
assert(node.toString(), '<div><p></p></div>', 'before after not affected');
node.firstChild.textContent = '0';
assert(node.firstChild.firstChild.nodeValue, '0', 'zero as nodeValue')
assert(node.toString(), '<div><p>0</p></div>', 'zero as text');
node.firstChild.textContent = null;
assert(node.firstChild.firstChild, null, 'textContent null clears all children and keeps childNodes empty')
assert(node.toString(), '<div><p></p></div>', 'null clears all children but does not set node');
node.firstChild.textContent = '';
assert(node.firstChild.firstChild.nodeValue, '', 'textContent empty string clears all children and contains single child with empty text')
assert(node.toString(), '<div><p></p></div>', 'empty text node as text');
node.firstChild.textContent = undefined;
assert(node.firstChild.firstChild, null, 'textContent undefined clears all children and keeps childNodes empty')
assert(node.toString(), '<div><p></p></div>', 'undefined clears all children but does not set node');
assert(text.isConnected, false, '!isConnected');
assert(text.parentElement, null, '!parentElement');
assert(node.contains(text), false, '!contains');
Expand Down Expand Up @@ -68,3 +80,7 @@ assert(node.childNodes.length, 1, 'normalize() empty text');
assert(text.nodeValue, 'text');
text.nodeValue = '';
assert(text.nodeValue, '');
text.nodeValue = '0'
assert(text.nodeValue, '0');
text.nodeValue = '';
assert(text.nodeValue, '');
2 changes: 1 addition & 1 deletion worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7304,7 +7304,7 @@ let Element$1 = class Element extends ParentNode {

set textContent(text) {
this.replaceChildren();
if (text)
if (text != null)
this.appendChild(new Text$1(this.ownerDocument, text));
}

Expand Down

0 comments on commit 760594b

Please sign in to comment.