Class: Sass::Script::StringInterpolation

Inherits:
Node show all
Defined in:
lib/sass/script/string_interpolation.rb

Instance Attribute Summary

Attributes inherited from Node

#context, #line, #options

Instance Method Summary collapse

Methods inherited from Node

#dasherize, #perform

Constructor Details

#initialize(before, mid, after) ⇒ StringInterpolation

Returns a new instance of StringInterpolation.



3
4
5
6
7
# File 'lib/sass/script/string_interpolation.rb', line 3

def initialize(before, mid, after)
  @before = before
  @mid = mid
  @after = after
end

Instance Method Details

#_perform(environment) (protected)



48
49
50
51
52
53
54
55
# File 'lib/sass/script/string_interpolation.rb', line 48

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

#children



42
43
44
# File 'lib/sass/script/string_interpolation.rb', line 42

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

#inspect



9
10
11
# File 'lib/sass/script/string_interpolation.rb', line 9

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

#parse_str(str) (protected)



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sass/script/string_interpolation.rb', line 57

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

#to_sass(opts = {})



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sass/script/string_interpolation.rb', line 13

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