From 79d6e28cf58c84f96e38f8e7fe5fea58567f70e1 Mon Sep 17 00:00:00 2001 From: Nico Hagenburger Date: Sun, 17 Oct 2010 23:06:54 +0200 Subject: [PATCH] allow images to be repeated --- .../sass_extensions/functions/sprites.rb | 13 ++++++++++- lib/compass/sprites.rb | 2 ++ spec/sprites_spec.rb | 23 +++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/compass/sass_extensions/functions/sprites.rb b/lib/compass/sass_extensions/functions/sprites.rb index 907d8d7f..27dd31fd 100644 --- a/lib/compass/sass_extensions/functions/sprites.rb +++ b/lib/compass/sass_extensions/functions/sprites.rb @@ -27,13 +27,24 @@ module Compass::SassExtensions::Functions::Sprites output_png = ChunkyPNG::Image.new(width, height, ChunkyPNG::Color::TRANSPARENT) images.each do |image| input_png = ChunkyPNG::Image.from_file(image[:file]) + position = environment.var("#{name}-#{image[:name]}-position") if position.unit_str == "%" image[:x] = (width - image[:width]) * (position.value / 100) else image[:x] = position.value end - output_png.replace input_png, image[:x], image[:y] + + repeat = environment.var("#{name}-#{image[:name]}-repeat").to_s + if repeat == "no-repeat" + output_png.replace input_png, image[:x], image[:y] + else + x = image[:x] - (image[:x] / image[:width]).ceil * image[:width] + while x < width do + output_png.replace input_png, x, image[:y] + x += image[:width] + end + end end output_png.save File.join(File.join(Compass.configuration.images_path, "#{path}.png")) diff --git a/lib/compass/sprites.rb b/lib/compass/sprites.rb index b282aba8..24177e31 100644 --- a/lib/compass/sprites.rb +++ b/lib/compass/sprites.rb @@ -42,11 +42,13 @@ module Compass $#{name}-sprite-dimensions: false !default; $#{name}-position: 0% !default; $#{name}-spacing: 0 !default; + $#{name}-repeat: no-repeat !default; #{images.map do |sprite| <<-SCSS $#{name}-#{sprite[:name]}-position: $#{name}-position !default; $#{name}-#{sprite[:name]}-spacing: $#{name}-spacing !default; + $#{name}-#{sprite[:name]}-repeat: $#{name}-repeat !default; SCSS end.join} diff --git a/spec/sprites_spec.rb b/spec/sprites_spec.rb index da6ab20b..ed94226f 100644 --- a/spec/sprites_spec.rb +++ b/spec/sprites_spec.rb @@ -322,4 +322,27 @@ describe Compass::Sprites do image_md5('squares.png').should == 'b61700e6d402d9df5f3820b73479f371' end + it "should repeat the image" do + css = render <<-SCSS + $squares-repeat: repeat; + @import "squares/*.png"; + @include all-squares-sprites; + SCSS + css.should == <<-CSS + .squares-sprite, .squares-10x10, .squares-20x20 { + background: url('/squares.png') no-repeat; + } + + .squares-10x10 { + background-position: 0 0; + } + + .squares-20x20 { + background-position: 0 -10px; + } + CSS + image_size('squares.png').should == [20, 30] + image_md5('squares.png').should == '0187306f3858136feee87d3017e7f307' + end + end \ No newline at end of file