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:
parent
ef1d39952c
commit
cb80f1f371
@ -40,11 +40,13 @@ $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) {
|
||||
@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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Generates a class for each space separated name in `$sprite-names`.
|
||||
@ -57,10 +59,12 @@ $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}";
|
||||
@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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -44,7 +44,6 @@ module Compass
|
||||
@height = nil
|
||||
@engine = nil
|
||||
@evaluation_context = context
|
||||
validate!
|
||||
compute_image_metadata!
|
||||
end
|
||||
|
||||
|
@ -31,15 +31,6 @@ module Compass
|
||||
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
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user