From 89d7f2c21dcf2decbc2509c15e7cfd1e2c44c4b6 Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Wed, 11 Nov 2026 07:39:19 -0600 Subject: [PATCH] Support character data assignment --- .changeset/dom-character-data.md | 5 +++++ packages/ui/src/dom.ts | 13 +++++++++++-- packages/ui/test/wpt.config.ts | 10 ++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 .changeset/dom-character-data.md diff --git a/.changeset/dom-character-data.md b/.changeset/dom-character-data.md new file mode 100644 index 0000000..3cc6996 --- /dev/null +++ b/.changeset/dom-character-data.md @@ -0,0 +1,5 @@ +--- +'@clack/ui': minor +--- + +Support Text and Comment nodeValue and textContent assignment and normalize CharacterData data assignments to strings, treating null as an empty string. diff --git a/packages/ui/src/dom.ts b/packages/ui/src/dom.ts index bccb240..fe2f1e8 100644 --- a/packages/ui/src/dom.ts +++ b/packages/ui/src/dom.ts @@ -124,8 +124,9 @@ export abstract class CharacterData extends Node { } set data(data: string) { - if (data !== this.data) { - this.#data = data; + const normal = data === null ? '' : String(data); + if (normal !== this.data) { + this.#data = normal; this.notify(); } } @@ -134,10 +135,18 @@ export abstract class CharacterData extends Node { return this.data; } + override set nodeValue(value: string | null) { + this.data = value ?? ''; + } + get textContent(): string { return this.data; } + set textContent(value: string | null) { + this.data = value ?? ''; + } + get length(): number { return this.data.length; } diff --git a/packages/ui/test/wpt.config.ts b/packages/ui/test/wpt.config.ts index fe88a3c..7a4ed5f 100644 --- a/packages/ui/test/wpt.config.ts +++ b/packages/ui/test/wpt.config.ts @@ -15,6 +15,16 @@ export default { 'dom/nodes/Document-createTextNode.html': true, 'dom/nodes/Document-createElement.html': true, 'dom/nodes/Document-createComment.html': true, + 'dom/nodes/CharacterData-data.html': true, + 'dom/nodes/Node-nodeValue.html': ['Text.nodeValue', 'Comment.nodeValue'], + 'dom/nodes/Node-textContent.html': [ + 'For an empty Text, textContent should be the empty string', + 'For an empty Comment, textContent should be the empty string', + 'For a Text with data, textContent should be that data', + 'For a Comment with data, textContent should be that data', + 'For a Text, textContent should set the data', + 'For a Comment, textContent should set the data', + ], }, exclude: { 'css/**': true,