Skip to content

Commit aa3e927

Browse files
authored
Support character data assignment (#56)
## What does this PR do? Normalize Text and Comment `data` assignments to strings, treating `null` as an empty string, and make `nodeValue` and `textContent` writable through the same data setter. ## Type of change - [ ] Bug fix - [x] Feature - [ ] Refactor (no behavior change) - [ ] Documentation - [ ] Performance improvement - [ ] Tests - [ ] Chore (dependencies, CI, tooling) ## Checklist - [x] All tests pass (`pnpm test`) - [x] Files are formatted (`pnpm format`) - [x] I have added/updated tests for my changes (if applicable) - [x] I have added a changeset ## AI-generated code disclosure - [ ] This PR includes AI-generated code
1 parent ed10c9f commit aa3e927

3 files changed

Lines changed: 26 additions & 2 deletions

File tree

‎.changeset/dom-character-data.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clack/ui': minor
3+
---
4+
5+
Support Text and Comment nodeValue and textContent assignment and normalize CharacterData data assignments to strings, treating null as an empty string.

‎packages/ui/src/dom.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,9 @@ export abstract class CharacterData extends Node {
124124
}
125125

126126
set data(data: string) {
127-
if (data !== this.data) {
128-
this.#data = data;
127+
const normal = data === null ? '' : String(data);
128+
if (normal !== this.data) {
129+
this.#data = normal;
129130
this.notify();
130131
}
131132
}
@@ -134,10 +135,18 @@ export abstract class CharacterData extends Node {
134135
return this.data;
135136
}
136137

138+
override set nodeValue(value: string | null) {
139+
this.data = value ?? '';
140+
}
141+
137142
get textContent(): string {
138143
return this.data;
139144
}
140145

146+
set textContent(value: string | null) {
147+
this.data = value ?? '';
148+
}
149+
141150
get length(): number {
142151
return this.data.length;
143152
}

‎packages/ui/test/wpt.config.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ export default {
1515
'dom/nodes/Document-createTextNode.html': true,
1616
'dom/nodes/Document-createElement.html': true,
1717
'dom/nodes/Document-createComment.html': true,
18+
'dom/nodes/CharacterData-data.html': true,
19+
'dom/nodes/Node-nodeValue.html': ['Text.nodeValue', 'Comment.nodeValue'],
20+
'dom/nodes/Node-textContent.html': [
21+
'For an empty Text, textContent should be the empty string',
22+
'For an empty Comment, textContent should be the empty string',
23+
'For a Text with data, textContent should be that data',
24+
'For a Comment with data, textContent should be that data',
25+
'For a Text, textContent should set the data',
26+
'For a Comment, textContent should set the data',
27+
],
1828
},
1929
exclude: {
2030
'css/**': true,

0 commit comments

Comments
 (0)