diff --git a/lib/compass/sass_extensions/functions.rb b/lib/compass/sass_extensions/functions.rb index e3723bb4..895455ed 100644 --- a/lib/compass/sass_extensions/functions.rb +++ b/lib/compass/sass_extensions/functions.rb @@ -4,7 +4,7 @@ end %w( selectors enumerate urls display inline_image image_size constants gradient_support - font_files lists colors trig + font_files lists colors trig sprites ).each do |func| require "compass/sass_extensions/functions/#{func}" end @@ -22,6 +22,7 @@ module Sass::Script::Functions include Compass::SassExtensions::Functions::Lists include Compass::SassExtensions::Functions::Colors include Compass::SassExtensions::Functions::Trig + include Compass::SassExtensions::Functions::Sprites end # Wierd that this has to be re-included to pick up sub-modules. Ruby bug? diff --git a/lib/compass/sprites/sass_functions.rb b/lib/compass/sass_extensions/functions/sprites.rb similarity index 70% rename from lib/compass/sprites/sass_functions.rb rename to lib/compass/sass_extensions/functions/sprites.rb index e1b819b3..998a048a 100644 --- a/lib/compass/sprites/sass_functions.rb +++ b/lib/compass/sass_extensions/functions/sprites.rb @@ -1,19 +1,78 @@ -module Sass::Script::Functions +module Compass::SassExtensions::Functions::Sprites + + class SpriteInfo < Sass::Script::Literal + attr_reader :sprite + attr_reader :sprite_item + attr_reader :type + + def initialize(type, sprite, sprite_item = nil, position_x = nil, position_y_shift = nil) + super(nil) + @type = type + @sprite = sprite + @sprite_item = sprite_item + @position_x = position_x + @position_y_shift = position_y_shift + end + + def to_s(opts = {}) + case @type + when :position + position + when :url + url + when :both + pos = position + if pos == '0 0' + url + else + "#{url} #{pos}" + end + end + end + + def to_sass + to_s + end + + private + + def position + x = @position_x || 0 + if @sprite_item[:index] == 0 and (@position_y_shift.nil? or @position_y_shift.value == 0) + "#{x.inspect} 0" + else + expression = "Compass::Sprites.sprites['#{@sprite[:file]}'][:images][#{@sprite_item[:index]}][:y].unary_minus" + expression << ".plus(Sass::Script::Number.new(#{@position_y_shift.value}, ['px']))" if @position_y_shift + "#{x.inspect} <%= #{expression} %>" + end + end + + def url + if defined?(Compass) + compass = Class.new.extend(Compass::SassExtensions::Functions::Urls) + compass.image_url(Sass::Script::String.new(@sprite[:file])).to_s + else + "url('/#{@sprite[:file]}')" + end + end + + end + def sprite_url(file) dir, name, basename = extract_names(file) sprite = sprite_for("#{dir}#{name}") - Sass::Script::SpriteInfo.new(:url, sprite) + SpriteInfo.new(:url, sprite) end def sprite_position(file, position_x = nil, position_y_shift = nil, margin_top_or_both = nil, margin_bottom = nil) sprite, sprite_item = sprite_url_and_position(file, position_x, position_y_shift, margin_top_or_both, margin_bottom) - Sass::Script::SpriteInfo.new(:position, sprite, sprite_item, position_x, position_y_shift) + SpriteInfo.new(:position, sprite, sprite_item, position_x, position_y_shift) end def sprite_image(file, position_x = nil, position_y_shift = nil, margin_top_or_both = nil, margin_bottom = nil) sprite, sprite_item = sprite_url_and_position(file, position_x, position_y_shift, margin_top_or_both, margin_bottom) - Sass::Script::SpriteInfo.new(:both, sprite, sprite_item, position_x, position_y_shift) + SpriteInfo.new(:both, sprite, sprite_item, position_x, position_y_shift) end alias_method :sprite_img, :sprite_image @@ -94,7 +153,7 @@ private image[:margin_top] = margin_top if margin_top > image[:margin_top] image[:margin_bottom] = margin_bottom if margin_bottom > image[:margin_bottom] else - width, height = ImageProperties.new(file).size + width, height = Compass::SassExtensions::Functions::ImageSize::ImageProperties.new(file).size x = (position_x and position_x.numerator_units == %w(%)) ? position_x : Sass::Script::Number.new(0) y = sprite[:height] + margin_top y = Sass::Script::Number.new(y, y == 0 ? [] : ['px']) diff --git a/lib/compass/sass_extensions/monkey_patches.rb b/lib/compass/sass_extensions/monkey_patches.rb index f0c04196..b119be9d 100644 --- a/lib/compass/sass_extensions/monkey_patches.rb +++ b/lib/compass/sass_extensions/monkey_patches.rb @@ -1,3 +1,3 @@ -%w(traversal).each do |patch| +%w(traversal sprites).each do |patch| require "compass/sass_extensions/monkey_patches/#{patch}" end diff --git a/lib/compass/sprites/sass_extension.rb b/lib/compass/sass_extensions/monkey_patches/sprites.rb similarity index 100% rename from lib/compass/sprites/sass_extension.rb rename to lib/compass/sass_extensions/monkey_patches/sprites.rb diff --git a/lib/compass/sprites.rb b/lib/compass/sprites.rb index 6db3135f..b95b5b73 100644 --- a/lib/compass/sprites.rb +++ b/lib/compass/sprites.rb @@ -1,5 +1,4 @@ require 'chunky_png' -require 'compass/sprites/sprite_info' module Compass::Sprites @@sprites = {} @@ -42,13 +41,6 @@ module Compass::Sprites end end - def extend_sass! - require 'sass' - require 'sass/plugin' - require 'compass/sprites/sass_functions' - require 'compass/sprites/sass_extension' - end - def sprite_changed?(sprite_name, sprite) existing_sprite_info = YAML.load(File.read(sprite_info_file(sprite_name))) existing_sprite_info[:sprite] != sprite or existing_sprite_info[:timestamps] != timestamps(sprite) @@ -114,14 +106,3 @@ module Compass::Sprites end end - - -if defined?(ActiveSupport) and Sass::Util.has?(:public_method, ActiveSupport, :on_load) - # Rails 3.0 - ActiveSupport.on_load :before_initialize do - Compass::Sprites.extend_sass! - end -else - Compass::Sprites.extend_sass! -end - diff --git a/lib/compass/sprites/sprite_info.rb b/lib/compass/sprites/sprite_info.rb deleted file mode 100644 index 76d32517..00000000 --- a/lib/compass/sprites/sprite_info.rb +++ /dev/null @@ -1,63 +0,0 @@ -require 'sass/script/literal' - -module Sass::Script - - class SpriteInfo < Literal - attr_reader :sprite - attr_reader :sprite_item - attr_reader :type - - def initialize(type, sprite, sprite_item = nil, position_x = nil, position_y_shift = nil) - super(nil) - @type = type - @sprite = sprite - @sprite_item = sprite_item - @position_x = position_x - @position_y_shift = position_y_shift - end - - def to_s(opts = {}) - case @type - when :position - position - when :url - url - when :both - pos = position - if pos == '0 0' - url - else - "#{url} #{pos}" - end - end - end - - def to_sass - to_s - end - - private - - def position - x = @position_x || 0 - if @sprite_item[:index] == 0 and (@position_y_shift.nil? or @position_y_shift.value == 0) - "#{x.inspect} 0" - else - expression = "Compass::Sprites.sprites['#{@sprite[:file]}'][:images][#{@sprite_item[:index]}][:y].unary_minus" - expression << ".plus(Sass::Script::Number.new(#{@position_y_shift.value}, ['px']))" if @position_y_shift - "#{x.inspect} <%= #{expression} %>" - end - end - - def url - if defined?(Compass) - compass = Class.new.extend(Compass::SassExtensions::Functions::Urls) - compass.image_url(Sass::Script::String.new(@sprite[:file])).to_s - else - "url('/#{@sprite[:file]}')" - end - end - - end - -end diff --git a/spec/sprite_info_spec.rb b/spec/sprite_info_spec.rb index 726b60b5..d3d26bf9 100644 --- a/spec/sprite_info_spec.rb +++ b/spec/sprite_info_spec.rb @@ -1,9 +1,9 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper') -describe Sass::Script::SpriteInfo do +describe Compass::SassExtensions::Functions::Sprites::SpriteInfo do def sprite_info(*args) - Sass::Script::SpriteInfo.new(*args).to_s + Compass::SassExtensions::Functions::Sprites::SpriteInfo.new(*args).to_s end ##