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

http://github.com/scalate/scalate · Ruby · 55 lines · 33 code · 6 blank · 16 comment · 0 complexity · 7d8c4f8b2b294c4e4ae8e4dc06372f76 MD5 · raw file

  1. require 'sass/tree/node'
  2. module Sass::Tree
  3. # A dynamic node representing a Sass `@for` loop.
  4. #
  5. # @see Sass::Tree
  6. class ForNode < Node
  7. # @param var [String] The name of the loop variable
  8. # @param from [Script::Node] The parse tree for the initial expression
  9. # @param to [Script::Node] The parse tree for the final expression
  10. # @param exclusive [Boolean] Whether to include `to` in the loop
  11. # or stop just before
  12. def initialize(var, from, to, exclusive)
  13. @var = var
  14. @from = from
  15. @to = to
  16. @exclusive = exclusive
  17. super()
  18. end
  19. protected
  20. # @see Node#to_src
  21. def to_src(tabs, opts, fmt)
  22. to = @exclusive ? "to" : "through"
  23. "#{' ' * tabs}@for $#{dasherize(@var, opts)} from #{@from.to_sass(opts)} #{to} #{@to.to_sass(opts)}" +
  24. children_to_src(tabs, opts, fmt)
  25. end
  26. # Runs the child nodes once for each time through the loop,
  27. # varying the variable each time.
  28. #
  29. # @param environment [Sass::Environment] The lexical environment containing
  30. # variable and mixin values
  31. # @return [Array<Tree::Node>] The resulting static nodes
  32. # @see Sass::Tree
  33. def _perform(environment)
  34. from = @from.perform(environment)
  35. to = @to.perform(environment)
  36. from.assert_int!
  37. to.assert_int!
  38. to = to.coerce(from.numerator_units, from.denominator_units)
  39. range = Range.new(from.to_i, to.to_i, @exclusive)
  40. children = []
  41. environment = Sass::Environment.new(environment)
  42. range.each do |i|
  43. environment.set_local_var(@var, Sass::Script::Number.new(i, from.numerator_units, from.denominator_units))
  44. children += perform_children(environment)
  45. end
  46. children
  47. end
  48. end
  49. end