The image_url function has to be accessed from within the engine for it to work right in all cases.

This commit is contained in:
Chris Eppstein 2010-09-12 17:28:37 -07:00
parent 29d39e808d
commit 9380326186
2 changed files with 18 additions and 52 deletions

View File

@ -1,41 +1,17 @@
module Compass::SassExtensions::Functions::Sprites
include Compass::SassExtensions::Functions::ImageSize
class SpriteInfo < Sass::Script::Literal
class SpriteInfo
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
def initialize(sprite, sprite_item = nil, position_x = nil, position_y_shift = nil)
@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)
@ -47,32 +23,28 @@ module Compass::SassExtensions::Functions::Sprites
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}")
SpriteInfo.new(:url, sprite)
image_url(Sass::Script::String.new(sprite[:file]))
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)
SpriteInfo.new(:position, sprite, sprite_item, position_x, position_y_shift)
info = SpriteInfo.new(sprite, sprite_item, position_x, position_y_shift)
Sass::Script::String.new(info.position)
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)
SpriteInfo.new(:both, sprite, sprite_item, position_x, position_y_shift)
def sprite_image(file, *args)
pos = sprite_position(file, *args)
url = sprite_url(file)
if pos.value == "0 0"
url
else
url.plus(" ").plus(pos)
end
end
alias_method :sprite_img, :sprite_image

View File

@ -3,7 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe Compass::SassExtensions::Functions::Sprites::SpriteInfo do
def sprite_info(*args)
Compass::SassExtensions::Functions::Sprites::SpriteInfo.new(*args).to_s
Compass::SassExtensions::Functions::Sprites::SpriteInfo.new(*args)
end
##
@ -12,14 +12,14 @@ describe Compass::SassExtensions::Functions::Sprites::SpriteInfo do
sprite = { :file => "sprites.png" }
sprite_item = { :y => Sass::Script::Number.new(20, ['px']), :index => 0 }
x = Sass::Script::Number.new(10, ['px'])
sprite_info(:position, sprite, sprite_item, x).should == "10px 0"
sprite_info(sprite, sprite_item, x).position.should == "10px 0"
end
it "should output the position for the second+ sprite" do
sprite = { :file => "sprites.png" }
sprite_item = { :y => Sass::Script::Number.new(20, ['px']), :index => 1 }
x = Sass::Script::Number.new(10, ['px'])
sprite_info(:position, sprite, sprite_item, x).should ==
sprite_info(sprite, sprite_item, x).position.should ==
"10px <%= Compass::Sprites.sprites['sprites.png'][:images][1][:y].unary_minus %>"
end
@ -28,7 +28,7 @@ describe Compass::SassExtensions::Functions::Sprites::SpriteInfo do
sprite_item = { :y => Sass::Script::Number.new(20, ['px']), :index => 1 }
x = Sass::Script::Number.new(10, ['px'])
y_shift = Sass::Script::Number.new(3, ['px'])
sprite_info(:position, sprite, sprite_item, x, y_shift).should ==
sprite_info(sprite, sprite_item, x, y_shift).position.should ==
"10px <%= Compass::Sprites.sprites['sprites.png'][:images][1][:y].unary_minus.plus(Sass::Script::Number.new(3, ['px'])) %>"
end
@ -36,14 +36,8 @@ describe Compass::SassExtensions::Functions::Sprites::SpriteInfo do
sprite = { :file => "sprites.png" }
sprite_item = { :y => Sass::Script::Number.new(20, ['px']), :index => 2 }
x = Sass::Script::Number.new(100, ['%'])
sprite_info(:position, sprite, sprite_item, x).should ==
sprite_info(sprite, sprite_item, x).position.should ==
"100% <%= Compass::Sprites.sprites['sprites.png'][:images][2][:y].unary_minus %>"
end
it "should output the url" do
sprite = { :file => "sprites.png" }
sprite_item = { }
sprite_info(:url, sprite, sprite_item).should == "url('/sprites.png')"
end
end