Merge pull request #739 from zanker/master

Allow numeric filenames in sprites
This commit is contained in:
Chris Eppstein 2012-03-11 19:22:44 -07:00
commit bb716926ef
8 changed files with 39 additions and 27 deletions

View File

@ -40,8 +40,10 @@ $disable-magic-sprite-selectors:false !default;
@mixin sprite-selectors($map, $sprite-name, $full-sprite-name, $offset-x: 0, $offset-y: 0) {
@each $selector in $sprite-selectors {
@if sprite_has_selector($map, $sprite-name, $selector) {
&:#{$selector}, &.#{$full-sprite-name}_#{$selector}, &.#{$full-sprite-name}-#{$selector} {
@include sprite-background-position($map, "#{$sprite-name}_#{$selector}", $offset-x, $offset-y);
@if sprite_has_valid_selector("#{$full-sprite-name}-#{$selector}") {
&:#{$selector}, &.#{$full-sprite-name}_#{$selector}, &.#{$full-sprite-name}-#{$selector} {
@include sprite-background-position($map, "#{$sprite-name}_#{$selector}", $offset-x, $offset-y);
}
}
}
}
@ -57,9 +59,11 @@ $disable-magic-sprite-selectors:false !default;
@each $sprite-name in $sprite-names {
@if sprite_does_not_have_parent($map, $sprite-name) {
$full-sprite-name: "#{$prefix}-#{$sprite-name}";
.#{$full-sprite-name} {
@if $base-class { @extend #{$base-class}; }
@include sprite($map, $sprite-name, $dimensions, $offset-x, $offset-y);
@if sprite_has_valid_selector($full-sprite-name) {
.#{$full-sprite-name} {
@if $base-class { @extend #{$base-class}; }
@include sprite($map, $sprite-name, $dimensions, $offset-x, $offset-y);
}
}
}
}

View File

@ -60,9 +60,7 @@ module Compass::SassExtensions::Functions::Sprites
def sprite(map, sprite, offset_x = ZERO, offset_y = ZERO)
sprite = convert_sprite_name(sprite)
verify_map(map)
unless sprite.is_a?(Sass::Script::String)
raise Sass::SyntaxError, %Q(The second argument to sprite() must be a sprite name. See http://beta.compass-style.org/help/tutorials/spriting/ for more information.)
end
verify_sprite(sprite)
url = sprite_url(map)
position = sprite_position(map, sprite, offset_x, offset_y)
Sass::Script::List.new([url] + position.value, :space)
@ -115,6 +113,13 @@ module Compass::SassExtensions::Functions::Sprites
Sass::Script::Functions.declare :sprite_has_selector, [:map, :sprite, :selector]
# Determines if the CSS selector is valid
def sprite_has_valid_selector(selector)
unless selector.value =~ /\A#{Sass::SCSS::RX::IDENT}\Z/
raise Sass::SyntaxError, "#{selector} must be a legal css identifier"
end
Sass::Script::Bool.new true
end
# Returns a url to the sprite image.
def sprite_url(map)
@ -148,7 +153,7 @@ module Compass::SassExtensions::Functions::Sprites
assert_type offset_y, :Number
sprite = convert_sprite_name(sprite)
verify_map(map, "sprite-position")
unless sprite && sprite.is_a?(Sass::Script::String)
unless sprite.is_a?(Sass::Script::String) || sprite.is_a?(Sass::Script::Number)
raise Sass::SyntaxError, %Q(The second argument to sprite-position must be a sprite name. See http://beta.compass-style.org/help/tutorials/spriting/ for more information.)
end
image = map.image_for(sprite.value)
@ -194,7 +199,7 @@ protected
end
def verify_sprite(sprite)
unless sprite.is_a?(Sass::Script::String)
unless sprite.is_a?(Sass::Script::String) || sprite.is_a?(Sass::Script::Number)
raise Sass::SyntaxError, %Q(The second argument to sprite() must be a sprite name. See http://beta.compass-style.org/help/tutorials/spriting/ for more information.)
end
end

View File

@ -4,6 +4,7 @@ module Compass
module ImageMethods
# Fetches the Sprite::Image object for the supplied name
def image_for(name)
name = name.to_s
@images.detect { |img| img.name == name}
end

View File

@ -44,7 +44,6 @@ module Compass
@height = nil
@engine = nil
@evaluation_context = context
validate!
compute_image_metadata!
end

View File

@ -30,16 +30,7 @@ module Compass
Image.new(self, relative_file, kwargs)
end
end
# Validates that the sprite_names are valid sass
def validate!
for sprite_name in sprite_names
unless sprite_name =~ /\A#{Sass::SCSS::RX::IDENT}\Z/
raise Sass::SyntaxError, "#{sprite_name} must be a legal css identifier"
end
end
end
def name_and_hash
"#{path}-s#{uniqueness_hash}.png"
end

View File

@ -87,11 +87,7 @@ module Compass
# Returns an Array of image names without the file extension
def self.sprite_names(uri)
files(uri).collect do |file|
filename = File.basename(file, '.png')
unless VAILD_FILE_NAME =~ filename
raise Compass::Error, "Sprite file names must be legal css identifiers. Please rename #{File.basename(file)}"
end
filename
File.basename(file, '.png')
end
end

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

View File

@ -457,7 +457,23 @@ class SpritesTest < Test::Unit::TestCase
}
CSS
end
it "should import sprites with numeric filenames via #738" do
css = render <<-SCSS
@import "numeric/*.png";
@include all-numeric-sprites;
SCSS
assert_correct css, <<-CSS
.numeric-sprite, .numeric-200 {
background: url('/numeric-saa92d65a89.png') no-repeat;
}
.numeric-200 {
background-position: 0 0;
}
CSS
end
it "should calculate corret sprite demsions when givin spacing via issue#253" do
css = render <<-SCSS
$squares-spacing: 10px;