Class: Sass::Script::Tree::Interpolation

Inherits:
Node
  • Object
show all
Defined in:
lib/sass/script/tree/interpolation.rb

Overview

A SassScript object representing #{} interpolation outside a string.

Instance Attribute Summary collapse

Attributes inherited from Node

#filename, #line, #options, #source_range

Instance Method Summary collapse

Methods inherited from Node

#dasherize, #force_division!, #opts, #perform

Constructor Details

#initialize(before, mid, after, wb, wa, opts = {}) ⇒ Interpolation

Interpolation in a property is of the form before #{mid} after.

Parameters:



50
51
52
53
54
55
56
57
58
59
# File 'lib/sass/script/tree/interpolation.rb', line 50

def initialize(before, mid, after, wb, wa, opts = {})
  @before = before
  @mid = mid
  @after = after
  @whitespace_before = wb
  @whitespace_after = wa
  @originally_text = opts[:originally_text] || false
  @warn_for_color = opts[:warn_for_color] || false
  @deprecation = opts[:deprecation] || :none
end

Instance Attribute Details

#afterNode (readonly)

Returns The SassScript after the interpolation.

Returns:

  • (Node)

    The SassScript after the interpolation



13
14
15
# File 'lib/sass/script/tree/interpolation.rb', line 13

def after
  @after
end

#beforeNode (readonly)

Returns The SassScript before the interpolation.

Returns:

  • (Node)

    The SassScript before the interpolation



7
8
9
# File 'lib/sass/script/tree/interpolation.rb', line 7

def before
  @before
end

#deprecationSymbol (readonly)

The type of interpolation deprecation for this node.

This can be :none, indicating that the node doesn't use deprecated interpolation; :immediate, indicating that a deprecation warning should be emitted as soon as possible; or :potential, indicating that a deprecation warning should be emitted if the resulting string is used in a way that would distinguish it from a list.

Returns:

  • (Symbol)


39
40
41
# File 'lib/sass/script/tree/interpolation.rb', line 39

def deprecation
  @deprecation
end

#midNode (readonly)

Returns The SassScript within the interpolation.

Returns:

  • (Node)

    The SassScript within the interpolation



10
11
12
# File 'lib/sass/script/tree/interpolation.rb', line 10

def mid
  @mid
end

#originally_textBoolean (readonly)

Returns Whether the original format of the interpolation was plain text, not an interpolation. This is used when converting back to SassScript.

Returns:

  • (Boolean)

    Whether the original format of the interpolation was plain text, not an interpolation. This is used when converting back to SassScript.



24
25
26
# File 'lib/sass/script/tree/interpolation.rb', line 24

def originally_text
  @originally_text
end

#warn_for_colorBoolean (readonly)

Returns Whether a color value passed to the interpolation should generate a warning.

Returns:

  • (Boolean)

    Whether a color value passed to the interpolation should generate a warning.



28
29
30
# File 'lib/sass/script/tree/interpolation.rb', line 28

def warn_for_color
  @warn_for_color
end

#whitespace_afterBoolean (readonly)

Returns Whether there was whitespace between } and after.

Returns:

  • (Boolean)

    Whether there was whitespace between } and after



19
20
21
# File 'lib/sass/script/tree/interpolation.rb', line 19

def whitespace_after
  @whitespace_after
end

#whitespace_beforeBoolean (readonly)

Returns Whether there was whitespace between before and #{.

Returns:

  • (Boolean)

    Whether there was whitespace between before and #{



16
17
18
# File 'lib/sass/script/tree/interpolation.rb', line 16

def whitespace_before
  @whitespace_before
end

Instance Method Details

#childrenArray<Node>

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

Returns:

See Also:



99
100
101
# File 'lib/sass/script/tree/interpolation.rb', line 99

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

#deep_copy

See Also:



104
105
106
107
108
109
110
# File 'lib/sass/script/tree/interpolation.rb', line 104

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

#inspectString

Returns A human-readable s-expression representation of the interpolation.

Returns:

  • (String)

    A human-readable s-expression representation of the interpolation



62
63
64
# File 'lib/sass/script/tree/interpolation.rb', line 62

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

#to_quoted_equivalentSass::Script::Tree::Node

Returns an unquote() expression that will evaluate to the same value as this interpolation.



85
86
87
88
89
90
91
92
# File 'lib/sass/script/tree/interpolation.rb', line 85

def to_quoted_equivalent
  Funcall.new(
    "unquote",
    [to_string_interpolation(self)],
    Sass::Util::NormalizedMap.new,
    nil,
    nil)
end

#to_sass(opts = {})

See Also:



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sass/script/tree/interpolation.rb', line 67

def to_sass(opts = {})
  return to_quoted_equivalent.to_sass if deprecation == :immediate

  res = ""
  res << @before.to_sass(opts) if @before
  res << ' ' if @before && @whitespace_before
  res << '#{' unless @originally_text
  res << @mid.to_sass(opts)
  res << '}' unless @originally_text
  res << ' ' if @after && @whitespace_after
  res << @after.to_sass(opts) if @after
  res
end

#to_string_interpolation(node_or_interp) ⇒ Sass::Script::Tree::StringInterpolation (protected)

Converts a script node into a corresponding string interpolation expression.

Parameters:

Returns:



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/sass/script/tree/interpolation.rb', line 119

def to_string_interpolation(node_or_interp)
  unless node_or_interp.is_a?(Interpolation)
    node = node_or_interp
    return string_literal(node.value.to_s) if node.is_a?(Literal)
    if node.is_a?(StringInterpolation)
      return concat(string_literal(node.quote), concat(node, string_literal(node.quote)))
    end
    return StringInterpolation.new(string_literal(""), node, string_literal(""))
  end

  interp = node_or_interp
  after_string_or_interp =
    if interp.after
      to_string_interpolation(interp.after)
    else
      string_literal("")
    end
  if interp.after && interp.whitespace_after
    after_string_or_interp = concat(string_literal(' '), after_string_or_interp)
  end

  mid_string_or_interp = to_string_interpolation(interp.mid)

  before_string_or_interp =
    if interp.before
      to_string_interpolation(interp.before)
    else
      string_literal("")
    end
  if interp.before && interp.whitespace_before
    before_string_or_interp = concat(before_string_or_interp, string_literal(' '))
  end

  concat(before_string_or_interp, concat(mid_string_or_interp, after_string_or_interp))
end