fix: throw on invalid rgb() and rgba() color strings - #10580
Open
giaBaoJS wants to merge 1 commit into
Open
Conversation
parseColor('rgb(a, b, c)') returned a Color with NaN channels instead
of throwing. The rgb branch matched with /^rgba?\((.*)\)$/ and passed
whatever was inside the parens through Number(), so NaN reached the
RGBColor constructor. The hsl and hsb branches validate their numeric
payload inside the regex, so they throw as documented.
Reject components that are empty or do not parse to a finite number,
which lets parseColor fall through to its "Invalid color value" error.
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.
Closes
parseColoris documented as "Throws an error if the string could not be parsed", and thehsl(),hsb()and#hexbranches all do. Thergb()/rgba()branch does not: it hands back aColorwhose channels areNaN.Intent: make the four parse branches agree on what an unparseable string is, so callers can keep relying on the documented throw. I picked the smallest change that does that rather than rewriting the rgb branch to a strict regex like the hsl/hsb ones, because the rgb branch also feeds the hex path and shares the
colorsarray with it.Reproduction:
Root cause:
RGBColor.parsematches with/^rgba?\((.*)\)$/, so anything between the parens is accepted and passed toNumber().Number('a')isNaN,??does not catchNaN, andclamp(NaN, 0, 255)isNaN, so the final guard (colors[0] === undefined || ...) never fires.HSL_REGEXandHSB_REGEXvalidate the numeric payload inside the regex instead, which is why those two behave correctly.This matters beyond the direct call:
useColorwrapsparseColorin a try/catch and returnsundefinedfor invalid input. Without the throw, aNaNColorflows intouseColorAreaState/useColorWheelState/useColorSliderStateand surfaces later as NaN thumb positions or an error thrown from a different stack.The fix rejects components that are empty or do not parse to a finite number, so
RGBColor.parsereturnsundefinedandparseColorfalls through to its existingInvalid color valueerror. The empty check is separate becauseNumber('')is0, notNaN. Valid input is unaffected, including the existing out-of-range clamping test (rgba(300, -10, 0, 4)).What the tests assert: three new cases in
packages/react-stately/test/color/Color.test.tsxunder the existingrgbdescribe, each assertingparseColorthrowsInvalid color value: <input>for non-numeric channels, a non-numeric alpha, and empty channels. They mirror theshould throw on invalid hex valuetest already in thehexdescribe. Reverting onlyColor.tsturns all three red with "Received function did not throw"; the other 26 cases in the file stay green either way.✅ Pull Request Checklist:
No issue exists for this; I found it while reading the color parsers. Happy to open one first if you would prefer that.
Docs are unchanged: the
parseColordocstring already promises the throw, so this makes the code match the existing documentation rather than changing it.📝 Test Instructions:
29 passing, up from 26. To see the failure, revert
packages/react-stately/src/color/Color.tsand rerun: the three new cases fail with "Received function did not throw".I also ran the wider color surface (
packages/react-stately/test/color,packages/react-aria/test/color,packages/@adobe/react-spectrum/test/color,packages/react-aria-components/test/Color*): 176 passing before, 179 after, no change in failures.This is parser logic with no rendered output, so there was nothing to check for mouse/touch/keyboard/screen reader, LTR/RTL, light/dark, or sizing. AI-assisted, reviewed and verified by me.
🧢 Your Project:
Personal contribution.