Sha256: 1b5f477c968a868a4f8d182fbb4aae26db0fabac031bcce45e432b1b001472a8

Contents?: true

Size: 1.98 KB

Versions: 18

Compression:

Stored size: 1.98 KB

Contents

module Sass::Script
  class StringInterpolation < Node
    def initialize(before, mid, after)
      @before = before
      @mid = mid
      @after = after
    end

    def inspect
      "(string_interpolation #{@before.inspect} #{@mid.inspect} #{@after.inspect})"
    end

    def to_sass(opts = {})
      # We can get rid of all of this when we remove the deprecated :equals context
      before_unquote, before_quote_char, before_str = parse_str(@before.to_sass(opts))
      after_unquote, after_quote_char, after_str = parse_str(@after.to_sass(opts))
      unquote = before_unquote || after_unquote ||
        (before_quote_char && !after_quote_char && !after_str.empty?) ||
        (!before_quote_char && after_quote_char && !before_str.empty?)
      quote_char =
        if before_quote_char && after_quote_char && before_quote_char != after_quote_char
          before_str.gsub!("\\'", "'")
          before_str.gsub!('"', "\\\"")
          after_str.gsub!("\\'", "'")
          after_str.gsub!('"', "\\\"")
          '"'
        else
          before_quote_char || after_quote_char
        end

      res = ""
      res << 'unquote(' if unquote
      res << quote_char if quote_char
      res << before_str
      res << '#{' << @mid.to_sass(opts) << '}'
      res << after_str
      res << quote_char if quote_char
      res << ')' if unquote
      res
    end

    def children
      [@before, @mid, @after].compact
    end

    protected

    def _perform(environment)
      res = ""
      res << @before.perform(environment).value
      val = @mid.perform(environment)
      res << (val.is_a?(Sass::Script::String) ? val.value : val.to_s)
      res << @after.perform(environment).value
      Sass::Script::String.new(res, :string)
    end

    def parse_str(str)
      case str
      when /^unquote\((["'])(.*)\1\)$/
        return true, $1, $2
      when '""'
        return false, nil, ""
      when /^(["'])(.*)\1$/
        return false, $1, $2
      else
        return false, nil, str
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
haml-edge-2.3.209 lib/sass/script/string_interpolation.rb
haml-3.0.0.beta.3 lib/sass/script/string_interpolation.rb
haml-edge-2.3.208 lib/sass/script/string_interpolation.rb
haml-edge-2.3.207 lib/sass/script/string_interpolation.rb
haml-edge-2.3.206 lib/sass/script/string_interpolation.rb
haml-edge-2.3.205 lib/sass/script/string_interpolation.rb
haml-edge-2.3.204 lib/sass/script/string_interpolation.rb
haml-3.0.0.beta.2 lib/sass/script/string_interpolation.rb
haml-edge-2.3.203 lib/sass/script/string_interpolation.rb
haml-edge-2.3.202 lib/sass/script/string_interpolation.rb
haml-edge-2.3.201 lib/sass/script/string_interpolation.rb
haml-edge-2.3.200 lib/sass/script/string_interpolation.rb
haml-edge-2.3.199 lib/sass/script/string_interpolation.rb
haml-edge-2.3.198 lib/sass/script/string_interpolation.rb
haml-edge-2.3.197 lib/sass/script/string_interpolation.rb
haml-edge-2.3.196 lib/sass/script/string_interpolation.rb
haml-edge-2.3.195 lib/sass/script/string_interpolation.rb
haml-edge-2.3.194 lib/sass/script/string_interpolation.rb