1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

automatic coercion from unitless numbers

This commit is contained in:
Chris Eppstein 2008-09-24 00:43:45 -07:00
parent ae30d64cfb
commit 6d5d56d14a
3 changed files with 11 additions and 6 deletions

View file

@ -104,7 +104,11 @@ module Sass::Constant
end
def operate(other, operation)
if unitless?
self.coerce!(operation, other.numerator_units, other.denominator_units)
else
other.coerce!(operation, numerator_units, denominator_units)
end
v = self.value.send(operation, other.value)
Number.from_value(v, *compute_units(other, operation))
rescue ArgumentError
@ -113,8 +117,10 @@ module Sass::Constant
def coerce!(operation, num_units, den_units)
if [:+, :-].include?(operation)
unless unitless?
@value *= coercion_factor(numerator_units, num_units)
@value /= coercion_factor(denominator_units, den_units)
end
@numerator_units = num_units.dup
@denominator_units = den_units.dup
end

View file

@ -38,7 +38,6 @@ class SassEngineTest < Test::Unit::TestCase
"!a = 1b + 2c" => "Incompatible units: b and c.",
"a\n :b= 1b * 2c" => "Incompatible units: b and c.",
"a\n :b= 1b % 2c" => "Cannot modulo by a number with units: 2c.",
"!a = 1b + 2" => "Incompatible units: b and unitless.",
"!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],

View file

@ -1,4 +1,4 @@
!width = 10em + 20em
!width = 10em + 20
!color = #00ff98
!main_text = #ffa
!num = 10
@ -39,7 +39,7 @@
:num
:num= 5+2
:num-un= 10em + 15em
:num-un2= 10em + 13em
:num-un2= 10 + 13em
:num-neg= 10 + -.13
:str= 100 + px
:col= 13 + #aaa