-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ensure color is string before calling toLowerCase() #10129
Conversation
@@ -49,7 +49,7 @@ function normalizeColor(color) { | |||
} else if (color in aliases) { | |||
return aliases[color] | |||
} else if (isHexColor(color)) { | |||
return `#${color.toLowerCase()}` | |||
return `#${color.toString().toLowerCase()}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about moving the toString()
to isHexColor()
? Like this below:
function isHexColor(s = '') {
return hexColorRegex.test(String(s))
}
The String(s)
is safer here. Even if the value is null,
it will not throw an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that would really help tbh.
The issue here is that the current implementation of isHexColor()
may already return true on a non-string input (I guess .test()
implicitly casts to string), leading us to try and call toLowerCase()
on an int. For example:
> isHexColor(111111)
true
so even if we make the cast explicit, we'd still need to call toString()
in this block, or do it somewhere before we reach here.
Null or undefined already return false with the current implementation:
> isHexColor(null)
false
> isHexColor(undefined)
false
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds reasonable to me 👍
Fixes #10128
I don't have a reliable repro for this bug, but somewhere along the line we are allowing a hex colour which is all numeric to not be a string.
I suggest hitting with a hammer until it plays nicely 🔨
(I'm aware that somewhere a typescript enthusiast is having an aneurysm)