Class: Sass::Tree::Visitors::Perform

Inherits:
Base
  • Object
show all
Defined in:
lib/sass/tree/visitors/perform.rb

Overview

A visitor for converting a dynamic Sass tree into a static Sass tree.

Constant Summary

Constants inherited from Base

Base::NODE_NAME_RE

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#node_name

Constructor Details

#initialize(env) ⇒ Perform (protected)

Returns a new instance of Perform.



12
13
14
# File 'lib/sass/tree/visitors/perform.rb', line 12

def initialize(env)
  @environment = env
end

Class Method Details

.visit(root, environment = Sass::Environment.new) ⇒ Tree::Node

Returns The resulting tree of static nodes.

Parameters:

  • root (Tree::Node)

    The root node of the tree to visit.

  • environment (Sass::Environment) (defaults to: Sass::Environment.new)

    The lexical environment.

Returns:

  • (Tree::Node)

    The resulting tree of static nodes.



6
7
8
# File 'lib/sass/tree/visitors/perform.rb', line 6

def self.visit(root, environment = Sass::Environment.new)
  new(environment).send(:visit, root)
end

Instance Method Details

#visit(node) (protected)

If an exception is raised, this add proper metadata to the backtrace.



17
18
19
20
21
22
# File 'lib/sass/tree/visitors/perform.rb', line 17

def visit(node)
  super(node.dup)
rescue Sass::SyntaxError => e
  e.modify_backtrace(:filename => node.filename, :line => node.line)
  raise e
end

#visit_children(parent) (protected)

Keeps track of the current environment.



25
26
27
28
29
30
# File 'lib/sass/tree/visitors/perform.rb', line 25

def visit_children(parent)
  with_environment Sass::Environment.new(@environment) do
    parent.children = super.flatten
    parent
  end
end

#visit_comment(node) (protected)

Removes this node from the tree if it's a silent comment.



54
55
56
57
58
59
60
61
62
63
# File 'lib/sass/tree/visitors/perform.rb', line 54

