diff --git a/lib/css_parser/rule_set.rb b/lib/css_parser/rule_set.rb index 82cbc87..bd880a8 100644 --- a/lib/css_parser/rule_set.rb +++ b/lib/css_parser/rule_set.rb @@ -359,9 +359,8 @@ def expand_dimensions_shorthand! # :nodoc: # # TODO: rgba, hsl, hsla value.gsub!(RE_COLOUR) { |c| c.gsub(/(\s*,\s*)/, ',') } - value.gsub!(RE_FUNCTIONS) { |c| c.gsub(/\s+/, WHITESPACE_REPLACEMENT) } - matches = value.strip.split(/\s+/) + matches = split_value_preserving_function_whitespace(value) case matches.length when 1 @@ -377,11 +376,7 @@ def expand_dimensions_shorthand! # :nodoc: raise ArgumentError, "Cannot parse #{value}" end - t, r, b, l = values - - replacement = {top => t, right => r, bottom => b, left => l}.transform_values do |replacement_value| - replacement_value.gsub(WHITESPACE_REPLACEMENT, ' ') - end + replacement = [top, right, bottom, left].zip(values).to_h declarations.replace_declaration!(property, replacement, preserve_importance: true) end @@ -640,6 +635,19 @@ def parse_selectors!(selectors) # :nodoc: s end end + + def split_value_preserving_function_whitespace(value) + split_value = value.gsub(RE_FUNCTIONS) do |c| + c.gsub!(/\s+/, WHITESPACE_REPLACEMENT) + c + end + + matches = split_value.strip.split(/\s+/) + + matches.each do |c| + c.gsub!(WHITESPACE_REPLACEMENT, ' ') + end + end end class OffsetAwareRuleSet < RuleSet