Add support for == and ~= in SassScript.

This commit is contained in:
Nathan Weizenbaum 2008-08-10 14:05:39 -04:00
parent e54639f495
commit f93f3fdca3
5 changed files with 41 additions and 2 deletions

View File

@ -33,6 +33,7 @@ module Sass
?~ => :not,
?& => :single_and,
?| => :single_or,
?= => :single_equals,
CONSTANT_CHAR => :const,
STRING_CHAR => :str,
ESCAPE_CHAR => :esc
@ -137,17 +138,22 @@ module Sass
end
# Time for a unary op!
if ![nil, :open, :close, :const, :single_and, :single_or].include?(symbol) && beginning_of_token
if ![nil, :open, :close, :const, :single_and, :single_or, :single_equals].include?(symbol) && beginning_of_token
beginning_of_token = true
to_return << :unary << symbol
next
end
if (symbol == :single_and || symbol == :single_or) && last == symbol
if [:single_and, :single_or, :single_equals].include?(symbol) && last == symbol
to_return[-1] = symbol.to_s.gsub(/^single_/, '').to_sym
next
end
if symbol == :single_equals && last == :not
to_return[-1] = :not_equals
next
end
# Are we looking at an operator?
if symbol && (symbol != :mod || str.empty?)
str = reset_str.call

View File

@ -53,6 +53,14 @@ class Sass::Constant::Literal # :nodoc:
to_bool ? self : other
end
def equals(other)
Sass::Constant::Bool.from_value(self.class == other.class && self.value == other.value)
end
def not_equals(other)
Sass::Constant::Bool.from_value(!equals(other).to_bool)
end
def unary_not
Sass::Constant::Bool.from_value(!to_bool)
end

View File

@ -59,6 +59,10 @@ module Sass::Constant
end
end
def equals(other)
Sass::Constant::Bool.from_value(super.to_bool && self.unit == other.unit)
end
def to_s
value = @value
value = value.to_i if value % 1 == 0.0

View File

@ -9,6 +9,7 @@ module Sass::Constant
def initialize(operand1, operand2, operator)
raise Sass::SyntaxError.new("SassScript doesn't support a single-& operator.") if operator == :single_and
raise Sass::SyntaxError.new("SassScript doesn't support a single-| operator.") if operator == :single_or
raise Sass::SyntaxError.new("SassScript doesn't support a single-= operator.") if operator == :single_equals
@operand1 = operand1
@operand2 = operand2

View File

@ -434,6 +434,26 @@ a
SASS
end
def test_equals
assert_equal(<<CSS, render(<<SASS))
a {
t1: true;
t2: true;
t3: true;
f1: false;
f2: false;
f3: false; }
CSS
a
t1 = "foo" == foo
t2 = 1 == 1.0
t3 = false ~= true
f1 = foo == bar
f2 = 1em == 1px
f3 = 12 ~= 12
SASS
end
def test_argument_error
assert_raise(Sass::SyntaxError) { render("a\n b = hsl(1)") }
end