fix: keep CharacterData nodeValue and data in sync#990
Merged
karfau merged 5 commits intoApr 18, 2026
Conversation
Add Object.defineProperty accessors for data and nodeValue on CharacterData.prototype so that direct property assignment keeps both properties and length synchronized. Fixes xmldom#989
Store the raw value and only update length for strings. This preserves existing behavior for internal code paths that pass undefined (e.g. createProcessingInstruction).
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #990 +/- ##
==========================================
+ Coverage 96.25% 96.70% +0.45%
==========================================
Files 8 8
Lines 2296 2307 +11
Branches 608 610 +2
==========================================
+ Hits 2210 2231 +21
+ Misses 86 76 -10 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Set length to 0 for non-string values instead of leaving it stale.
…odevalue-data-sync # Conflicts: # lib/dom.js
karfau
approved these changes
Apr 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #989.
Problem
CharacterDatastores text in two plain properties —dataandnodeValue— that are not kept in sync under direct property assignment:XMLSerializerreadsnode.data, so changes vianodeValueare silently lost on serialization. The reverse (node.data = 'text'leavingnodeValuestale) also applies.Built-in mutation methods (
appendData,replaceData,splitText) are unaffected — they explicitly assign both properties. The issue is only with direct property assignment.Fix
Add
Object.definePropertyaccessors fordataandnodeValueonCharacterData.prototype, inside the existingtry { if (Object.defineProperty) { ... } }block:datais the canonical store (backed by_data); the getter normalizesnull/undefinedto'', and the setter updateslengthfor string assignmentsnodeValueis a thin alias that delegates todataThis follows the same pattern already used in the codebase for
textContentandLiveNodeList.length.Existing mutation methods are unchanged — their
this.nodeValue = this.data = textassignments now flow through the setters, which is redundant but correct and avoids any behavior change for environments withoutObject.defineProperty.Tests
New tests in
test/dom/character-data.test.js, covering:nodeValueassignment updatesdataandlength(Text, Comment, CDATASection)dataassignment updatesnodeValueandlength(Text, Comment, CDATASection)XMLSerializeroutput reflects both assignment paths per node typenullvalues stay in sync betweendataandnodeValuereplaceDataandappendDatastill work correctlycloneNodepreservesdata/nodeValue/lengthsplitTextpreserves sync on both halvesProcessingInstructionnodeValue/data sync and serializer outputSpec reference
WHATWG DOM Living Standard §4.10 — for
CharacterDatanodes, thenodeValuegetter and setter must be equivalent todata.Environments without
Object.definePropertykeep existing behavior; this PR does not add a separate fallback.Related to #42.