Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dom-character-data.md
Original file line number Diff line number Diff line change
@@ -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.
13 changes: 11 additions & 2 deletions packages/ui/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand All @@ -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;
}
Expand Down
10 changes: 10 additions & 0 deletions packages/ui/test/wpt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading