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