2008-10-13 09:49:35 -07:00
|
|
|
require 'sass/tree/node'
|
|
|
|
|
|
|
|
module Sass::Tree
|
2009-04-25 02:04:24 -07:00
|
|
|
# A dynamic node representing a Sass `@for` loop.
|
|
|
|
#
|
|
|
|
# @see Sass::Tree
|
2008-10-13 09:49:35 -07:00
|
|
|
class ForNode < Node
|
2009-04-25 02:04:24 -07:00
|
|
|
# @param var [String] The name of the loop variable
|
|
|
|
# @param from [Script::Node] The parse tree for the initial expression
|
|
|
|
# @param to [Script::Node] The parse tree for the final expression
|
|
|
|
# @param exclusive [Boolean] Whether to include `to` in the loop
|
|
|
|
# or stop just before
|
2009-04-21 19:25:18 -07:00
|
|
|
def initialize(var, from, to, exclusive)
|
2008-10-13 09:49:35 -07:00
|
|
|
@var = var
|
|
|
|
@from = from
|
|
|
|
@to = to
|
|
|
|
@exclusive = exclusive
|
2009-04-21 19:25:18 -07:00
|
|
|
super()
|
2008-10-13 09:49:35 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2009-04-25 02:04:24 -07:00
|
|
|
# Runs the child nodes once for each time through the loop,
|
|
|
|
# varying the variable each time.
|
|
|
|
#
|
|
|
|
# @param environment [Sass::Environment] The lexical environment containing
|
|
|
|
# variable and mixin values
|
|
|
|
# @return [Array<Tree::Node>] The resulting static nodes
|
|
|
|
# @see Sass::Tree
|
2008-10-13 09:49:35 -07:00
|
|
|
def _perform(environment)
|
2009-06-28 23:40:47 -07:00
|
|
|
from = @from.perform(environment)
|
|
|
|
to = @to.perform(environment)
|
2009-06-29 01:19:24 -07:00
|
|
|
from.assert_int!
|
|
|
|
to.assert_int!
|
|
|
|
|
|
|
|
to = to.send(:coerce, from.numerator_units, from.denominator_units)
|
2009-06-28 23:40:47 -07:00
|
|
|
range = Range.new(from.to_i, to.to_i, @exclusive)
|
2008-10-13 09:49:35 -07:00
|
|
|
|
|
|
|
children = []
|
2009-04-14 02:13:24 -07:00
|
|
|
environment = Sass::Environment.new(environment)
|
2008-10-15 20:00:28 -07:00
|
|
|
range.each do |i|
|
2009-06-28 23:40:47 -07:00
|
|
|
environment.set_local_var(@var, Sass::Script::Number.new(i, from.numerator_units, from.denominator_units))
|
2008-10-15 20:00:28 -07:00
|
|
|
children += perform_children(environment)
|
|
|
|
end
|
2008-10-13 09:49:35 -07:00
|
|
|
children
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|