Sha256: cf2d9b1e6eead0602f625980907f27c23e528fab8c90b2e0c3139884953452cc

Contents?: true

Size: 1010 Bytes

Versions: 1

Compression:

Stored size: 1010 Bytes

Contents

module SCSSLint
  class Linter::ColorKeyword < Linter
    include LinterRegistry

    def visit_script_color(node)
      add_color_lint(node, node.original_string) if color_keyword?(node.original_string)
    end

    def visit_script_string(node)
      return unless node.type == :identifier

      remove_quoted_strings(node.value).scan(/[a-z]+/i) do |word|
        add_color_lint(node, word) if color_keyword?(word)
      end
    end

  private

    def add_color_lint(node, original)
      hex_form = Sass::Script::Value::Color.new(color_rgb(original)).tap do |color|
        color.options = {} # `inspect` requires options to be set
      end.inspect

      add_lint(node,
               "Color `#{original}` should be written in hexadecimal form " <<
               "as `#{shortest_hex_form(hex_form)}`")
    end

    def color_keyword?(string)
      !!color_rgb(string) && string != 'transparent'
    end

    def color_rgb(string)
      Sass::Script::Value::Color::COLOR_NAMES[string]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
scss-lint-0.11.1 lib/scss_lint/linter/color_keyword.rb