Skip to content

Commit

Permalink
Allow modulo arithmetric for numbers with units
Browse files Browse the repository at this point in the history
For example, 24px % 13px will return 11px. Removal of previous exception
that was used when units were present "Cannot modulo by a number with
units: 13px"
  • Loading branch information
icedtoast authored and nex3 committed Jan 8, 2014
1 parent 5054035 commit c9f1cbd
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 8 deletions.
2 changes: 2 additions & 0 deletions doc-src/SASS_CHANGELOG.md
Expand Up @@ -457,6 +457,8 @@ maps instead.

## 3.2.10

* Allow modulo arithmetic for numbers with units thanks to [Isaac Devine](http://www.devinesystems.co.nz)

* Use the Sass logger infrastructure for `@debug` directives.

* When printing a Sass error into a CSS comment, escape `*/` so the comment
Expand Down
8 changes: 2 additions & 6 deletions lib/sass/script/value/number.rb
Expand Up @@ -175,13 +175,9 @@ def div(other)
# @param other [Number] The right-hand side of the operator
# @return [Number] This number modulo the other
# @raise [NoMethodError] if `other` is an invalid type
# @raise [Sass::UnitConversionError] if `other` has any units
# @raise [Sass::UnitConversionError] if `other` has incompatible units
def mod(other)
if other.is_a?(Number)
unless other.unitless?
raise Sass::UnitConversionError.new(
"Cannot modulo by a number with units: #{other.inspect}.")
end
operate(other, :%)
else
raise NoMethodError.new(nil, :mod)
Expand Down Expand Up @@ -388,7 +384,7 @@ def self.round(num)
end
end

OPERATIONS = [:+, :-, :<=, :<, :>, :>=]
OPERATIONS = [:+, :-, :<=, :<, :>, :>=, :%]

def operate(other, operation)
this = self
Expand Down
2 changes: 1 addition & 1 deletion test/sass/engine_test.rb
Expand Up @@ -67,7 +67,7 @@ class SassEngineTest < Test::Unit::TestCase
"$a: 1b <= 2c" => "Incompatible units: 'c' and 'b'.",
"$a: 1b >= 2c" => "Incompatible units: 'c' and 'b'.",
"a\n b: 1b * 2c" => "2b*c isn't a valid CSS value.",
"a\n b: 1b % 2c" => "Cannot modulo by a number with units: 2c.",
"a\n b: 1b % 2c" => "Incompatible units: 'c' and 'b'.",
"$a: 2px + #ccc" => "Cannot add a number with units (2px) to a color (#cccccc).",
"$a: #ccc + 2px" => "Cannot add a number with units (2px) to a color (#cccccc).",
"& a\n :b c" => ["Base-level rules cannot contain the parent-selector-referencing character '&'.", 1],
Expand Down
3 changes: 2 additions & 1 deletion test/sass/results/units.css
Expand Up @@ -8,4 +8,5 @@ b {
pt: -72pt;
inches: 2in;
more-inches: 3.5in;
mixed: 2.04167in; }
mixed: 2.04167in;
mod-with-same-units: 5px; }
1 change: 1 addition & 0 deletions test/sass/templates/units.sass
Expand Up @@ -9,3 +9,4 @@ b
:inches 1in + 2.54cm
:more-inches 1in + ((72pt * 2in) + (36pt * 1in)) / 2.54cm
:mixed (1 + (1em * 6px / 3in)) * 4in / 2em
:mod-with-same-units 16px % 11px

0 comments on commit c9f1cbd

Please sign in to comment.