mirror of
https://github.com/haml/haml.git
synced 2022-11-09 12:33:31 -05:00
Merge branch 'chriseppstein/master' (early part)
This commit is contained in:
commit
ad518a4dc1
4 changed files with 80 additions and 3 deletions
|
@ -40,15 +40,49 @@ module Sass::Script
|
|||
hue_to_rgb(m1, m2, h - 1.0/3)].map { |c| (c * 0xff).round })
|
||||
end
|
||||
|
||||
# Converts a unitless number into a percent and multiplies the number by 100.
|
||||
# E.g. percentage(100px / 50px) => 200%
|
||||
# Some may find this more natural than: 1% * 100px / 50px
|
||||
def percentage(value)
|
||||
unless value.is_a?(Sass::Script::Number) && value.unitless?
|
||||
raise ArgumentError.new("Value is not a unitless number")
|
||||
raise ArgumentError.new("#{value} is not a unitless number")
|
||||
end
|
||||
Sass::Script::Number.new(value.value * 100, '%')
|
||||
end
|
||||
|
||||
# Rounds a number to the nearest whole number.
|
||||
def round(value)
|
||||
numeric_transformation(value) {|n| n.round}
|
||||
end
|
||||
|
||||
# Rounds up to the nearest whole number.
|
||||
def ceil(value)
|
||||
numeric_transformation(value) {|n| n.ceil}
|
||||
end
|
||||
|
||||
# Rounds down to the nearest whole number.
|
||||
def floor(value)
|
||||
numeric_transformation(value) {|n| n.floor}
|
||||
end
|
||||
|
||||
# Returns the absolute value of a number.
|
||||
def abs(value)
|
||||
numeric_transformation(value) {|n| n.abs}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# This method implements the pattern of transforming a numeric value into
|
||||
# another numeric value with the same units.
|
||||
# It yields a number to a block to perform the operation and return a number
|
||||
def numeric_transformation(value)
|
||||
unless value.is_a?(Sass::Script::Number)
|
||||
calling_function = caller.first.scan(/`([^']+)'/).first.first
|
||||
raise Sass::SyntaxError.new("#{value} is not a number for `#{calling_function}'")
|
||||
end
|
||||
Sass::Script::Number.new(yield(value.value), value.numerator_units, value.denominator_units)
|
||||
end
|
||||
|
||||
def hue_to_rgb(m1, m2, h)
|
||||
h += 1 if h < 0
|
||||
h -= 1 if h > 1
|
||||
|
|
|
@ -129,8 +129,10 @@ module Sass::Script
|
|||
other = other.coerce(numerator_units, denominator_units)
|
||||
end
|
||||
end
|
||||
# avoid integer division
|
||||
value = (:/ == operation) ? this.value.to_f : this.value
|
||||
result = value.send(operation, other.value)
|
||||
|
||||
result = this.value.send(operation, other.value)
|
||||
if result.is_a?(Numeric)
|
||||
Number.new(result, *compute_units(this, other, operation))
|
||||
else # Boolean op
|
||||
|
|
|
@ -43,9 +43,50 @@ class SassFunctionTest < Test::Unit::TestCase
|
|||
assert_rgb_hsl(purple, ['2820', '100%', '50%'])
|
||||
end
|
||||
|
||||
def test_percentage
|
||||
assert_equal("50%", evaluate("percentage(.5)"))
|
||||
assert_equal("100%", evaluate("percentage(1)"))
|
||||
assert_equal("25%", evaluate("percentage(25px / 100px)"))
|
||||
assert_error_message("25px is not a unitless number for `percentage'", "percentage(25px)")
|
||||
assert_error_message("#cccccc is not a unitless number for `percentage'", "percentage(#ccc)")
|
||||
assert_error_message("string is not a unitless number for `percentage'", "percentage(string)")
|
||||
end
|
||||
|
||||
def test_numeric_transformations
|
||||
assert_equal("5", evaluate("round(4.8)"))
|
||||
assert_equal("5px", evaluate("round(4.8px)"))
|
||||
assert_equal("5px", evaluate("round(5.49px)"))
|
||||
assert_equal("4", evaluate("floor(4.8)"))
|
||||
assert_equal("4px", evaluate("floor(4.8px)"))
|
||||
assert_equal("5", evaluate("ceil(4.1)"))
|
||||
assert_equal("5px", evaluate("ceil(4.8px)"))
|
||||
assert_equal("5", evaluate("abs(-5)"))
|
||||
assert_equal("5px", evaluate("abs(-5px)"))
|
||||
assert_equal("5", evaluate("abs(5)"))
|
||||
assert_equal("5px", evaluate("abs(5px)"))
|
||||
assert_error_message("#cccccc is not a number for `round'", "round(#ccc)")
|
||||
assert_error_message("foo is not a number for `floor'", "floor(foo)")
|
||||
assert_error_message("'a' is not a number for `ceil'", "ceil('a')")
|
||||
assert_error_message("#aaaaaa is not a number for `abs'", "abs(#aaa)")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def assert_rgb_hsl(rgb, hsl)
|
||||
assert_equal(rgb, Sass::Script::Functions.hsl(*hsl.map(&Sass::Script::Parser.method(:parse))).value)
|
||||
end
|
||||
|
||||
def evaluate(value)
|
||||
Sass::Script::Parser.parse(value).perform({}).to_s
|
||||
end
|
||||
|
||||
def assert_error_message(message, value)
|
||||
begin
|
||||
evaluate(value)
|
||||
flunk("Error message expected but not raised: #{message}")
|
||||
rescue Sass::SyntaxError => e
|
||||
assert_equal(message, e.message)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
#times { num-num: 7; num-col: #7496b8; col-num: #092345; col-col: #243648; }
|
||||
|
||||
#div { num-num: 3.333; num-num2: 3; col-num: #092345; col-col: #0b0d0f; comp: 1px; }
|
||||
#div { num-num: 3.333; num-num2: 3.333; col-num: #092345; col-col: #0b0d0f; comp: 1px; }
|
||||
|
||||
#mod { num-num: 2; col-col: #0f0e05; col-num: #020001; }
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue