Skip to content

Commit

Permalink
Move whitespace preserving split to `RuleSet#split_value_preserving_w…
Browse files Browse the repository at this point in the history
…hitespace`
  • Loading branch information
dark-panda committed Jul 27, 2021
1 parent 720eb34 commit 2b65639
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions lib/css_parser/rule_set.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2b65639

Please sign in to comment.