updated importer to validate that sprites are png files

This commit is contained in:
Scott Davis 2011-06-09 16:27:23 -04:00
parent 7ff9ff15d0
commit ab5d6e51d9
4 changed files with 27 additions and 0 deletions

View File

@ -3,6 +3,7 @@ module Compass
attr_accessor :uri, :options attr_accessor :uri, :options
VAILD_FILE_NAME = /\A#{Sass::SCSS::RX::IDENT}\Z/ VAILD_FILE_NAME = /\A#{Sass::SCSS::RX::IDENT}\Z/
SPRITE_IMPORTER_REGEX = %r{((.+/)?([^\*.]+))/(.+?)\.png} SPRITE_IMPORTER_REGEX = %r{((.+/)?([^\*.]+))/(.+?)\.png}
VALID_EXTENSIONS = ['.png']
def self.load(uri, options) def self.load(uri, options)
klass = Compass::SpriteImporter.new klass = Compass::SpriteImporter.new
@ -89,6 +90,14 @@ module Compass
end end
end end
def validate_sprites!
files.each do |file|
unless VALID_EXTENSIONS.include? File.extname(file)
raise Compass::Error, "Invalid sprite extension only: #{VALID_EXTENSIONS.join(',')} images are allowed"
end
end
end
# Returns the sass options for this sprite # Returns the sass options for this sprite
def sass_options def sass_options
@sass_options ||= options.merge(:filename => name, :syntax => :scss, :importer => self) @sass_options ||= options.merge(:filename => name, :syntax => :scss, :importer => self)
@ -96,6 +105,7 @@ module Compass
# Returns a Sass::Engine for this sprite object # Returns a Sass::Engine for this sprite object
def sass_engine def sass_engine
validate_sprites!
Sass::Engine.new(content_for_images, sass_options) Sass::Engine.new(content_for_images, sass_options)
end end

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

View File

@ -46,4 +46,21 @@ class ImporterTest < Test::Unit::TestCase
assert_equal 'bar', @importer.sass_options[:foo] assert_equal 'bar', @importer.sass_options[:foo]
end end
test "should fail givin bad sprite extensions" do
@images_src_path = File.join(File.dirname(__FILE__), '..', '..', 'fixtures', 'sprites', 'public', 'images')
file = StringIO.new("images_path = #{@images_src_path.inspect}\n")
Compass.add_configuration(file, "sprite_config")
importer = Compass::SpriteImporter.new(:uri => 'bad_extensions/*.jpg', :options => options)
begin
importer.sass_engine
assert false, "Somthing happened an invalid sprite file made it past validation"
rescue Compass::Error => e
assert e.message.include?('.png')
end
end
def taredown
Compass.reset_configuration!
end
end end