Repository navigation
fix: keep the string value of valid numbers in number inputs - #1339
Merged
snowystinger merged 2 commits intoNov 10, 2026
Merged
snowystinger merged 2 commits into
snowystinger merged 2 commits into
Conversation
The JSDOM workaround in sanitizeValue ran every numeric string through String(Number(v)). That turned values like ".80" into "0.8" and "007" into "7", so pasting or typing them into <input type="number"> gave a different value than a browser does. Only normalize values that are not already a valid floating-point number, which is what the workaround is for (e.g. "1."). Closes testing-library#1053
snowystinger
reviewed
Nov 9, 2026
|
|
||
| // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-floating-point-number | ||
| function isValidFloatingPointNumber(v: string) { | ||
| return /^-?(?:\d+|\d*\.\d+)(?:[eE][-+]?\d+)?$/.test(v) |
Contributor
There was a problem hiding this comment.
snowystinger
approved these changes
Nov 10, 2026
snowystinger
left a comment
Contributor
There was a problem hiding this comment.
I think this looks good, thanks!
|
🎉 This PR is included in version 14.6.9 🎉 The release is available on: Your semantic-release bot 📦🚀 |
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
Pasting or typing
.80into<input type="number">gives0.8as the value. A browser keeps.80.Why
sanitizeValue()has a workaround for jsdom, which drops values like1.. It ran every numeric string throughString(Number(v)), so valid values like.80,1.50or007got rewritten too.How
I only apply the workaround when the value isn't already a valid floating-point number as the HTML spec defines it. Values like
1.still go throughNumber()like before.Added a test in
tests/documentfor setting the value directly, and apastetest that matches the repro in the issue. Both fail without the change. The full jest suite, typecheck and lint pass.Checklist
Closes #1053