2008-10-13 09:49:35 -07:00
|
|
|
require 'sass/tree/node'
|
|
|
|
|
|
|
|
module Sass::Tree
|
2009-04-25 02:47:35 -07:00
|
|
|
# A dynamic node representing a mixin include.
|
|
|
|
#
|
|
|
|
# @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
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2009-04-25 02:47:35 -07:00
|
|
|
# Runs the mixin.
|
|
|
|
#
|
|
|
|
# @param environment [Sass::Environment] The lexical environment containing
|
|
|
|
# variable and mixin values
|
|
|
|
# @return [Array<Tree::Node>] The resulting static nodes
|
|
|
|
# @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
|
2008-10-13 09:49:35 -07:00
|
|
|
def _perform(environment)
|
2008-10-15 20:00:28 -07:00
|
|
|
raise Sass::SyntaxError.new("Undefined mixin '#{@name}'.", @line) unless mixin = environment.mixin(@name)
|
|
|
|
|
|
|
|
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
|
|
|
|
mixin.tree.map {|c| c.perform(environment)}.flatten
|
2008-10-13 09:49:35 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|