Refactor the gradient literals.

This commit is contained in:
Chris Eppstein 2011-04-20 22:28:46 -07:00
parent 9cafbc642c
commit f4ac295894

View File

@ -33,82 +33,109 @@ module Compass::SassExtensions::Functions::GradientSupport
end end
end end
class RadialGradient < Sass::Script::Literal module Gradient
attr_accessor :position_or_angle, :shape_and_size, :color_stops
def children def self.included(base)
[color_stops, position_or_angle, shape_and_size].compact base.extend ClassMethods
end end
def initialize(position_or_angle, shape_and_size, color_stops)
unless color_stops.value.size >= 2 module ClassMethods
raise Sass::SyntaxError, "At least two color stops are required for a radial-gradient" def standardized_prefix(prefix)
class_eval %Q{
def to_#{prefix}(options = self.options)
Sass::Script::String.new("-#{prefix}-\#{to_s(options)}")
end
}
end end
self.position_or_angle = position_or_angle
self.shape_and_size = shape_and_size
self.color_stops = color_stops
end end
def inspect def inspect
to_s to_s
end end
def supports?(aspect)
GRADIENT_ASPECTS.include?(aspect)
end
def has_aspect?
true
end
def angle?(value)
value.is_a?(Sass::Script::Number) && value.numerator_units.size == 1 && value.numerator_units.first == "deg" && value.denominator_units.empty?
end
end
class RadialGradient < Sass::Script::Literal
include Gradient
attr_accessor :position, :shape_and_size, :color_stops
def children
[color_stops, position, shape_and_size].compact
end
def initialize(position, shape_and_size, color_stops)
unless color_stops.value.size >= 2
raise Sass::SyntaxError, "At least two color stops are required for a radial-gradient"
end
if angle?(position)
raise Sass::SyntaxError, "CSS no longer allows angles in radial-gradients."
end
self.position = position
self.shape_and_size = shape_and_size
self.color_stops = color_stops
end
def to_s(options = self.options) def to_s(options = self.options)
s = "radial-gradient(" s = "radial-gradient("
s << position_or_angle.to_s(options) << ", " if position_or_angle s << position.to_s(options) << ", " if position
s << shape_and_size.to_s(options) << ", " if shape_and_size s << shape_and_size.to_s(options) << ", " if shape_and_size
s << color_stops.to_s(options) s << color_stops.to_s(options)
s << ")" s << ")"
end end
def supports?(aspect)
if aspect == "svg" standardized_prefix :webkit
if position_or_angle.nil? || is_position(position_or_angle).to_bool || is_position_list(position_or_angle).to_bool standardized_prefix :moz
true standardized_prefix :o
else
Compass::Util.compass_warn("Warning: Angle-based gradients are not yet supported in SVG. Found: #{position_or_angle}")
false
end
else
GRADIENT_ASPECTS.include?(aspect)
end
end
def has_aspect?
true
end
def to_webkit(options = self.options)
Sass::Script::String.new("-webkit-#{to_s(options)}")
end
def to_owg(options = self.options) def to_owg(options = self.options)
args = [ args = [
grad_point(position_or_angle || _center_position), grad_point(position || _center_position),
Sass::Script::String.new("0"), Sass::Script::String.new("0"),
grad_point(position_or_angle || _center_position), grad_point(position || _center_position),
grad_end_position(color_stops, Sass::Script::Bool.new(true)), grad_end_position(color_stops, Sass::Script::Bool.new(true)),
grad_color_stops(color_stops) grad_color_stops(color_stops)
] ]
args.each {|a| a.options = options} args.each {|a| a.options = options}
Sass::Script::String.new("-webkit-gradient(radial, #{args.join(', ')})") Sass::Script::String.new("-webkit-gradient(radial, #{args.join(', ')})")
end end
def to_moz(options = self.options)
Sass::Script::String.new("-moz-#{to_s(options)}")
end
def to_o(options = self.options)
Sass::Script::String.new("-o-#{to_s(options)}")
end
def to_svg(options = self.options) def to_svg(options = self.options)
# XXX Add shape support if possible # XXX Add shape support if possible
radial_svg_gradient(color_stops, position_or_angle || _center_position) radial_svg_gradient(color_stops, position || _center_position)
end end
def to_pie(options = self.options) def to_pie(options = self.options)
Compass::Logger.new.record(:warning, "PIE does not support radial-gradient.") Compass::Logger.new.record(:warning, "PIE does not support radial-gradient.")
Sass::Script::String.new("-pie-radial-gradient(unsupported)") Sass::Script::String.new("-pie-radial-gradient(unsupported)")
end end
def to_css2(options = self.options) def to_css2(options = self.options)
Sass::Script::String.new("") Sass::Script::String.new("")
end end
end end
class LinearGradient < Sass::Script::Literal class LinearGradient < Sass::Script::Literal
include Gradient
attr_accessor :color_stops, :position_or_angle attr_accessor :color_stops, :position_or_angle
def children def children
[color_stops, position_or_angle].compact [color_stops, position_or_angle].compact
end end
def initialize(position_or_angle, color_stops) def initialize(position_or_angle, color_stops)
unless color_stops.value.size >= 2 unless color_stops.value.size >= 2
raise Sass::SyntaxError, "At least two color stops are required for a linear-gradient" raise Sass::SyntaxError, "At least two color stops are required for a linear-gradient"
@ -116,24 +143,18 @@ module Compass::SassExtensions::Functions::GradientSupport
self.position_or_angle = position_or_angle self.position_or_angle = position_or_angle
self.color_stops = color_stops self.color_stops = color_stops
end end
def inspect
to_s
end
def to_s(options = self.options) def to_s(options = self.options)
s = "linear-gradient(" s = "linear-gradient("
s << position_or_angle.to_s(options) << ", " if position_or_angle s << position_or_angle.to_s(options) << ", " if position_or_angle
s << color_stops.to_s(options) s << color_stops.to_s(options)
s << ")" s << ")"
end end
def supports?(aspect)
GRADIENT_ASPECTS.include?(aspect) standardized_prefix :webkit
end standardized_prefix :moz
def has_aspect? standardized_prefix :o
true
end
def to_webkit(options = self.options)
Sass::Script::String.new("-webkit-#{to_s(options)}")
end
# Output the original webkit gradient syntax # Output the original webkit gradient syntax
def to_owg(options = self.options) def to_owg(options = self.options)
args = [] args = []
@ -143,20 +164,17 @@ module Compass::SassExtensions::Functions::GradientSupport
args.each{|a| a.options = options} args.each{|a| a.options = options}
Sass::Script::String.new("-webkit-gradient(linear, #{args.join(', ')})") Sass::Script::String.new("-webkit-gradient(linear, #{args.join(', ')})")
end end
def to_moz(options = self.options)
Sass::Script::String.new("-moz-#{to_s(options)}")
end
def to_o(options = self.options)
Sass::Script::String.new("-o-#{to_s(options)}")
end
def to_svg(options = self.options) def to_svg(options = self.options)
linear_svg_gradient(color_stops, position_or_angle || Sass::Script::String.new("top")) linear_svg_gradient(color_stops, position_or_angle || Sass::Script::String.new("top"))
end end
def to_pie(options = self.options) def to_pie(options = self.options)
# PIE just uses the standard rep, but the property is prefixed so # PIE just uses the standard rep, but the property is prefixed so
# the presence of this attribute helps flag when to render a special rule. # the presence of this attribute helps flag when to render a special rule.
Sass::Script::String.new to_s(options) Sass::Script::String.new to_s(options)
end end
def to_css2(options = self.options) def to_css2(options = self.options)
Sass::Script::String.new("") Sass::Script::String.new("")
end end
@ -437,10 +455,6 @@ module Compass::SassExtensions::Functions::GradientSupport
end end
end end
def angle?
position_or_angle.is_a?(Sass::Script::Number) && position_or_angle.numerator_units == ["deg"] && position_or_angle.denominator_units.empty?
end
def linear_svg(color_stops, x1, y1, x2, y2) def linear_svg(color_stops, x1, y1, x2, y2)
transform = '' transform = ''
if angle? if angle?