fix(isRgbColor): accept alpha values with more than 2 decimal digits#2793
Open
JSap0914 wants to merge 1 commit into
Open
fix(isRgbColor): accept alpha values with more than 2 decimal digits#2793JSap0914 wants to merge 1 commit into
JSap0914 wants to merge 1 commit into
Conversation
The alpha regex (0?\.\d\d?|1(\.0)?|0(\.0)?) limited alpha to at
most 2 decimal digits and did not allow trailing zeros on the boundary
values 1 and 0, causing valid CSS rgba() colors to be incorrectly
rejected:
isRgbColor('rgba(255,255,255,.123)') // false — should be true
isRgbColor('rgba(255,255,255,1.00)') // false — should be true
isRgbColor('rgba(0,0,0,0.123)') // false — should be true
The CSS specification defines <alpha-value> as a CSS <number>
(https://www.w3.org/TR/css-color-4/#typedef-alpha-value), which
imposes no limit on decimal precision.
Fix: widen the alpha sub-pattern to:
(0?\.\d+|1(\.0+)?|0(\.0+)?)
so that any number of decimal digits after the point is accepted, and
both 1.0 and 1.00 (and similarly for 0) are valid.
Closes validatorjs#2792
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2793 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 114 114
Lines 2587 2587
Branches 656 656
=========================================
Hits 2587 2587 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Summary
isRgbColorincorrectly rejected valid CSSrgba()colors where the alpha value had more than 2 decimal digits, or where alpha was1.00(trailing zeros on the boundary value 1).Closes #2792
Bug
The old alpha sub-pattern
(0?\.\d\d?|1(\.0)?|0(\.0)?)limited the decimal part to at most 2 digits and only allowed exactly one trailing zero on the boundary values:The CSS specification defines
<alpha-value>as a CSS<number>, which imposes no limit on decimal precision.Fix
Changed the alpha sub-pattern in both
rgbaColorandrgbaColorPercentfrom:to:
This allows any number of decimal digits after the dot and properly handles trailing zeros on the opaque (
1) and transparent (0) boundary values.Verification
npm test→ 317 passing, 1 failing (validator.isRgbColor("rgba(255,255,255,.123)") failed but should have passed)npm test→ 318 passingAll existing tests continue to pass. Values outside the 0–1 range (
rgba(0,0,0,2),rgba(0,0,0,1.5)) are still correctly rejected.