Permalink
Browse files

Merge branch 'decrementing-loops'

Conflicts:
	doc-src/SASS_CHANGELOG.md

Closes #923
Closes #691
  • Loading branch information...
2 parents 2dbd633 + 0039f04 commit 64ce1550b14fca410e39902db86d6a8d2257fdf3 @nex3 nex3 committed Jan 8, 2014
Showing with 55 additions and 3 deletions.
  1. +4 −0 doc-src/SASS_CHANGELOG.md
  2. +2 −1 doc-src/SASS_REFERENCE.md
  3. +4 −2 lib/sass/tree/visitors/perform.rb
  4. +45 −0 test/sass/scss/scss_test.rb
@@ -325,6 +325,10 @@ maps instead.
`e`, and `f`; and then `g`, `h`, and `i`. For more information, see
{file:SASS_REFERENCE.md#each-multi-assign the `@each` reference}.
+* `@for` loops can now go downward as well as upward. For example,
+ `@for $var from 5 through 1` will set `$var` to `5`, `4`, `3`, `2`,
+ and `1`. Thanks to [Robin Roestenburg](http://twitter.com/robinroest).
+
* There is a new {Sass::Script::Value::Helpers convenience API} for creating
Sass values from within ruby extensions.
@@ -1961,7 +1961,8 @@ counter variable is used to adjust the output. The directive has two forms:
`@for $var from <start> through <end>` and `@for $var from <start> to <end>`.
Note the difference in the keywords `through` and `to`. `$var` can be any
variable name, like `$i`; `<start>` and `<end>` are SassScript expressions that
-should return integers.
+should return integers. When `<start>` is greater than `<end>` the counter will
+decrement instead of increment.
The `@for` statement sets `$var` to each successive number in the specified
range and each time outputs the nested styles using that value of `$var`. For
@@ -242,12 +242,14 @@ def visit_for(node)
to.assert_int!
to = to.coerce(from.numerator_units, from.denominator_units)
- range = Range.new(from.to_i, to.to_i, node.exclusive)
+ direction = from.to_i > to.to_i ? -1 : 1
+ range = Range.new(direction * from.to_i, direction * to.to_i, node.exclusive)
with_environment Sass::Environment.new(@environment) do
range.map do |i|
@environment.set_local_var(node.var,
- Sass::Script::Value::Number.new(i, from.numerator_units, from.denominator_units))
+ Sass::Script::Value::Number.new(direction * i,
+ from.numerator_units, from.denominator_units))
node.children.map {|c| visit(c)}
end.flatten
end
@@ -163,6 +163,51 @@ def test_for_directive
SCSS
end
+ def test_for_directive_with_same_start_and_end
+ assert_equal <<CSS, render(<<SCSS)
+CSS
+.foo {
+ @for $var from 1 to 1 {a: $var;}
+}
+SCSS
+
+ assert_equal <<CSS, render(<<SCSS)
+.foo {
+ a: 1; }
+CSS
+.foo {
+ @for $var from 1 through 1 {a: $var;}
+}
+SCSS
+ end
+
+ def test_decrementing_estfor_directive
+ assert_equal <<CSS, render(<<SCSS)
+.foo {
+ a: 5;
+ a: 4;
+ a: 3;
+ a: 2;
+ a: 1; }
+CSS
+.foo {
+ @for $var from 5 through 1 {a: $var;}
+}
+SCSS
+
+ assert_equal <<CSS, render(<<SCSS)
+.foo {
+ a: 5;
+ a: 4;
+ a: 3;
+ a: 2; }
+CSS
+.foo {
+ @for $var from 5 to 1 {a: $var;}
+}
+SCSS
+ end
+
def test_if_directive
assert_equal <<CSS, render(<<SCSS)
foo {

0 comments on commit 64ce155

Please sign in to comment.