Get rid of the unnecessary distinction between .new and .from_value for constant values.
This commit is contained in:
parent
2919d6eff5
commit
a53b2ed03e
|
@ -2,12 +2,6 @@ require 'sass/constant/literal'
|
|||
|
||||
module Sass::Constant
|
||||
class Bool < Literal # :nodoc:
|
||||
|
||||
def parse(value)
|
||||
first, second, unit = value.scan(Literal::NUMBER)[0]
|
||||
@value = value == 'true' ? true : false
|
||||
end
|
||||
|
||||
def to_s
|
||||
@value.to_s
|
||||
end
|
||||
|
|
|
@ -22,20 +22,13 @@ module Sass::Constant
|
|||
'aqua' => 0x00ffff
|
||||
}
|
||||
|
||||
REGEXP = /\##{"([0-9a-fA-F]{1,2})" * 3}/
|
||||
|
||||
def parse(value)
|
||||
if (value =~ REGEXP)
|
||||
@value = value.scan(REGEXP)[0].map { |num| num.ljust(2, num).to_i(16) }
|
||||
else
|
||||
color = HTML4_COLORS[value.downcase]
|
||||
@value = (0..2).map{ |n| color >> (n << 3) & 0xff }.reverse
|
||||
end
|
||||
def initialize(rgb)
|
||||
super(rgb.map {|c| c.to_i})
|
||||
end
|
||||
|
||||
def plus(other)
|
||||
if other.is_a? Sass::Constant::String
|
||||
Sass::Constant::String.from_value(self.to_s + other.to_s)
|
||||
Sass::Constant::String.new(self.to_s + other.to_s)
|
||||
else
|
||||
piecewise(other, :+)
|
||||
end
|
||||
|
@ -79,12 +72,6 @@ module Sass::Constant
|
|||
end
|
||||
alias_method :inspect, :to_s
|
||||
|
||||
protected
|
||||
|
||||
def self.filter_value(value)
|
||||
value.map { |num| num.to_i }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def piecewise(other, operation)
|
||||
|
@ -99,7 +86,7 @@ module Sass::Constant
|
|||
res = @value[i].send(operation, other_num ? other_val : other_val[i])
|
||||
rgb[i] = [ [res, 255].min, 0 ].max
|
||||
end
|
||||
Color.from_value(rgb)
|
||||
Color.new(rgb)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -18,7 +18,7 @@ module Sass
|
|||
|
||||
def perform
|
||||
unless Functions.public_instance_methods.include?(name) && name !~ /^__/
|
||||
return Constant::String.from_value("#{name}(#{args.join(', ')})")
|
||||
return Constant::String.new("#{name}(#{args.join(', ')})")
|
||||
end
|
||||
|
||||
return Functions.send(name, *args)
|
||||
|
|
|
@ -46,7 +46,7 @@ module Sass::Constant
|
|||
unless value.is_a?(Sass::Constant::Number) && value.unitless?
|
||||
raise ArgumentError.new("Value is not a unitless number")
|
||||
end
|
||||
Sass::Constant::Number.from_value(value.value * 100, '%')
|
||||
Sass::Constant::Number.new(value.value * 100, '%')
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -81,7 +81,7 @@ module Sass
|
|||
return unless @scanner.scan(/(-)?(?:(\d*\.\d+)|(\d+))([a-zA-Z%]+)?/)
|
||||
value = @scanner[2] ? @scanner[2].to_f : @scanner[3].to_i
|
||||
value = -value if @scanner[1]
|
||||
[:number, Constant::Number.from_value(value, Array(@scanner[4]))]
|
||||
[:number, Constant::Number.new(value, Array(@scanner[4]))]
|
||||
end
|
||||
|
||||
COLOR = /\##{"([0-9a-fA-F]{1,2})" * 3}|(#{Color::HTML4_COLORS.keys.join("|")})/
|
||||
|
|
|
@ -9,35 +9,10 @@ require 'sass/constant/color'
|
|||
require 'sass/constant/bool'
|
||||
|
||||
class Sass::Constant::Literal # :nodoc:
|
||||
# The regular expression matching numbers.
|
||||
NUMBER = /^(-?\d*?\.?)(\d+)([^\d\s]*)$/
|
||||
|
||||
html_color_matcher = Sass::Constant::Color::HTML4_COLORS.keys.map { |c| "^#{c}$" }.join '|'
|
||||
|
||||
# The regular expression matching colors.
|
||||
COLOR = /^\# (?: [\da-f]{3} | [\da-f]{6} ) | #{html_color_matcher}/ix
|
||||
|
||||
def self.parse(value)
|
||||
case value
|
||||
when NUMBER
|
||||
Sass::Constant::Number.new(value)
|
||||
when COLOR
|
||||
Sass::Constant::Color.new(value)
|
||||
when "true", "false"
|
||||
Sass::Constant::Bool.new(value)
|
||||
when ::Symbol
|
||||
value
|
||||
else
|
||||
Sass::Constant::String.new(value)
|
||||
end
|
||||
end
|
||||
attr_reader :value
|
||||
|
||||
def initialize(value = nil)
|
||||
if value.is_a?(String)
|
||||
self.parse(value)
|
||||
else
|
||||
@value = value
|
||||
end
|
||||
@value = value
|
||||
end
|
||||
|
||||
def perform
|
||||
|
@ -53,23 +28,23 @@ class Sass::Constant::Literal # :nodoc:
|
|||
end
|
||||
|
||||
def eq(other)
|
||||
Sass::Constant::Bool.from_value(self.class == other.class && self.value == other.value)
|
||||
Sass::Constant::Bool.new(self.class == other.class && self.value == other.value)
|
||||
end
|
||||
|
||||
def neq(other)
|
||||
Sass::Constant::Bool.from_value(!eq(other).to_bool)
|
||||
Sass::Constant::Bool.new(!eq(other).to_bool)
|
||||
end
|
||||
|
||||
def unary_not
|
||||
Sass::Constant::Bool.from_value(!to_bool)
|
||||
Sass::Constant::Bool.new(!to_bool)
|
||||
end
|
||||
|
||||
def concat(other)
|
||||
Sass::Constant::String.from_value("#{self.to_s} #{other.to_s}")
|
||||
Sass::Constant::String.new("#{self.to_s} #{other.to_s}")
|
||||
end
|
||||
|
||||
def comma(other)
|
||||
Sass::Constant::String.from_value("#{self.to_s}, #{other.to_s}")
|
||||
Sass::Constant::String.new("#{self.to_s}, #{other.to_s}")
|
||||
end
|
||||
|
||||
def inspect
|
||||
|
@ -83,18 +58,4 @@ class Sass::Constant::Literal # :nodoc:
|
|||
def to_i
|
||||
raise SyntaxError.new("#{value.dump} is not an integer.")
|
||||
end
|
||||
|
||||
attr_reader :value
|
||||
|
||||
protected
|
||||
|
||||
def self.filter_value(value)
|
||||
value
|
||||
end
|
||||
|
||||
def self.from_value(value)
|
||||
instance = self.new
|
||||
instance.instance_variable_set('@value', self.filter_value(value))
|
||||
instance
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,11 +7,11 @@ module Sass::Constant
|
|||
|
||||
PRECISION = 1000.0
|
||||
|
||||
def parse(value)
|
||||
first, second, unit = value.scan(Literal::NUMBER)[0]
|
||||
@value = first.empty? ? second.to_i : "#{first}#{second}".to_f
|
||||
@numerator_units = Array(unit)
|
||||
@denominator_units = []
|
||||
def initialize(value, numerator_units = [], denominator_units = [])
|
||||
super(value)
|
||||
@numerator_units = numerator_units
|
||||
@denominator_units = denominator_units
|
||||
normalize!
|
||||
end
|
||||
|
||||
def plus(other)
|
||||
|
@ -20,7 +20,7 @@ module Sass::Constant
|
|||
elsif other.is_a?(Color)
|
||||
other.plus(self)
|
||||
else
|
||||
Sass::Constant::String.from_value(self.to_s + other.to_s)
|
||||
Sass::Constant::String.new(self.to_s + other.to_s)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -33,7 +33,7 @@ module Sass::Constant
|
|||
end
|
||||
|
||||
def unary_minus
|
||||
Number.from_value(-value, numerator_units, denominator_units)
|
||||
Number.new(-value, numerator_units, denominator_units)
|
||||
end
|
||||
|
||||
def times(other)
|
||||
|
@ -66,7 +66,7 @@ module Sass::Constant
|
|||
end
|
||||
|
||||
def eq(other)
|
||||
Sass::Constant::Bool.from_value(super.to_bool &&
|
||||
Sass::Constant::Bool.new(super.to_bool &&
|
||||
self.numerator_units.sort == other.numerator_units.sort &&
|
||||
self.denominator_units.sort == other.denominator_units.sort)
|
||||
end
|
||||
|
@ -120,14 +120,6 @@ module Sass::Constant
|
|||
|
||||
protected
|
||||
|
||||
def self.from_value(value, numerator_units = [], denominator_units = [])
|
||||
instance = super(value)
|
||||
instance.instance_variable_set('@numerator_units', numerator_units)
|
||||
instance.instance_variable_set('@denominator_units', denominator_units)
|
||||
instance.send :normalize!
|
||||
instance
|
||||
end
|
||||
|
||||
def operate(other, operation)
|
||||
this = self
|
||||
if [:+, :-].include?(operation)
|
||||
|
@ -140,14 +132,14 @@ module Sass::Constant
|
|||
|
||||
result = this.value.send(operation, other.value)
|
||||
if result.is_a?(Numeric)
|
||||
Number.from_value(result, *compute_units(this, other, operation))
|
||||
Number.new(result, *compute_units(this, other, operation))
|
||||
else # Boolean op
|
||||
Bool.from_value(result)
|
||||
Bool.new(result)
|
||||
end
|
||||
end
|
||||
|
||||
def coerce(num_units, den_units)
|
||||
Number.from_value(if unitless?
|
||||
Number.new(if unitless?
|
||||
self.value
|
||||
else
|
||||
self.value * coercion_factor(self.numerator_units, num_units) /
|
||||
|
|
|
@ -2,33 +2,28 @@ require 'sass/constant/literal'
|
|||
|
||||
module Sass::Constant
|
||||
class String < Literal # :nodoc:
|
||||
|
||||
def parse(value)
|
||||
@value = value
|
||||
end
|
||||
|
||||
def plus(other)
|
||||
Sass::Constant::String.from_value(self.to_s + other.to_s)
|
||||
Sass::Constant::String.new(self.to_s + other.to_s)
|
||||
end
|
||||
|
||||
def minus(other)
|
||||
Sass::Constant::String.from_value("#{self.to_s}-#{other.to_s}")
|
||||
Sass::Constant::String.new("#{self.to_s}-#{other.to_s}")
|
||||
end
|
||||
|
||||
def unary_minus
|
||||
Sass::Constant::String.from_value("-#{self.to_s}")
|
||||
Sass::Constant::String.new("-#{self.to_s}")
|
||||
end
|
||||
|
||||
def div(other)
|
||||
Sass::Constant::String.from_value("#{self.to_s}/#{other.to_s}")
|
||||
Sass::Constant::String.new("#{self.to_s}/#{other.to_s}")
|
||||
end
|
||||
|
||||
def unary_div
|
||||
Sass::Constant::String.from_value("/#{self.to_s}")
|
||||
Sass::Constant::String.new("/#{self.to_s}")
|
||||
end
|
||||
|
||||
def funcall(other)
|
||||
Sass::Constant::String.from_value("#{self.to_s}(#{other.to_s})")
|
||||
Sass::Constant::String.new("#{self.to_s}(#{other.to_s})")
|
||||
end
|
||||
|
||||
def to_s
|
||||
|
|
|
@ -83,7 +83,7 @@ module Sass
|
|||
:load_paths => ['.']
|
||||
}.merge! options
|
||||
@template = template
|
||||
@constants = {"important" => Constant::String.from_value("!important")}
|
||||
@constants = {"important" => Constant::String.new("!important")}
|
||||
@mixins = {}
|
||||
end
|
||||
|
||||
|
@ -361,7 +361,7 @@ END
|
|||
tree = []
|
||||
old_constants = @constants.dup
|
||||
for i in range
|
||||
@constants[var[1..-1]] = Constant::Number.from_value(i)
|
||||
@constants[var[1..-1]] = Constant::Number.new(i)
|
||||
append_children(tree, line.children, root)
|
||||
end
|
||||
@constants = old_constants
|
||||
|
|
|
@ -7,45 +7,45 @@ class SassFunctionTest < Test::Unit::TestCase
|
|||
# These tests adapted from the w3c browser tests
|
||||
# http://www.w3.org/Style/CSS/Test/CSS3/Color/20070927/html4/t040204-hsl-h-rotating-b.htm
|
||||
red = [255, 0, 0]
|
||||
assert_rgb_hsl(red, [0, '100%', '50%'])
|
||||
assert_rgb_hsl(red, [-360, '100%', '50%'])
|
||||
assert_rgb_hsl(red, [360, '100%', '50%'])
|
||||
assert_rgb_hsl(red, [6120, '100%', '50%'])
|
||||
assert_rgb_hsl(red, ['0', '100%', '50%'])
|
||||
assert_rgb_hsl(red, ['-360', '100%', '50%'])
|
||||
assert_rgb_hsl(red, ['360', '100%', '50%'])
|
||||
assert_rgb_hsl(red, ['6120', '100%', '50%'])
|
||||
|
||||
yellow = [255, 255, 0]
|
||||
assert_rgb_hsl(yellow, [60, '100%', '50%'])
|
||||
assert_rgb_hsl(yellow, [-300, '100%', '50%'])
|
||||
assert_rgb_hsl(yellow, [420, '100%', '50%'])
|
||||
assert_rgb_hsl(yellow, [-9660, '100%', '50%'])
|
||||
assert_rgb_hsl(yellow, ['60', '100%', '50%'])
|
||||
assert_rgb_hsl(yellow, ['-300', '100%', '50%'])
|
||||
assert_rgb_hsl(yellow, ['420', '100%', '50%'])
|
||||
assert_rgb_hsl(yellow, ['-9660', '100%', '50%'])
|
||||
|
||||
green = [0, 255, 0]
|
||||
assert_rgb_hsl(green, [120, '100%', '50%'])
|
||||
assert_rgb_hsl(green, [-240, '100%', '50%'])
|
||||
assert_rgb_hsl(green, [480, '100%', '50%'])
|
||||
assert_rgb_hsl(green, [99840, '100%', '50%'])
|
||||
assert_rgb_hsl(green, ['120', '100%', '50%'])
|
||||
assert_rgb_hsl(green, ['-240', '100%', '50%'])
|
||||
assert_rgb_hsl(green, ['480', '100%', '50%'])
|
||||
assert_rgb_hsl(green, ['99840', '100%', '50%'])
|
||||
|
||||
cyan = [0, 255, 255]
|
||||
assert_rgb_hsl(cyan, [180, '100%', '50%'])
|
||||
assert_rgb_hsl(cyan, [-180, '100%', '50%'])
|
||||
assert_rgb_hsl(cyan, [540, '100%', '50%'])
|
||||
assert_rgb_hsl(cyan, [-900, '100%', '50%'])
|
||||
assert_rgb_hsl(cyan, ['180', '100%', '50%'])
|
||||
assert_rgb_hsl(cyan, ['-180', '100%', '50%'])
|
||||
assert_rgb_hsl(cyan, ['540', '100%', '50%'])
|
||||
assert_rgb_hsl(cyan, ['-900', '100%', '50%'])
|
||||
|
||||
blue = [0, 0, 255]
|
||||
assert_rgb_hsl(blue, [240, '100%', '50%'])
|
||||
assert_rgb_hsl(blue, [-120, '100%', '50%'])
|
||||
assert_rgb_hsl(blue, [600, '100%', '50%'])
|
||||
assert_rgb_hsl(blue, [-104880, '100%', '50%'])
|
||||
assert_rgb_hsl(blue, ['240', '100%', '50%'])
|
||||
assert_rgb_hsl(blue, ['-120', '100%', '50%'])
|
||||
assert_rgb_hsl(blue, ['600', '100%', '50%'])
|
||||
assert_rgb_hsl(blue, ['-104880', '100%', '50%'])
|
||||
|
||||
purple = [255, 0, 255]
|
||||
assert_rgb_hsl(purple, [300, '100%', '50%'])
|
||||
assert_rgb_hsl(purple, [-60, '100%', '50%'])
|
||||
assert_rgb_hsl(purple, [660, '100%', '50%'])
|
||||
assert_rgb_hsl(purple, [2820, '100%', '50%'])
|
||||
assert_rgb_hsl(purple, ['300', '100%', '50%'])
|
||||
assert_rgb_hsl(purple, ['-60', '100%', '50%'])
|
||||
assert_rgb_hsl(purple, ['660', '100%', '50%'])
|
||||
assert_rgb_hsl(purple, ['2820', '100%', '50%'])
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def assert_rgb_hsl(rgb, hsl)
|
||||
assert_equal(rgb, Sass::Constant::Functions.hsl(*hsl.map(&Sass::Constant::Number.method(:new))).value)
|
||||
assert_equal(rgb, Sass::Constant::Functions.hsl(*hsl.map(&Sass::Constant::Parser.method(:parse))).value)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue