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,