diff --git a/lib/sass/constant/literal.rb b/lib/sass/constant/literal.rb index 72958e8f..c4ee849c 100644 --- a/lib/sass/constant/literal.rb +++ b/lib/sass/constant/literal.rb @@ -6,7 +6,7 @@ require 'sass/constant/color' class Sass::Constant::Literal # The regular expression matching numbers. - NUMBER = /^\-?[0-9]*\.?[0-9]+$/ + NUMBER = /^(-?[0-9]*?\.?)([0-9]+)([^0-9]*)$/ # The regular expression matching colors. COLOR = /^\#(#{"[0-9a-fA-F]" * 3}|#{"[0-9a-fA-F]" * 6})/ diff --git a/lib/sass/constant/number.rb b/lib/sass/constant/number.rb index 5b561b7f..4f1b65c3 100644 --- a/lib/sass/constant/number.rb +++ b/lib/sass/constant/number.rb @@ -2,10 +2,13 @@ require 'sass/constant/literal' module Sass::Constant class Number < Literal - + + attr_reader :unit + def parse(value) - value = value.include?('.') ? value.to_f : value.to_i - @value = value + first, second, unit = value.scan(Literal::NUMBER)[0] + @value = first.empty? ? second.to_i : "#{first}#{second}".to_f + @unit = unit unless unit.empty? end def plus(other) @@ -55,13 +58,30 @@ module Sass::Constant def to_s value = @value value = value.to_i if value % 1 == 0.0 - value.to_s + "#{value}#{@unit}" end protected + + def self.from_value(value, unit=nil) + instance = super(value) + instance.instance_variable_set('@unit', unit) + instance + end def operate(other, operation) - Number.from_value(self.value.send(operation, other.value)) + unit = nil + if other.unit.nil? + unit = self.unit + elsif self.unit.nil? + unit = other.unit + elsif other.unit == self.unit + unit = self.unit + else + raise Sass::SyntaxError.new("Incompatible units: #{self.unit} and #{other.unit}") + end + + Number.from_value(self.value.send(operation, other.value), unit) end end end diff --git a/test/sass/engine_test.rb b/test/sass/engine_test.rb index af1bc6c4..1b0c64d2 100644 --- a/test/sass/engine_test.rb +++ b/test/sass/engine_test.rb @@ -32,7 +32,8 @@ class SassEngineTest < Test::Unit::TestCase "a\n\t:b c" => "Illegal Indentation: Only two space characters are allowed as tabulation.", "a\n :b c" => "Illegal Indentation: Only two space characters are allowed as tabulation.", "a\n :b c" => "Illegal Indentation: Only two space characters are allowed as tabulation.", - "a\n :b\n !c = 3" => "Constants may only be declared at the root of a document." + "a\n :b\n !c = 3" => "Constants may only be declared at the root of a document.", + "!a = 1b + 2c" => "Incompatible units: b and c" } def test_basic_render diff --git a/test/sass/results/constants.css b/test/sass/results/constants.css index d6cf3466..5fe3f639 100644 --- a/test/sass/results/constants.css +++ b/test/sass/results/constants.css @@ -1,7 +1,7 @@ -#main { content: Hello!; qstr: Quo"ted"!; hstr: Hyph-en!; width: 30; background-color: #000; color: #ffffaf; con: foo bar 9 hi there boom; } +#main { content: Hello!; qstr: Quo"ted"!; hstr: Hyph-en!; width: 30em; background-color: #000; color: #ffffaf; con: foo bar 9 hi there boom; } #main #sidebar { background-color: #00ff98; num-normal: 10; num-dec: 10.2; num-dec0: 99; num-neg: -10; esc: 10+12; many: 6; order: 7; complex: #4c9db1hi16; } -#plus { num-num: 7; num-num-neg: -3; num-str: 100px; num-col: #bcbcbc; str-str: hi there; str-str2: hi there; str-num: times: 13; col-num: #ff8aaa; col-col: #5f80ff; } +#plus { num-num: 7; num-num-un: 25em; num-num-neg: 9.87; num-str: 100px; num-col: #bcbcbc; str-str: hi there; str-str2: hi there; str-num: times: 13; col-num: #ff8aaa; col-col: #5f80ff; } #minus { num-num: 900; col-num: #f9f9f4; col-col: #000035; } diff --git a/test/sass/templates/constants.sass b/test/sass/templates/constants.sass index 6fdd0161..ad886f7b 100644 --- a/test/sass/templates/constants.sass +++ b/test/sass/templates/constants.sass @@ -1,4 +1,4 @@ -!width = 10 + 20 +!width = 10em + 20 !color = #00ff98 !main_text = #ffa !num = 10 @@ -34,7 +34,8 @@ #plus :num :num= 5+2 - :num-neg= 10 + -13 + :num-un= 10em + 15em + :num-neg= 10 + -.13 :str= 100 + px :col= 13 + #aaa :str