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