Skip to content

Commit

Permalink
textContent="" should clear all child nodes (#244)
Browse files Browse the repository at this point in the history
* textContent="" should clear all child nodes

* fixed typo in assert msg
  • Loading branch information
Havunen committed Jan 22, 2024
1 parent 37ad011 commit 4e3c350
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cjs/interface/element.js
Expand Up @@ -190,7 +190,7 @@ class Element extends ParentNode {

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

Expand Down
2 changes: 1 addition & 1 deletion esm/interface/element.js
Expand Up @@ -192,7 +192,7 @@ export class Element extends ParentNode {

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

Expand Down
7 changes: 6 additions & 1 deletion test/interface/text.js
Expand Up @@ -15,6 +15,9 @@ assert(node.toString(), '<div><p></p></div>', 'before after not affected');
node.firstChild.textContent = '<test>';
assert(node.toString(), '<div><p>&lt;test&gt;</p></div>', 'before after not affected');
node.firstChild.textContent = '';
assert(node.firstChild.childNodes.length, 0, 'there should be no leftovers when clearing content with textContent=""')
assert(node.firstChild.firstChild, null, 'textContent empty string clears children')
assert(node.firstChild.textContent, '', 'textContent was set empty string')
assert(node.toString(), '<div><p></p></div>', 'before after not affected');
node.firstChild.textContent = '0';
assert(node.firstChild.firstChild.nodeValue, '0', 'zero as nodeValue')
Expand All @@ -23,7 +26,9 @@ 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.firstChild.childNodes.length, 0, 'there should be no leftovers when clearing content with textContent=""')
assert(node.firstChild.firstChild, null, 'textContent empty string clears children')
assert(node.firstChild.textContent, '', 'textContent was set empty string')
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')
Expand Down
2 changes: 1 addition & 1 deletion worker.js
Expand Up @@ -7738,7 +7738,7 @@ let Element$1 = class Element extends ParentNode {

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

Expand Down

0 comments on commit 4e3c350

Please sign in to comment.