From 4e3c35054d1ba86c1bb25ff6a6683d1acacac733 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sampo=20Kivist=C3=B6?= <2021355+Havunen@users.noreply.github.com> Date: Mon, 22 Jan 2024 15:46:34 +0200 Subject: [PATCH] textContent="" should clear all child nodes (#244) * textContent="" should clear all child nodes * fixed typo in assert msg --- cjs/interface/element.js | 2 +- esm/interface/element.js | 2 +- test/interface/text.js | 7 ++++++- worker.js | 2 +- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/cjs/interface/element.js b/cjs/interface/element.js index 0ccae56f..01cd83e9 100644 --- a/cjs/interface/element.js +++ b/cjs/interface/element.js @@ -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)); } diff --git a/esm/interface/element.js b/esm/interface/element.js index bbfd0948..07623917 100644 --- a/esm/interface/element.js +++ b/esm/interface/element.js @@ -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)); } diff --git a/test/interface/text.js b/test/interface/text.js index 10248084..53aa9f75 100644 --- a/test/interface/text.js +++ b/test/interface/text.js @@ -15,6 +15,9 @@ assert(node.toString(), '

', 'before after not affected'); node.firstChild.textContent = ''; assert(node.toString(), '

<test>

', '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(), '

', 'before after not affected'); node.firstChild.textContent = '0'; assert(node.firstChild.firstChild.nodeValue, '0', 'zero as nodeValue') @@ -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(), '

', '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(), '

', 'empty text node as text'); node.firstChild.textContent = undefined; assert(node.firstChild.firstChild, null, 'textContent undefined clears all children and keeps childNodes empty') diff --git a/worker.js b/worker.js index 6c70c1a6..0ab91531 100644 --- a/worker.js +++ b/worker.js @@ -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)); }