diff --git a/lib/compass/sass_extensions/sprites/base.rb b/lib/compass/sass_extensions/sprites/base.rb index 0534d216..2cefa0e9 100644 --- a/lib/compass/sass_extensions/sprites/base.rb +++ b/lib/compass/sass_extensions/sprites/base.rb @@ -70,9 +70,7 @@ module Compass # Calculates the overal image dimensions # collects image sizes and input parameters for each sprite def compute_image_positions! - imgs = @images.sort { |a,b| a.width <=> b.width } - - fitter = ::Compass::SassExtensions::Sprites::RowFitter.new(imgs) + fitter = ::Compass::SassExtensions::Sprites::RowFitter.new(@images) current_y = 0 fitter.fit!.each do |row| diff --git a/lib/compass/sass_extensions/sprites/image.rb b/lib/compass/sass_extensions/sprites/image.rb index 3a3e57cf..ceb88670 100644 --- a/lib/compass/sass_extensions/sprites/image.rb +++ b/lib/compass/sass_extensions/sprites/image.rb @@ -107,7 +107,10 @@ module Compass base.image_for($1) end end - + + def <=>(other) + other.width <=> self.width + end private def dimensions diff --git a/lib/compass/sass_extensions/sprites/row_fitter.rb b/lib/compass/sass_extensions/sprites/row_fitter.rb index b48f3c23..498a7b3d 100644 --- a/lib/compass/sass_extensions/sprites/row_fitter.rb +++ b/lib/compass/sass_extensions/sprites/row_fitter.rb @@ -10,7 +10,7 @@ module Compass def_delegators :rows, :[] def initialize(images) - @images = images + @images = images.sort @rows = [] end diff --git a/test/units/sprites/row_fitter_test.rb b/test/units/sprites/row_fitter_test.rb index d36557b7..089e9368 100644 --- a/test/units/sprites/row_fitter_test.rb +++ b/test/units/sprites/row_fitter_test.rb @@ -14,7 +14,11 @@ class RowFitterTest < Test::Unit::TestCase end def create_images(dims) - dims.collect { |width, height| stub(:width => width, :height => height) } + dims.collect { |width, height| + image = Compass::SassExtensions::Sprites::Image.new('blah', 'blah', {}) + image.stubs(:width => width, :height => height) + image + } end def basic_dims @@ -22,8 +26,8 @@ class RowFitterTest < Test::Unit::TestCase [ 100, 10 ], [ 80, 10 ], [ 50, 10 ], - [ 20, 10 ], - [ 35, 10 ] + [ 35, 10 ], + [ 20, 10 ] ] end @@ -53,30 +57,8 @@ class RowFitterTest < Test::Unit::TestCase assert_equal 3, row_fitter.rows.length assert_equal [ images[0] ], row_fitter[0].images - assert_equal [ images[1], images[3] ], row_fitter[1].images - assert_equal [ images[2], images[4] ], row_fitter[2].images - end - - it 'should use the scan placement algorithm and get images from the front' do - images = create_images( - [ - [ 100, 10 ], - [ 35, 10 ], - [ 80, 10 ], - [ 50, 10 ], - [ 20, 10 ], - ] - ) - - row_fitter(images) - - row_fitter.fit!(:scan) - - assert_equal 3, row_fitter.rows.length - - assert_equal [ images[0] ], row_fitter[0].images - assert_equal [ images[1], images[3] ], row_fitter[1].images - assert_equal [ images[2], images[4] ], row_fitter[2].images + assert_equal [ images[1], images[4] ], row_fitter[1].images + assert_equal [ images[2], images[3] ], row_fitter[2].images end end