class Sass::Script::Tree::StringInterpolation

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

@see Interpolation

Attributes

after[R]

@return [StringInterpolation, Literal]

The string literal or string interpolation before this interpolation.
before[R]

@return [Literal] The string literal before this interpolation.

mid[R]

@return [Node] The SassScript within the 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 [StringInterpolation, Literal] See {StringInterpolation#before} @param mid [Node] See {StringInterpolation#mid} @param after [Literal] See {StringInterpolation#after}

# File lib/sass/script/tree/string_interpolation.rb, line 39
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 69
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 74
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 46
def inspect
  "(string_interpolation #{@before.inspect} #{@mid.inspect} #{@after.inspect})"
end
quote() click to toggle source

Returns the quote character that should be used to wrap a Sass representation of this interpolation.

# File lib/sass/script/tree/string_interpolation.rb, line 29
def quote
  quote_for(self) || '"'
end
to_sass(opts = {}) click to toggle source

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

# File lib/sass/script/tree/string_interpolation.rb, line 51
def to_sass(opts = {})
  quote = type == :string ? opts[:quote] || quote_for(self) || '"' : :none
  opts = opts.merge(:quote => quote)

  res = ""
  res << quote if quote != :none
  res << _to_sass(before, opts)
  res << '#{' << @mid.to_sass(opts.merge(:quote => nil)) << '}'
  res << _to_sass(after, opts)
  res << quote if quote != :none
  res
end
type() click to toggle source

Whether this is a CSS string or a CSS identifier. The difference is that strings are written with double-quotes, while identifiers aren't.

String interpolations are only ever identifiers if they're quote-like functions such as `url()`.

@return [Symbol] `:string` or `:identifier`

# File lib/sass/script/tree/string_interpolation.rb, line 23
def type
  @before.value.type
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 89
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

_to_sass(string_or_interp, opts) click to toggle source
# File lib/sass/script/tree/string_interpolation.rb, line 101
def _to_sass(string_or_interp, opts)
  result = string_or_interp.to_sass(opts)
  opts[:quote] == :none ? result : result.slice(1...-1)
end
quote_for(string_or_interp) click to toggle source
# File lib/sass/script/tree/string_interpolation.rb, line 106
def quote_for(string_or_interp)
  if string_or_interp.is_a?(Sass::Script::Tree::Literal)
    return nil if string_or_interp.value.value.empty?
    return '"' if string_or_interp.value.value.include?("'")
    return "'" if string_or_interp.value.value.include?('"')
    return nil
  end

  # Double-quotes take precedence over single quotes.
  before_quote = quote_for(string_or_interp.before)
  return '"' if before_quote == '"'
  after_quote = quote_for(string_or_interp.after)
  return '"' if after_quote == '"'

  # Returns "'" if either or both insist on single quotes, and nil
  # otherwise.
  before_quote || after_quote
end