PageRenderTime 31ms CodeModel.GetById 15ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/scalate-jruby/src/main/resources/haml-3.0.25/lib/sass/tree/mixin_node.rb

http://github.com/scalate/scalate
Ruby | 111 lines | 80 code | 13 blank | 18 comment | 13 complexity | f95bf988cf9450dd009a24b302f29dab MD5 | raw file
  1require 'sass/tree/node'
  2
  3module Sass::Tree
  4  # A static node representing a mixin include.
  5  # When in a static tree, the sole purpose is to wrap exceptions
  6  # to add the mixin to the backtrace.
  7  #
  8  # @see Sass::Tree
  9  class MixinNode < Node
 10    # @see Node#options=
 11    def options=(opts)
 12      super
 13      @args.each {|a| a.context = :equals} if opts[:sass2]
 14    end
 15
 16    # @param name [String] The name of the mixin
 17    # @param args [Array<Script::Node>] The arguments to the mixin
 18    def initialize(name, args)
 19      @name = name
 20      @args = args
 21      super()
 22    end
 23
 24    # @see Node#cssize
 25    def cssize(extends, parent = nil)
 26      _cssize(extends, parent) # Pass on the parent even if it's not a MixinNode
 27    end
 28
 29    protected
 30
 31    # @see Node#to_src
 32    def to_src(tabs, opts, fmt)
 33      args = '(' + @args.map {|a| a.to_sass(opts)}.join(", ") + ')' unless @args.empty?
 34      "#{'  ' * tabs}#{fmt == :sass ? '+' : '@include '}#{dasherize(@name, opts)}#{args}#{semi fmt}\n"
 35    end
 36
 37    # @see Node#_cssize
 38    def _cssize(extends, parent)
 39      children.map do |c|
 40        parent.check_child! c
 41        c.cssize(extends, parent)
 42      end.flatten
 43    rescue Sass::SyntaxError => e
 44      e.modify_backtrace(:mixin => @name, :filename => filename, :line => line)
 45      e.add_backtrace(:filename => filename, :line => line)
 46      raise e
 47    end
 48
 49    # Runs the mixin.
 50    #
 51    # @param environment [Sass::Environment] The lexical environment containing
 52    #   variable and mixin values
 53    # @raise [Sass::SyntaxError] if there is no mixin with the given name
 54    # @raise [Sass::SyntaxError] if an incorrect number of arguments was passed
 55    # @see Sass::Tree
 56    def perform!(environment)
 57      handle_include_loop!(environment) if environment.mixins_in_use.include?(@name)
 58
 59      original_env = environment
 60      original_env.push_frame(:filename => filename, :line => line)
 61      original_env.prepare_frame(:mixin => @name)
 62      raise Sass::SyntaxError.new("Undefined mixin '#{@name}'.") unless mixin = environment.mixin(@name)
 63
 64      raise Sass::SyntaxError.new(<<END.gsub("\n", "")) if mixin.args.size < @args.size
 65Mixin #{@name} takes #{mixin.args.size} argument#{'s' if mixin.args.size != 1}
 66 but #{@args.size} #{@args.size == 1 ? 'was' : 'were'} passed.
 67END
 68      environment = mixin.args.zip(@args).
 69        inject(Sass::Environment.new(mixin.environment)) do |env, ((var, default), value)|
 70        env.set_local_var(var.name,
 71          if value
 72            value.perform(environment)
 73          elsif default
 74            val = default.perform(env)
 75            if default.context == :equals && val.is_a?(Sass::Script::String)
 76              val = Sass::Script::String.new(val.value)
 77            end
 78            val
 79          end)
 80        raise Sass::SyntaxError.new("Mixin #{@name} is missing parameter #{var.inspect}.") unless env.var(var.name)
 81        env
 82      end
 83
 84      self.children = mixin.tree.map {|c| c.perform(environment)}.flatten
 85    rescue Sass::SyntaxError => e
 86      if original_env # Don't add backtrace info if this is an @include loop
 87        e.modify_backtrace(:mixin => @name, :line => @line)
 88        e.add_backtrace(:line => @line)
 89      end
 90      raise e
 91    ensure
 92      original_env.pop_frame if original_env
 93    end
 94
 95    private
 96
 97    def handle_include_loop!(environment)
 98      msg = "An @include loop has been found:"
 99      mixins = environment.stack.map {|s| s[:mixin]}.compact
100      if mixins.size == 2 && mixins[0] == mixins[1]
101        raise Sass::SyntaxError.new("#{msg} #{@name} includes itself")
102      end
103
104      mixins << @name
105      msg << "\n" << Haml::Util.enum_cons(mixins, 2).map do |m1, m2|
106        "    #{m1} includes #{m2}"
107      end.join("\n")
108      raise Sass::SyntaxError.new(msg)
109    end
110  end
111end