Sass numbers have units attached.
git-svn-id: svn://hamptoncatlin.com/haml/trunk@361 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
parent
15ca29614b
commit
87e5f9b392
|
@ -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})/
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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; }
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue