refactored to allow a per sprite cleanup
This commit is contained in:
parent
6cb9bc2662
commit
4321fc0cf5
@ -19,11 +19,11 @@ module Compass::SassExtensions::Functions::Sprites
|
||||
#
|
||||
# The sprite map object will generate the sprite map image, if necessary,
|
||||
# the first time it is converted to a url. Simply constructing it has no side-effects.
|
||||
def sprite_map(glob, kwargs = {})
|
||||
def sprite_map(glob, cleanup, kwargs = {})
|
||||
kwargs.extend VariableReader
|
||||
Compass::SassExtensions::Sprites::Base.from_uri(glob, self, kwargs)
|
||||
Compass::SassExtensions::Sprites::Base.from_uri(glob, self, kwargs, cleanup)
|
||||
end
|
||||
Sass::Script::Functions.declare :sprite_map, [:glob], :var_kwargs => true
|
||||
Sass::Script::Functions.declare :sprite_map, [:glob, :cleanup], :var_kwargs => true
|
||||
|
||||
# Returns the image and background position for use in a single shorthand property:
|
||||
#
|
||||
|
@ -6,13 +6,13 @@ module Compass
|
||||
|
||||
# Initialize a new aprite object from a relative file path
|
||||
# the path is relative to the <tt>images_path</tt> confguration option
|
||||
def self.from_uri(uri, context, kwargs)
|
||||
def self.from_uri(uri, context, kwargs, cleanup = Sass::Script::Bool.new(true))
|
||||
sprite_map = ::Compass::SpriteMap.new(uri.value, {})
|
||||
|
||||
sprites = sprite_map.files.map do |sprite|
|
||||
sprite.gsub(Compass.configuration.images_path+"/", "")
|
||||
end
|
||||
new(sprites, sprite_map, context, kwargs)
|
||||
new(sprites, sprite_map, context, cleanup, kwargs)
|
||||
end
|
||||
|
||||
# Loads the sprite engine
|
||||
@ -24,13 +24,13 @@ module Compass
|
||||
# We should do so only when the packing algorithm changes
|
||||
SPRITE_VERSION = "1"
|
||||
|
||||
attr_accessor :image_names, :path, :name, :options, :map
|
||||
attr_accessor :image_names, :path, :name, :options, :map, :cleanup
|
||||
attr_accessor :images, :width, :height
|
||||
|
||||
|
||||
def initialize(image_names, map, context, options)
|
||||
def initialize(image_names, map, context, cleanup, options)
|
||||
require_engine!
|
||||
@image_names, @path, @name, @options = image_names, map.path, map.name, options
|
||||
@image_names, @path, @name, @options, @cleanup = image_names, map.path, map.name, options, cleanup
|
||||
@images = nil
|
||||
@width = nil
|
||||
@height = nil
|
||||
@ -118,7 +118,7 @@ module Compass
|
||||
# Generate a sprite image if necessary
|
||||
def generate
|
||||
if generation_required?
|
||||
if options["#{@map.name}_clean_up_sprites"]
|
||||
if @cleanup.value
|
||||
cleanup_old_sprites
|
||||
end
|
||||
sprite_data = construct_sprite
|
||||
|
@ -74,9 +74,9 @@ $#{name}-position: 0% !default;
|
||||
$#{name}-spacing: 0 !default;
|
||||
$#{name}-repeat: no-repeat !default;
|
||||
$#{name}-prefix: '' !default;
|
||||
$#{name}-clean-up-sprites: true !default;
|
||||
$#{name}-clean-up: true !default;
|
||||
|
||||
#{skip_overrides ? "$#{name}-sprites: sprite-map(\"#{uri}\");" : generate_overrides }
|
||||
#{skip_overrides ? "$#{name}-sprites: sprite-map(\"#{uri}\", $#{name}-clean-up);" : generate_overrides }
|
||||
|
||||
// All sprites should extend this class
|
||||
// The #{name}-sprite mixin will do so for you.
|
||||
@ -130,7 +130,7 @@ $#{name}-#{sprite_name}-repeat: $#{name}-repeat !default;
|
||||
SCSS
|
||||
end.join
|
||||
|
||||
content += "\n$#{name}-sprites: sprite-map(\"#{uri}\",\n"
|
||||
content += "\n$#{name}-sprites: sprite-map(\"#{uri}\", $#{name}-clean-up,\n"
|
||||
content += sprite_names.map do |sprite_name|
|
||||
%Q{ $#{sprite_name}-position: $#{name}-#{sprite_name}-position,
|
||||
$#{sprite_name}-spacing: $#{name}-#{sprite_name}-spacing,
|
||||
|
@ -24,7 +24,11 @@ class SpritesTest < Test::Unit::TestCase
|
||||
|
||||
|
||||
def map_location(file)
|
||||
Dir.glob(File.join(@images_tmp_path, file)).first
|
||||
map_files(file).first
|
||||
end
|
||||
|
||||
def map_files(glob)
|
||||
Dir.glob(File.join(@images_tmp_path, glob))
|
||||
end
|
||||
|
||||
def image_size(file)
|
||||
@ -255,7 +259,7 @@ class SpritesTest < Test::Unit::TestCase
|
||||
|
||||
it "should use position adjustments in functions" do
|
||||
css = render <<-SCSS
|
||||
$squares: sprite-map("squares/*.png", $position: 100%);
|
||||
$squares: sprite-map("squares/*.png", true, $position: 100%);
|
||||
.squares-sprite {
|
||||
background: $squares no-repeat;
|
||||
}
|
||||
@ -407,7 +411,7 @@ class SpritesTest < Test::Unit::TestCase
|
||||
it "should work even if @import is missing" do
|
||||
css = render <<-SCSS
|
||||
.squares {
|
||||
background: sprite(sprite-map("squares/*.png"), twenty-by-twenty) no-repeat;
|
||||
background: sprite(sprite-map("squares/*.png", true), twenty-by-twenty) no-repeat;
|
||||
}
|
||||
SCSS
|
||||
assert_correct css, <<-CSS
|
||||
@ -547,4 +551,31 @@ class SpritesTest < Test::Unit::TestCase
|
||||
CSS
|
||||
end
|
||||
|
||||
it "should generate a sprite and remove the old file" do
|
||||
FileUtils.touch File.join(@images_tmp_path, "selectors-cc8834Fdd.png")
|
||||
assert_equal 1, map_files('selectors-*.png').size
|
||||
css = render <<-SCSS
|
||||
@import "selectors/*.png";
|
||||
a {
|
||||
$disable-magic-sprite-selectors:true;
|
||||
@include selectors-sprite(ten-by-ten)
|
||||
}
|
||||
SCSS
|
||||
assert_equal 1, map_files('selectors-*.png').size, "File was not removed"
|
||||
end
|
||||
|
||||
it "should generate a sprite and NOT remove the old file" do
|
||||
FileUtils.touch File.join(@images_tmp_path, "selectors-cc8834Ftest.png")
|
||||
assert_equal 1, map_files('selectors-*.png').size
|
||||
css = render <<-SCSS
|
||||
$selectors-clean-up: false;
|
||||
@import "selectors/*.png";
|
||||
a {
|
||||
$disable-magic-sprite-selectors:true;
|
||||
@include selectors-sprite(ten-by-ten)
|
||||
}
|
||||
SCSS
|
||||
assert_equal 2, map_files('selectors-*.png').size, "File was removed"
|
||||
end
|
||||
|
||||
end
|
@ -3,6 +3,7 @@ require 'test_helper'
|
||||
class SpritesBaseTest < Test::Unit::TestCase
|
||||
|
||||
def setup
|
||||
Hash.send(:include, Compass::SassExtensions::Functions::Sprites::VariableReader)
|
||||
@images_src_path = File.join(File.dirname(__FILE__), '..', '..', 'fixtures', 'sprites', 'public', 'images')
|
||||
@images_tmp_path = File.join(File.dirname(__FILE__), '..', '..', 'fixtures', 'sprites', 'public', 'images-tmp')
|
||||
FileUtils.cp_r @images_src_path, @images_tmp_path
|
||||
@ -10,13 +11,13 @@ class SpritesBaseTest < Test::Unit::TestCase
|
||||
config.images_path = @images_tmp_path
|
||||
Compass.add_configuration(config)
|
||||
Compass.configure_sass_plugin!
|
||||
@options = Compass.sass_engine_options.extend Compass::SassExtensions::Functions::Sprites::VariableReader
|
||||
@options = {}
|
||||
setup_map
|
||||
end
|
||||
|
||||
def setup_map
|
||||
@map = Compass::SpriteMap.new("selectors/*.png", @options)
|
||||
@base = Compass::SassExtensions::Sprites::Base.new(@map.sprite_names.map{|n| "selectors/#{n}.png"}, @map, @map.sass_engine, @map.options)
|
||||
@base = Compass::SassExtensions::Sprites::Base.new(@map.sprite_names.map{|n| "selectors/#{n}.png"}, @map, @map.sass_engine, Sass::Script::Bool.new(true), @map.options)
|
||||
end
|
||||
|
||||
def teardown
|
||||
@ -81,7 +82,7 @@ class SpritesBaseTest < Test::Unit::TestCase
|
||||
file_to_remove = File.join(@images_tmp_path, 'selectors', 'ten-by-ten.png')
|
||||
FileUtils.rm file_to_remove
|
||||
assert !File.exists?(file_to_remove), "Failed to remove sprite file"
|
||||
@options["selectors_clean_up_sprites"] = true
|
||||
@options["cleanup"] = true
|
||||
setup_map
|
||||
@base.generate
|
||||
assert !File.exists?(file), "Sprite file did not get removed"
|
||||
|
@ -20,7 +20,7 @@ class SpritesImageTest < Test::Unit::TestCase
|
||||
|
||||
def parent
|
||||
map = Compass::SpriteMap.new("selectors/*.png", options)
|
||||
@parent ||= Compass::SassExtensions::Sprites::Base.new(map.sprite_names.map{|n| "selectors/#{n}.png"}, map, map.sass_engine, map.options)
|
||||
@parent ||= Compass::SassExtensions::Sprites::Base.new(map.sprite_names.map{|n| "selectors/#{n}.png"}, map, map.sass_engine, Sass::Script::Bool.new(true), map.options)
|
||||
end
|
||||
|
||||
let(:options) do
|
||||
|
Loading…
Reference in New Issue
Block a user