Check the full CSS selector to see if it's valid to allow numeric selectors when manually calling sprite methods

This commit is contained in:
Zachary Anker 2012-02-27 01:04:51 -08:00
parent ef1d39952c
commit cb80f1f371
6 changed files with 22 additions and 26 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) { @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) {
&:#{$selector}, &.#{$full-sprite-name}_#{$selector}, &.#{$full-sprite-name}-#{$selector} { @if sprite_has_valid_selector("#{$full-sprite-name}-#{$selector}") {
@include sprite-background-position($map, "#{$sprite-name}_#{$selector}", $offset-x, $offset-y); &:#{$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 { @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}";
.#{$full-sprite-name} { @if sprite_has_valid_selector($full-sprite-name) {
@if $base-class { @extend #{$base-class}; } .#{$full-sprite-name} {
@include sprite($map, $sprite-name, $dimensions, $offset-x, $offset-y); @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) 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

@ -30,16 +30,7 @@ module Compass
Image.new(self, relative_file, kwargs) Image.new(self, relative_file, kwargs)
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