Sha256: ba291f4552b4b4668f3f99b5fb873bd7efe9765bc3faf068ae2f0f84b554855c

Contents?: true

Size: 1.03 KB

Versions: 11

Compression:

Stored size: 1.03 KB

Contents

class SCSSBeautifier::Formatters::StringQuotes < Sass::Tree::Visitors::Base
  def visit_prop(node)
    check_quotes(node)
    visit_children(node)
  end
  def check_quotes(node)
    return unless node.value.is_a?(Sass::Script::Tree::Literal)

    # TODO Check for single or double quote preference
    str_without_quotes = extract_string_without_quotes(node.value.value.to_s)
    change_to_single_quotes(node) if str_without_quotes && !str_without_quotes.match(/'/)
  end

  private

  STRING_WITHOUT_QUOTES_REGEX = %r{
    \A
    ["'](.*)["']    # Extract text between quotes
    \s*\)?\s*;?\s*  # Sometimes the Sass parser includes a trailing ) or ;
    (//.*)?         # Exclude any trailing comments that might have snuck in
    \z
  }x

  def change_to_single_quotes(node)
    new_val = node.value.value.to_s.gsub('"', '\'')
    node.instance_variable_set(:@value, Sass::Script::Value::String.new(new_val))
  end

  def extract_string_without_quotes(source)
    return unless match = STRING_WITHOUT_QUOTES_REGEX.match(source)
    match[1]
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
scss_beautifier-0.1.11 lib/scss_beautifier/formatters/string_quotes.rb
scss_beautifier-0.1.10 lib/scss_beautifier/formatters/string_quotes.rb
scss_beautifier-0.1.8 lib/scss_beautifier/formatters/string_quotes.rb
scss_beautifier-0.1.7 lib/scss_beautifier/formatters/string_quotes.rb
scss_beautifier-0.1.6 lib/scss_beautifier/formatters/string_quotes.rb
scss_beautifier-0.1.5 lib/scss_beautifier/formatters/string_quotes.rb
scss_beautifier-0.1.4 lib/scss_beautifier/formatters/string_quotes.rb
scss_beautifier-0.1.3 lib/scss_beautifier/formatters/string_quotes.rb
scss_beautifier-0.1.2 lib/scss_beautifier/formatters/string_quotes.rb
scss_beautifier-0.1.1 lib/scss_beautifier/formatters/string_quotes.rb
scss_beautifier-0.1.0 lib/scss_beautifier/formatters/string_quotes.rb