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,12 +40,14 @@ $disable-magic-sprite-selectors:false !default;
@mixin sprite-selectors($map, $sprite-name, $full-sprite-name, $offset-x: 0, $offset-y: 0) { @mixin sprite-selectors($map, $sprite-name, $full-sprite-name, $offset-x: 0, $offset-y: 0) {
@each $selector in $sprite-selectors { @each $selector in $sprite-selectors {
@if sprite_has_selector($map, $sprite-name, $selector) { @if sprite_has_selector($map, $sprite-name, $selector) {
@if sprite_has_valid_selector("#{$full-sprite-name}-#{$selector}") {
&:#{$selector}, &.#{$full-sprite-name}_#{$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); @include sprite-background-position($map, "#{$sprite-name}_#{$selector}", $offset-x, $offset-y);
} }
} }
} }
} }
}
// Generates a class for each space separated name in `$sprite-names`. // Generates a class for each space separated name in `$sprite-names`.
// The class will be of the form .<map-name>-<sprite-name>. // The class will be of the form .<map-name>-<sprite-name>.
@ -57,6 +59,7 @@ $disable-magic-sprite-selectors:false !default;
@each $sprite-name in $sprite-names { @each $sprite-name in $sprite-names {
@if sprite_does_not_have_parent($map, $sprite-name) { @if sprite_does_not_have_parent($map, $sprite-name) {
$full-sprite-name: "#{$prefix}-#{$sprite-name}"; $full-sprite-name: "#{$prefix}-#{$sprite-name}";
@if sprite_has_valid_selector($full-sprite-name) {
.#{$full-sprite-name} { .#{$full-sprite-name} {
@if $base-class { @extend #{$base-class}; } @if $base-class { @extend #{$base-class}; }
@include sprite($map, $sprite-name, $dimensions, $offset-x, $offset-y); @include sprite($map, $sprite-name, $dimensions, $offset-x, $offset-y);
@ -64,3 +67,4 @@ $disable-magic-sprite-selectors:false !default;
} }
} }
} }
}

View File

@ -60,9 +60,7 @@ module Compass::SassExtensions::Functions::Sprites
def sprite(map, sprite, offset_x = ZERO, offset_y = ZERO) def sprite(map, sprite, offset_x = ZERO, offset_y = ZERO)
sprite = convert_sprite_name(sprite) sprite = convert_sprite_name(sprite)
verify_map(map) verify_map(map)
unless sprite.is_a?(Sass::Script::String) verify_sprite(sprite)
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
url = sprite_url(map) url = sprite_url(map)
position = sprite_position(map, sprite, offset_x, offset_y) position = sprite_position(map, sprite, offset_x, offset_y)
Sass::Script::List.new([url] + position.value, :space) 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] 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. # Returns a url to the sprite image.
def sprite_url(map) def sprite_url(map)
@ -148,7 +153,7 @@ module Compass::SassExtensions::Functions::Sprites
assert_type offset_y, :Number assert_type offset_y, :Number
sprite = convert_sprite_name(sprite) sprite = convert_sprite_name(sprite)
verify_map(map, "sprite-position") 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.) 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 end
image = map.image_for(sprite.value) image = map.image_for(sprite.value)
@ -194,7 +199,7 @@ protected
end end
def verify_sprite(sprite) 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.) 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
end end

View File

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

View File

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

View File

@ -31,15 +31,6 @@ module Compass
end end
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 def name_and_hash
"#{path}-s#{uniqueness_hash}.png" "#{path}-s#{uniqueness_hash}.png"
end end

View File

@ -87,11 +87,7 @@ module Compass
# Returns an Array of image names without the file extension # Returns an Array of image names without the file extension
def self.sprite_names(uri) def self.sprite_names(uri)
files(uri).collect do |file| files(uri).collect do |file|
filename = File.basename(file, '.png') 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
end end
end end

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

View File

@ -458,6 +458,22 @@ class SpritesTest < Test::Unit::TestCase
CSS CSS
end 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 it "should calculate corret sprite demsions when givin spacing via issue#253" do
css = render <<-SCSS css = render <<-SCSS
$squares-spacing: 10px; $squares-spacing: 10px;