PageRenderTime 33ms CodeModel.GetById 14ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/scalate-jruby/src/main/resources/haml-3.0.25/lib/sass/script/string_interpolation.rb

http://github.com/scalate/scalate
Ruby | 93 lines | 64 code | 8 blank | 21 comment | 14 complexity | 1ff565f2f656c067eb521da6f02482f3 MD5 | raw file
 1module Sass::Script
 2  # A SassScript object representing `#{}` interpolation within a string.
 3  #
 4  # @see Interpolation
 5  class StringInterpolation < Node
 6    # Interpolation in a string is of the form `"before #{mid} after"`,
 7    # where `before` and `after` may include more interpolation.
 8    #
 9    # @param before [Node] The string before the interpolation
10    # @param mid [Node] The SassScript within the interpolation
11    # @param after [Node] The string after the interpolation
12    def initialize(before, mid, after)
13      @before = before
14      @mid = mid
15      @after = after
16    end
17
18    # @return [String] A human-readable s-expression representation of the interpolation
19    def inspect
20      "(string_interpolation #{@before.inspect} #{@mid.inspect} #{@after.inspect})"
21    end
22
23    # @see Node#to_sass
24    def to_sass(opts = {})
25      # We can get rid of all of this when we remove the deprecated :equals context
26      before_unquote, before_quote_char, before_str = parse_str(@before.to_sass(opts))
27      after_unquote, after_quote_char, after_str = parse_str(@after.to_sass(opts))
28      unquote = before_unquote || after_unquote ||
29        (before_quote_char && !after_quote_char && !after_str.empty?) ||
30        (!before_quote_char && after_quote_char && !before_str.empty?)
31      quote_char =
32        if before_quote_char && after_quote_char && before_quote_char != after_quote_char
33          before_str.gsub!("\\'", "'")
34          before_str.gsub!('"', "\\\"")
35          after_str.gsub!("\\'", "'")
36          after_str.gsub!('"', "\\\"")
37          '"'
38        else
39          before_quote_char || after_quote_char
40        end
41
42      res = ""
43      res << 'unquote(' if unquote
44      res << quote_char if quote_char
45      res << before_str
46      res << '#{' << @mid.to_sass(opts) << '}'
47      res << after_str
48      res << quote_char if quote_char
49      res << ')' if unquote
50      res
51    end
52
53    # Returns the three components of the interpolation, `before`, `mid`, and `after`.
54    #
55    # @return [Array<Node>]
56    # @see #initialize
57    # @see Node#children
58    def children
59      [@before, @mid, @after].compact
60    end
61
62    protected
63
64    # Evaluates the interpolation.
65    #
66    # @param environment [Sass::Environment] The environment in which to evaluate the SassScript
67    # @return [Sass::Script::String] The SassScript string that is the value of the interpolation
68    def _perform(environment)
69      res = ""
70      before = @before.perform(environment)
71      res << before.value
72      mid = @mid.perform(environment)
73      res << (mid.is_a?(Sass::Script::String) ? mid.value : mid.to_s)
74      res << @after.perform(environment).value
75      opts(Sass::Script::String.new(res, before.type))
76    end
77
78    private
79
80    def parse_str(str)
81      case str
82      when /^unquote\((["'])(.*)\1\)$/
83        return true, $1, $2
84      when '""'
85        return false, nil, ""
86      when /^(["'])(.*)\1$/
87        return false, $1, $2
88      else
89        return false, nil, str
90      end
91    end
92  end
93end