Skip to content

Commit 920a01b

Browse files
authored
Merge pull request #187 from fukayatsu/fix-rgba-leading-dot-alpha
Accept CSS numbers without a leading zero in rgb/rgba/hsl/hsla
2 parents 300f130 + 9fc40c6 commit 920a01b

4 files changed

Lines changed: 24 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Unreleased
44

5+
* Accept CSS `<number>` values with an omitted integer part (e.g. `.1`) inside `rgb()`/`rgba()`/`hsl()`/`hsla()`. Previously `RE_COLOUR_NUMERIC` and `RE_COLOUR_NUMERIC_ALPHA` required at least one digit before the decimal point, which caused colours such as `rgba(0,0,0,.1)` to be silently dropped during shorthand expansion (`background-color` from `background:`, `border-*-color` from `border:`).
6+
57
### Version 2.1.0
68
* Validate ssl when pulling files via https
79

lib/css_parser/regexps.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,10 @@ def self.regex_possible_values(*values)
259259
inherit
260260
currentColor
261261
].freeze
262-
RE_COLOUR_NUMERIC = /\b(hsl|rgb)\s*\(-?\s*-?\d+(\.\d+)?%?\s*%?,-?\s*-?\d+(\.\d+)?%?\s*%?,-?\s*-?\d+(\.\d+)?%?\s*%?\)/i.freeze
263-
RE_COLOUR_NUMERIC_ALPHA = /\b(hsla|rgba)\s*\(-?\s*-?\d+(\.\d+)?%?\s*%?,-?\s*-?\d+(\.\d+)?%?\s*%?,-?\s*-?\d+(\.\d+)?%?\s*%?,-?\s*-?\d+(\.\d+)?%?\s*%?\)/i.freeze
262+
# CSS <number> allows the integer part to be omitted (e.g. `.1`), per CSS Values & Units.
263+
# `(?:\d*\.)?\d+` accepts `1`, `1.5`, and `.5` while still rejecting bare `1.`.
264+
RE_COLOUR_NUMERIC = /\b(hsl|rgb)\s*\(-?\s*-?(?:\d*\.)?\d+%?\s*%?,-?\s*-?(?:\d*\.)?\d+%?\s*%?,-?\s*-?(?:\d*\.)?\d+%?\s*%?\)/i.freeze
265+
RE_COLOUR_NUMERIC_ALPHA = /\b(hsla|rgba)\s*\(-?\s*-?(?:\d*\.)?\d+%?\s*%?,-?\s*-?(?:\d*\.)?\d+%?\s*%?,-?\s*-?(?:\d*\.)?\d+%?\s*%?,-?\s*-?(?:\d*\.)?\d+%?\s*%?\)/i.freeze
264266
RE_COLOUR_HEX = /\s*#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})\b/.freeze
265267
RE_COLOUR_NAMED = /\s*\b(#{NAMED_COLOURS.join('|')})\b/i.freeze
266268
RE_COLOUR = Regexp.union(RE_COLOUR_NUMERIC, RE_COLOUR_NUMERIC_ALPHA, RE_COLOUR_HEX, RE_COLOUR_NAMED)

test/test_css_parser_regexps.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def test_colour
4040
'color: #fff', 'color:#f0a09c;', 'color: #04A', 'color: #04a9CE',
4141
'color: rgb(100, -10%, 300);', 'color: rgb(10,10,10)', 'color:rgb(12.7253%, -12%,0)',
4242
'color: hsla(-15, -77%, 19%, 5%);',
43+
'color: rgba(0,0,0,.1)', 'color: rgba(0, 0, 0, .5)', 'color: hsla(0, 0%, 0%, .05)',
4344
'color: black', 'color:Red;', 'color: AqUa;', 'color: blue ', 'color: transparent',
4445
'color: darkslategray'
4546
].each do |colour|

test/test_rule_set_expanding_shorthand.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ def test_expanding_border_shorthand
1818
assert_equal '1px', declarations['border-top-width']
1919
assert_equal 'solid', declarations['border-bottom-style']
2020

21+
# Regression: rgba/hsla with no leading zero on the alpha (e.g. `.1`) used
22+
# to fail the colour regex, causing border-*-color to be silently dropped
23+
# during shorthand expansion.
24+
declarations = expand_declarations('border: 1px solid rgba(0,0,0,.1)')
25+
assert_equal '1px', declarations['border-top-width']
26+
assert_equal 'solid', declarations['border-top-style']
27+
assert_equal 'rgba(0,0,0,.1)', declarations['border-top-color']
28+
assert_equal 'rgba(0,0,0,.1)', declarations['border-right-color']
29+
assert_equal 'rgba(0,0,0,.1)', declarations['border-bottom-color']
30+
assert_equal 'rgba(0,0,0,.1)', declarations['border-left-color']
31+
2132
declarations = expand_declarations('border-color: red hsla(255, 0, 0, 5) rgb(2% ,2%,2%)')
2233
assert_equal 'red', declarations['border-top-color']
2334
assert_equal 'rgb(2%,2%,2%)', declarations['border-bottom-color']
@@ -197,7 +208,12 @@ def test_getting_background_size_from_shorthand
197208
end
198209

199210
def test_getting_background_colour_from_shorthand
200-
['blue', 'lime', 'rgb(10,10,10)', 'rgb ( -10%, 99, 300)', '#ffa0a0', '#03c', 'trAnsparEnt', 'inherit'].each do |colour|
211+
[
212+
'blue', 'lime', 'rgb(10,10,10)', 'rgb ( -10%, 99, 300)', '#ffa0a0', '#03c', 'trAnsparEnt', 'inherit',
213+
# Regression: alpha without a leading zero (e.g. `.1`) used to fail the
214+
# colour regex and silently drop background-color.
215+
'rgba(0,0,0,.1)'
216+
].each do |colour|
201217
shorthand = "background:#{colour} url('chess.png') center repeat fixed ;"
202218
declarations = expand_declarations(shorthand)
203219
assert_equal(colour, declarations['background-color'])

0 commit comments

Comments
 (0)