2008-10-13 09:49:35 -07:00
|
|
|
require 'sass/tree/node'
|
|
|
|
|
|
|
|
module Sass::Tree
|
2009-11-26 01:48:40 -08:00
|
|
|
# A static node representing a mixin include.
|
|
|
|
# When in a static tree, the sole purpose is to wrap exceptions
|
|
|
|
# to add the mixin to the backtrace.
|
2009-04-25 02:47:35 -07:00
|
|
|
#
|
|
|
|
# @see Sass::Tree
|
2008-10-13 09:49:35 -07:00
|
|
|
class MixinNode < Node
|
2009-04-25 02:47:35 -07:00
|
|
|
# @param name [String] The name of the mixin
|
|
|
|
# @param args [Array<Script::Node>] The arguments to the mixin
|
2009-04-21 19:25:18 -07:00
|
|
|
def initialize(name, args)
|
2008-10-15 20:00:28 -07:00
|
|
|
@name = name
|
2008-10-13 09:49:35 -07:00
|
|
|
@args = args
|
2009-04-21 19:25:18 -07:00
|
|
|
super()
|
2008-10-13 09:49:35 -07:00
|
|
|
end
|
|
|
|
|
2009-11-26 01:48:40 -08:00
|
|
|
# @see Node#cssize
|
2010-02-21 21:29:12 -08:00
|
|
|
def cssize(extends, parent = nil)
|
|
|
|
_cssize(extends, parent) # Pass on the parent even if it's not a MixinNode
|
2009-11-26 01:48:40 -08:00
|
|
|
end
|
|
|
|
|
2010-01-28 17:22:35 -08:00
|
|
|
protected
|
|
|
|
|
|
|
|
def to_src(tabs, opts, fmt)
|
2010-01-26 23:08:52 -08:00
|
|
|
args = '(' + @args.map {|a| a.to_sass}.join(", ") + ')' unless @args.empty?
|
2010-01-28 17:22:35 -08:00
|
|
|
"#{' ' * tabs}#{fmt == :sass ? '+' : '@include '}#{@name}#{args}#{semi fmt}\n"
|
2010-01-26 23:08:52 -08:00
|
|
|
end
|
|
|
|
|
2009-11-26 01:48:40 -08:00
|
|
|
# @see Node#_cssize
|
2010-02-21 21:29:12 -08:00
|
|
|
def _cssize(extends, parent)
|
2010-02-21 21:41:20 -08:00
|
|
|
children.map do |c|
|
|
|
|
parent.check_child! c
|
|
|
|
c.cssize(extends, parent)
|
|
|
|
end.flatten
|
2009-11-26 01:48:40 -08:00
|
|
|
rescue Sass::SyntaxError => e
|
2010-02-21 21:41:20 -08:00
|
|
|
e.modify_backtrace(:mixin => @name, :filename => filename, :line => line)
|
2009-11-26 01:48:40 -08:00
|
|
|
e.add_backtrace(:filename => filename, :line => line)
|
|
|
|
raise e
|
|
|
|
end
|
|
|
|
|
2009-04-25 02:47:35 -07:00
|
|
|
# Runs the mixin.
|
|
|
|
#
|
|
|
|
# @param environment [Sass::Environment] The lexical environment containing
|
|
|
|
# variable and mixin values
|
|
|
|
# @raise [Sass::SyntaxError] if there is no mixin with the given name
|
|
|
|
# @raise [Sass::SyntaxError] if an incorrect number of arguments was passed
|
|
|
|
# @see Sass::Tree
|
2009-11-26 01:48:40 -08:00
|
|
|
def perform!(environment)
|
2009-09-13 14:24:02 -07:00
|
|
|
raise Sass::SyntaxError.new("Undefined mixin '#{@name}'.") unless mixin = environment.mixin(@name)
|
2008-10-15 20:00:28 -07:00
|
|
|
|
|
|
|
raise Sass::SyntaxError.new(<<END.gsub("\n", "")) if mixin.args.size < @args.size
|
|
|
|
Mixin #{@name} takes #{mixin.args.size} argument#{'s' if mixin.args.size != 1}
|
|
|
|
but #{@args.size} #{@args.size == 1 ? 'was' : 'were'} passed.
|
|
|
|
END
|
|
|
|
|
2009-04-25 02:29:43 -07:00
|
|
|
environment = mixin.args.zip(@args).
|
2009-07-19 14:20:55 -07:00
|
|
|
inject(Sass::Environment.new(mixin.environment)) do |env, ((var, default), value)|
|
|
|
|
env.set_local_var(var.name,
|
2008-10-15 20:00:28 -07:00
|
|
|
if value
|
|
|
|
value.perform(environment)
|
2009-04-25 02:29:43 -07:00
|
|
|
elsif default
|
|
|
|
default.perform(env)
|
2008-10-15 20:00:28 -07:00
|
|
|
end)
|
2009-07-19 14:20:55 -07:00
|
|
|
raise Sass::SyntaxError.new("Mixin #{@name} is missing parameter #{var.inspect}.") unless env.var(var.name)
|
2008-10-15 20:00:28 -07:00
|
|
|
env
|
|
|
|
end
|
2009-11-26 01:48:40 -08:00
|
|
|
|
|
|
|
self.children = mixin.tree.map {|c| c.perform(environment)}.flatten
|
2009-11-24 20:31:15 -08:00
|
|
|
rescue Sass::SyntaxError => e
|
|
|
|
e.modify_backtrace(:mixin => @name, :line => @line)
|
|
|
|
e.add_backtrace(:line => @line)
|
|
|
|
raise e
|
2008-10-13 09:49:35 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|