fix(dom-renderer): use setAttribute for kebab-case data keys#44
Merged
Conversation
dataset[key] requires the key to already be camelCase; DOMStringMap
throws a SyntaxError for hyphenated property names (e.g. "section-id").
Data props are passed as kebab-case keys, so writing them via dataset
crashes updateNodeData in every conformant browser.
Use setAttribute('data-' + key, ...) instead, consistent with the
removeAttribute branch above it. This also fixes a latent set/remove
asymmetry for camelCase keys, where the set path wrote data-test-id
while the remove path targeted data-testid and silently no-op'd.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
What
updateNodeDatain the DOM renderer wrote data props vianode.div.dataset[key]. Switched tonode.div.setAttribute('data-' + key, ...).Why
dataset[key](DOMStringMap) requires the key to already be camelCase. The spec mandates aSyntaxErrorwhen the property name contains a hyphen followed by an ASCII lowercase letter, so writing a kebab-case key likesection-idcrashesupdateNodeDatain every conformant browser — not WebOS-specific (reproduced in a regular desktop browser and in jsdom).Our data props are passed as kebab-case, data-attribute-style keys, so
setAttribute('data-' + key, ...)is the correct path. It's also consistent with theremoveAttribute('data-' + key)branch right above it.Bonus fix
The set/remove branches were already inconsistent for camelCase keys (e.g. the internal
testId):dataset['testId']→ wrotedata-test-idremoveAttribute('data-testId')→ lowercased todata-testid→ never matched, so removal silently no-op'dRouting both through the literal
'data-' + keymakes set and remove round-trip correctly.Reviewer note
For camelCase keys the resulting attribute name changes from the auto-kebab'd
data-test-idto the lowercased literaldata-testid. In practice the only internal camelCase key istestId, anddata-testidis the conventional testing attribute (matches@testing-library's default). Flag if any consumer queries[data-test-id]specifically.Testing
npm run build— succeedsnpm test— 136/136 passing🤖 Generated with Claude Code