# A visitor for converting a dynamic Sass tree into a static Sass tree.classSass::Tree::Visitors::Perform<Sass::Tree::Visitors::Base# @param root [Tree::Node] The root node of the tree to visit.# @param environment [Sass::Environment] The lexical environment.# @return [Tree::Node] The resulting tree of static nodes.defself.visit(root,environment=Sass::Environment.new)new(environment).send(:visit,root)endprotecteddefinitialize(env)@environment=envend# If an exception is raised, this add proper metadata to the backtrace.defvisit(node)super(node.dup)rescueSass::SyntaxError=>ee.modify_backtrace(:filename=>node.filename,:line=>node.line)raiseeend# Keeps track of the current environment.defvisit_children(parent)with_environmentSass::Environment.new(@environment)doparent.children=super.flattenparentendend# Runs a block of code with the current environment replaced with the given one.## @param env [Sass::Environment] The new environment for the duration of the block.# @yield A block in which the environment is set to `env`.# @return [Object] The return value of the block.defwith_environment(env)old_env,@environment=@environment,envyieldensure@environment=old_envend# Sets the options on the environment if this is the top-level root.defvisit_root(node)@environment.options=node.optionsif@environment.options.nil?||@environment.options.empty?yieldrescueSass::SyntaxError=>ee.sass_template||=node.templateraiseeend# Removes this node from the tree if it's a silent comment.defvisit_comment(node)return[]ifnode.invisible?ifnode.evaluated?node.value.gsub!(/(^|[^\\])\#\{([^}]*)\}/)do|md|$1+Sass::Script.parse($2,node.line,0,node.options).perform(@environment).to_sendnode.value=run_interp([Sass::Script::String.new(node.value)])endnodeend# Prints the expression to STDERR.defvisit_debug(node)res=node.expr.perform(@environment)res=res.valueifres.is_a?(Sass::Script::String)ifnode.filename$stderr.puts"#{node.filename}:#{node.line} DEBUG: #{res}"else$stderr.puts"Line #{node.line} DEBUG: #{res}"end[]end# Runs the child nodes once for each value in the list.defvisit_each(node)list=node.list.perform(@environment)with_environmentSass::Environment.new(@environment)dolist.to_a.mapdo|v|@environment.set_local_var(node.var,v)node.children.map{|c|visit(c)}end.flattenendend# Runs SassScript interpolation in the selector,# and then parses the result into a {Sass::Selector::CommaSequence}.defvisit_extend(node)parser=Sass::SCSS::CssParser.new(run_interp(node.selector),node.line)node.resolved_selector=parser.parse_selector(node.filename)nodeend# Runs the child nodes once for each time through the loop, varying the variable each time.defvisit_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_environmentSass::Environment.new(@environment)dorange.mapdo|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.flattenendend# Loads the function into the environment.defvisit_function(node)@environment.set_function(node.name,Sass::Callable.new(node.name,node.args,@environment,node.children))[]end# Runs the child nodes if the conditional expression is true;# otherwise, tries the else nodes.defvisit_if(node)ifnode.expr.nil?||node.expr.perform(@environment).to_boolyieldnode.childrenelsifnode.elsevisit(node.else)else[]endend# Returns a static DirectiveNode if this is importing a CSS file,# or parses and includes the imported Sass file.defvisit_import(node)ifpath=node.css_import?returnSass::Tree::DirectiveNode.new("@import url(#{path})")end@environment.push_frame(:filename=>node.filename,:line=>node.line)root=node.imported_file.to_treenode.children=root.children.map{|c|visit(c)}.flattennoderescueSass::SyntaxError=>ee.modify_backtrace(:filename=>node.imported_file.options[:filename])e.add_backtrace(:filename=>node.filename,:line=>node.line)raiseeensure@environment.pop_frameend# Loads a mixin into the environment.defvisit_mixindef(node)@environment.set_mixin(node.name,Sass::Callable.new(node.name,node.args,@environment,node.children))[]end# Runs a mixin.defvisit_mixin(node)handle_include_loop!(node)if@environment.mixins_in_use.include?(node.name)original_env=@environmentoriginal_env.push_frame(:filename=>node.filename,:line=>node.line)original_env.prepare_frame(:mixin=>node.name)raiseSass::SyntaxError.new("Undefined mixin '#{node.name}'.")unlessmixin=@environment.mixin(node.name)passed_args=node.args.duppassed_keywords=node.keywords.dupraiseSass::SyntaxError.new(<<END.gsub("\n",""))ifmixin.args.size<passed_args.size
Mixin #{node.name} takes #{mixin.args.size} argument#{'s'ifmixin.args.size!=1}
but #{node.args.size}#{node.args.size==1?'was':'were'} passed.
ENDpassed_keywords.eachdo|name,value|# TODO: Make this fastunlessmixin.args.find{|(var,default)|var.underscored_name==name}raiseSass::SyntaxError.new("Mixin #{node.name} doesn't have an argument named $#{name}")endendenvironment=mixin.args.zip(passed_args).inject(Sass::Environment.new(mixin.environment))do|env,((var,default),value)|env.set_local_var(var.name,ifvaluevalue.perform(@environment)elsifkv=passed_keywords[var.underscored_name]kv.perform(@environment)elsifdefaultdefault.perform(env)end)raiseSass::SyntaxError.new("Mixin #{node.name} is missing parameter #{var.inspect}.")unlessenv.var(var.name)envendwith_environment(environment){node.children=mixin.tree.map{|c|visit(c)}.flatten}noderescueSass::SyntaxError=>eiforiginal_env# Don't add backtrace info if this is an @include loope.modify_backtrace(:mixin=>node.name,:line=>node.line)e.add_backtrace(:line=>node.line)endraiseeensureoriginal_env.pop_frameiforiginal_envend# Runs any SassScript that may be embedded in a property.defvisit_prop(node)node.resolved_name=run_interp(node.name)val=node.value.perform(@environment)node.resolved_value=val.to_syieldend# Returns the value of the expression.defvisit_return(node)throw:_sass_return,node.expr.perform(@environment)end# Runs SassScript interpolation in the selector,# and then parses the result into a {Sass::Selector::CommaSequence}.defvisit_rule(node)parser=Sass::SCSS::StaticParser.new(run_interp(node.rule),node.line)node.parsed_rules||=parser.parse_selector(node.filename)ifnode.options[:trace_selectors]@environment.push_frame(:filename=>node.filename,:line=>node.line)node.stack_trace=@environment.stack_trace@environment.pop_frameendyieldend# Loads the new variable value into the environment.defvisit_variable(node)return[]ifnode.guarded&&!@environment.var(node.name).nil?val=node.expr.perform(@environment)@environment.set_var(node.name,val)[]end# Prints the expression to STDERR with a stylesheet trace.defvisit_warn(node)@environment.push_frame(:filename=>node.filename,:line=>node.line)res=node.expr.perform(@environment)res=res.valueifres.is_a?(Sass::Script::String)msg="WARNING: #{res}\n "msg<<@environment.stack_trace.join("\n ")msg<<"\n"Sass::Util.sass_warnmsg[]ensure@environment.pop_frameend# Runs the child nodes until the continuation expression becomes false.defvisit_while(node)children=[]with_environmentSass::Environment.new(@environment)dochildren+=node.children.map{|c|visit(c)}whilenode.expr.perform(@environment).to_boolendchildren.flattenenddefvisit_directive(node)ifnode.value['#{']node.value=run_interp(Sass::Engine.parse_interp(node.value,node.line,0,node.options))endyieldnodeendprivatedefrun_interp(text)text.mapdo|r|nextrifr.is_a?(String)val=r.perform(@environment)# Interpolated strings should never render with quotesnextval.valueifval.is_a?(Sass::Script::String)val.to_send.join.stripenddefhandle_include_loop!(node)msg="An @include loop has been found:"mixins=@environment.stack.map{|s|s[:mixin]}.compactifmixins.size==2&&mixins[0]==mixins[1]raiseSass::SyntaxError.new("#{msg}#{node.name} includes itself")endmixins<<node.namemsg<<"\n"<<Sass::Util.enum_cons(mixins,2).mapdo|m1,m2|" #{m1} includes #{m2}"end.join("\n")raiseSass::SyntaxError.new(msg)endend