class Sass::Script::Tree::StringInterpolation - sass-3.4.14 Documentation

# class Sass::Script::Tree::StringInterpolation

A SassScript object representing `#{}` interpolation within a string.

@see Interpolation

### Public Class Methods

new(before, mid, after) click to toggle source

Interpolation in a string is of the form `“before #{mid} after”`, where `before` and `after` may include more interpolation.

@param before [Node] The string before the interpolation @param mid [Node] The SassScript within the interpolation @param after [Node] The string after the interpolation

    # File lib/sass/script/tree/string_interpolation.rb, line 12
    def initialize(before, mid, after)
      @before = before
      @mid = mid
      @after = after
    end

### Public Instance Methods

children() click to toggle source

Returns the three components of the interpolation, `before`, `mid`, and `after`.

@return [Array<Node>] @see initialize @see Sass::Script::Tree::Node#children

    # File lib/sass/script/tree/string_interpolation.rb, line 59
    def children
      [@before, @mid, @after].compact
    end

deep_copy() click to toggle source

@see Sass::Script::Tree::Node#deep_copy

    # File lib/sass/script/tree/string_interpolation.rb, line 64
    def deep_copy
      node = dup
      node.instance_variable_set('@before', @before.deep_copy) if @before
      node.instance_variable_set('@mid', @mid.deep_copy)
      node.instance_variable_set('@after', @after.deep_copy) if @after
      node
    end

inspect() click to toggle source

@return [String] A human-readable s-expression representation of the interpolation

    # File lib/sass/script/tree/string_interpolation.rb, line 19
    def inspect
      "(string_interpolation #{@before.inspect} #{@mid.inspect} #{@after.inspect})"
    end

to_sass(opts = {}) click to toggle source

@see Sass::Script::Tree::Node#to_sass

    # File lib/sass/script/tree/string_interpolation.rb, line 24
    def to_sass(opts = {})
      # We can get rid of all of this when we remove the deprecated :equals context
      # XXX CE: It's gone now but I'm not sure what can be removed now.
      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

### Protected Instance Methods

_perform(environment) click to toggle source

Evaluates the interpolation.

@param environment [Sass::Environment] The environment in which to evaluate the SassScript @return [Sass::Script::Value::String]

    The SassScript string that is the value of the interpolation

    # File lib/sass/script/tree/string_interpolation.rb, line 79
    def _perform(environment)
      res = ""
      before = @before.perform(environment)
      res << before.value
      mid = @mid.perform(environment)
      res << (mid.is_a?(Sass::Script::Value::String) ? mid.value : mid.to_s(:quote => :none))
      res << @after.perform(environment).value
      opts(Sass::Script::Value::String.new(res, before.type))
    end

### Private Instance Methods

parse_str(str) click to toggle source

    # File lib/sass/script/tree/string_interpolation.rb, line 91
    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