Skip to content

Commit

Permalink
lemonade compatibility specs and functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hagenburger authored and chriseppstein committed Nov 28, 2010
1 parent 330b390 commit 47cec81
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
22 changes: 16 additions & 6 deletions lib/compass/sass_extensions/functions/sprites.rb
Expand Up @@ -3,9 +3,8 @@
module Compass::SassExtensions::Functions::Sprites
SASS_NULL = Sass::Script::Number::new(0)

def sprite_image(uri)
uri = uri.value
path, name = Compass::Sprites.path_and_name(uri)
def generate_sprite_image(uri)
path, name = Compass::Sprites.path_and_name(uri.value)
last_spacing = 0
width = 0
height = 0
Expand Down Expand Up @@ -48,12 +47,23 @@ def sprite_image(uri)
end
output_png.save File.join(File.join(Compass.configuration.images_path, "#{path}.png"))

sprite_url(uri)
end

def sprite_image(uri, x_shift = SASS_NULL, y_shift = SASS_NULL)
url = sprite_url(uri)
position = sprite_position(uri, x_shift, y_shift)
Sass::Script::String.new("#{url} #{position}")
end

def sprite_url(uri)
path, name = Compass::Sprites.path_and_name(uri.value)
image_url(Sass::Script::String.new("#{path}.png"))
end

def sprite_position(file, x_shift = SASS_NULL, y_shift = SASS_NULL)
name = File.dirname(file.value)
image_name = File.basename(file.value, '.png')
def sprite_position(uri, x_shift = SASS_NULL, y_shift = SASS_NULL)
name = File.dirname(uri.value)
image_name = File.basename(uri.value, '.png')
image = Compass::Sprites.sprites(name).detect{ |image| image[:name] == image_name }
if x_shift.unit_str == "%"
x = x_shift.to_s
Expand Down
4 changes: 2 additions & 2 deletions lib/compass/sprites.rb
Expand Up @@ -8,7 +8,7 @@ def reset
end

def path_and_name(uri)
if uri =~ %r{((.+/)?(.+))/(\*)\.png}
if uri =~ %r{((.+/)?(.+))/(.+?)\.png}
[$1, $3, $4]
end
end
Expand Down Expand Up @@ -53,7 +53,7 @@ def find(uri, options)
end.join}
\#{$#{name}-sprite-base-class} {
background: sprite-image("#{uri}") no-repeat;
background: generate-sprite-image("#{uri}") no-repeat;
}
@mixin #{name}-sprite-dimensions($sprite) {
Expand Down
51 changes: 51 additions & 0 deletions spec/sprites_spec.rb
Expand Up @@ -345,4 +345,55 @@ def render(scss)
image_md5('squares.png').should == '0187306f3858136feee87d3017e7f307'
end

it "should use the sprite-image and sprite-url function as in lemonade" do
css = render <<-SCSS
@import "squares/*.png";
.squares-1 {
background: sprite-image("squares/20x20.png") no-repeat;
}
.squares-2 {
background: sprite-image("squares/20x20.png", 100%) no-repeat;
}
.squares-3 {
background: sprite-image("squares/20x20.png", -4px, 3px) no-repeat;
}
.squares-4 {
background-image: sprite-url("squares/20x20.png");
}
.squares-5 {
background-image: sprite-url("squares/*.png");
}
SCSS
css.should == <<-CSS
.squares-sprite {
background: url('/squares.png') no-repeat;
}
.squares-1 {
background: url('/squares.png') 0 -10px no-repeat;
}
.squares-2 {
background: url('/squares.png') 100% -10px no-repeat;
}
.squares-3 {
background: url('/squares.png') -4px -7px no-repeat;
}
.squares-4 {
background-image: url('/squares.png');
}
.squares-5 {
background-image: url('/squares.png');
}
CSS
end

end

0 comments on commit 47cec81

Please sign in to comment.