Sha256: 9f6bab2c056b7e957bf021d0f8b64d2e15ea56432d9701c1d13592310a00f183

Contents?: true

Size: 990 Bytes

Versions: 4

Compression:

Stored size: 990 Bytes

Contents

class SCSSBeautifier::Formatters::Color < SCSSBeautifier::Formatters::Base
  HEX_REGEX = /(#\h{3})(?!\h)/

  def visit_prop(node)
    if node.value.respond_to?(:each)
      node.value.each{ |item| format_color(item) }
    else
      format_color(node.value)
    end
  end

  def format_color(item)
    if Sass::Script::Tree::Literal === item && Sass::Script::Value::String === item.value
      if color = Sass::Script::Value::Color::COLOR_NAMES[item.value.value]
        color_value = Sass::Script::Value::Color.new(color)
        color_value.options = {}
        item.instance_variable_set(:@value, Sass::Script::Value::String.new(color_value.inspect))
      elsif item.value.value =~ HEX_REGEX

        new_val = item.value.value.split(" ").map do |val|
          val.match(HEX_REGEX) ? [val[0..1], val[1], val[2], val[2], val[3], val[3]].join : val
        end.join(" ")

        item.instance_variable_set(:@value, Sass::Script::Value::String.new(new_val))
      end
    end
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
scss_beautifier-0.1.19 lib/scss_beautifier/formatters/color.rb
scss_beautifier-0.1.18 lib/scss_beautifier/formatters/color.rb
scss_beautifier-0.1.17 lib/scss_beautifier/formatters/color.rb
scss_beautifier-0.1.16 lib/scss_beautifier/formatters/color.rb