def visit_comment(node)
  return [] if node.invisible?
  if node.evaluated?
    node.value.gsub!(/(^|[^\\])\#\{([^}]*)\}/) do |md|
      $1+Sass::Script.parse($2, node.line, 0, node.options).perform(@environment).to_s
    end
    node.value = run_interp([Sass::Script::String.new(node.value)])
  end
  node
end

#visit_debug(node) (protected)

Prints the expression to STDERR.



66
67
68
69
70
71
72
73
74
75
# File 'lib/sass/tree/visitors/perform.rb', line 66

def visit_debug(node)
  res = node.expr.perform(@environment)
  res = res.value if res.is_a?(Sass::Script::String)
  if node.filename
    $stderr.puts "#{node.filename}:#{node.line} DEBUG: #{res}"
  else
    $stderr.puts "Line #{node.line} DEBUG: #{res}"
  end
  []
end

#visit_directive(node) (protected)



268
269
270
271
272
273
274
# File 'lib/sass/tree/visitors/perform.rb', line 268

def visit_directive(node)
  if node.value['#{']
    node.value = run_interp(Sass::Engine.parse_interp(node.value, node.line, 0, node.options))
  end
  yield
  node
end

#visit_each(node) (protected)

Runs the child nodes once for each value in the list.



78
79
80
81
82
83
84
85
86
87
# File 'lib/sass/tree/visitors/perform.rb', line 78

def visit_each(node)
  list = node.list.perform(@environment)

  with_environment Sass::Environment.new(@environment) do
    list.to_a.map do |v|
      @environment.set_local_var(node.var, v)
      node.children.map {|c| visit(c)}
    end.flatten
  end
end

#visit_extend(node) (protected)

Runs SassScript interpolation in the selector, and then parses the result into a Selector::CommaSequence.



91
92
93
94
95
# File 'lib/sass/tree/visitors/perform.rb', line 91

def visit_extend(node)
  parser = Sass::SCSS::CssParser.new(run_interp(node.selector), node.line)
  node.resolved_selector = parser.parse_selector(node.filename)
  node
end

#visit_for(node) (protected)

Runs the child nodes once for each time through the loop, varying the variable each time.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/sass/tree/visitors/perform.rb', line 98

def visit_for(node)
  from = node.from.perform(@environment)
  to = node.to.perform(@environment)
  from.assert_int!
  to.assert_int!

  to = to.coerce(from.numerator_units, from.denominator_units)
  range = Range.new(from.to_i, to.to_i, node.exclusive)

  with_environment Sass::Environment.new(@environment) do
    range.map do |i|
      @environment.set_local_var(node.var,
        Sass::Script::Number.new(i, from.numerator_units, from.denominator_units))
      node.children.map {|c| visit(c)}
    end.flatten
  end
end

#visit_function(node) (protected)

Loads the function into the environment.



117
118
119
120
121
# File 'lib/sass/tree/visitors/perform.rb', line 117

def visit_function(node)
  @environment.set_function(node.name,
    Sass::Callable.new(node.name, node.args, @environment, node.children))
  []
end

#visit_if(node) (protected)

Runs the child nodes if the conditional expression is true; otherwise, tries the else nodes.



125
126
127
128
129
130
131
132
133
134
# File 'lib/sass/tree/visitors/perform.rb', line 125

def visit_if(node)
  if node.expr.nil? || node.expr.perform(@environment).to_bool
    yield
    node.children
  elsif node.else
    visit(node.else)
  else
    []
  end
end

#visit_import(node) (protected)

Returns a static DirectiveNode if this is importing a CSS file, or parses and includes the imported Sass file.



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/sass/tree/visitors/perform.rb', line 138

def visit_import(node)
  if path = node.css_import?
    return Sass::Tree::DirectiveNode.new("@import url(#{path})")
  end

  @environment.push_frame(:filename => node.filename, :line => node.line)
  root = node.imported_file.to_tree
  node.children = root.children.map {|c| visit(c)}.flatten
  node
rescue Sass::SyntaxError => e
  e.modify_backtrace(:filename => node.imported_file.options[:filename])
  e.add_backtrace(:filename => node.filename, :line => node.line)
  raise e
ensure
  @environment.pop_frame
end

#visit_mixin(node) (protected)

Runs a mixin.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/sass/tree/visitors/perform.rb', line 163

def visit_mixin(node)
  handle_include_loop!(node) if @environment.mixins_in_use.include?(node.name)

  original_env = @environment
  original_env.push_frame(:filename => node.filename, :line => node.line)
  original_env.prepare_frame(:mixin => node.name)
  raise Sass::SyntaxError.new("Undefined mixin '#{node.name}'.") unless mixin = @environment.mixin(node.name)

  passed_args = node.args.dup
  passed_keywords = node.keywords.dup

  raise Sass::SyntaxError.new(<<END.gsub("\n", "")) if mixin.args.size < passed_args.size
Mixin #{node.name} takes #{mixin.args.size} argument#{'s' if mixin.args.size != 1}
 but #{node.args.size} #{node.args.size == 1 ? 'was' : 'were'} passed.
END

  passed_keywords.each do |name, value|
    # TODO: Make this fast
    unless mixin.args.find {|(var, default)| var.underscored_name == name}
      raise Sass::SyntaxError.new("Mixin #{node.name} doesn't have an argument named $#{name}")
    end
  end

  environment = mixin.args.zip(passed_args).
    inject(Sass::Environment.new(mixin.environment)) do |env, ((var, default), value)|
    env.set_local_var(var.name,
      if value
        value.perform(@environment)
      elsif kv = passed_keywords[var.underscored_name]
        kv.perform(@environment)
      elsif default
        default.perform(env)
      end)
    raise Sass::SyntaxError.new("Mixin #{node.name} is missing parameter #{var.inspect}.") unless env.var(var.name)
    env
  end

  with_environment(environment) {node.children = mixin.tree.map {|c| visit(c)}.flatten}
  node
rescue Sass::SyntaxError => e
  if original_env # Don't add backtrace info if this is an @include loop
    e.modify_backtrace(:mixin => node.name, :line => node.line)
    e.add_backtrace(:line => node.line)
  end
  raise e
ensure
  original_env.pop_frame if original_env
end

#visit_mixindef(node) (protected)

Loads a mixin into the environment.



156
157
158
159
160
# File 'lib/sass/tree/visitors/perform.rb', line 156

def visit_mixindef(node)
  @environment.set_mixin(node.name,
    Sass::Callable.new(node.name, node.args, @environment, node.children))
  []
end

#visit_prop(node) (protected)

Runs any SassScript that may be embedded in a property.



213
214
215
216
217
218
# File 'lib/sass/tree/visitors/perform.rb', line 213

def visit_prop(node)
  node.resolved_name = run_interp(node.name)
  val = node.value.perform(@environment)
  node.resolved_value = val.to_s
  yield
end

#visit_return(node) (protected)

Returns the value of the expression.



221
222
223
# File 'lib/sass/tree/visitors/perform.rb', line 221

def visit_return(node)
  throw :_sass_return, node.expr.perform(@environment)
end

#visit_root(node) (protected)

Sets the options on the environment if this is the top-level root.



45
46
47
48
49
50
51
# File 'lib/sass/tree/visitors/perform.rb', line 45

def visit_root(node)
  @environment.options = node.options if @environment.options.nil? || @environment.options.empty?
  yield
rescue Sass::SyntaxError => e
  e.sass_template ||= node.template
  raise e
end

#visit_rule(node) (protected)

Runs SassScript interpolation in the selector, and then parses the result into a Selector::CommaSequence.



227
228
229
230
231
# File 'lib/sass/tree/visitors/perform.rb', line 227

def visit_rule(node)
  parser = Sass::SCSS::StaticParser.new(run_interp(node.rule), node.line)
  node.parsed_rules ||= parser.parse_selector(node.filename)
  yield
end

#visit_variable(node) (protected)

Loads the new variable value into the environment.



234
235
236
237
238
239
# File 'lib/sass/tree/visitors/perform.rb', line 234

def visit_variable(node)
  return [] if node.guarded && [email protected](node.name).nil?
  val = node.expr.perform(@environment)
  @environment.set_var(node.name, val)
  []
end

#visit_warn(node) (protected)

Prints the expression to STDERR with a stylesheet trace.



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/sass/tree/visitors/perform.rb', line 242

def visit_warn(node)
  @environment.push_frame(:filename => node.filename, :line => node.line)
  res = node.expr.perform(@environment)
  res = res.value if res.is_a?(Sass::Script::String)
  msg = "WARNING: #{res}\n"
  @environment.stack.reverse.each_with_index do |entry, i|
    msg << "        #{i == 0 ? "on" : "from"} line #{entry[:line]}" <<
      " of #{entry[:filename] || "an unknown file"}"
    msg << ", in `#{entry[:mixin]}'" if entry[:mixin]
    msg << "\n"
  end
  Sass::Util.sass_warn msg
  []
ensure
  @environment.pop_frame
end

#visit_while(node) (protected)

Runs the child nodes until the continuation expression becomes false.



260
261
262
263
264
265
266
# File 'lib/sass/tree/visitors/perform.rb', line 260

def visit_while(node)
  children = []
  with_environment Sass::Environment.new(@environment) do
    children += node.children.map {|c| visit(c)} while node.expr.perform(@environment).to_bool
  end
  children.flatten
end

#with_environment(env) { ... } ⇒ Object (protected)

Runs a block of code with the current environment replaced with the given one.

Parameters:

Yields:

  • A block in which the environment is set to env.

Returns:

  • (Object)

    The return value of the block.



37
38
39
40
41
42
# File 'lib/sass/tree/visitors/perform.rb', line 37

def with_environment(env)
  old_env, @environment = @environment, env
  yield
ensure
  @environment = old_env
end