1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

[Sass] Allow interpolation in url().

This commit is contained in:
Nathan Weizenbaum 2010-04-17 14:27:40 -07:00
parent 45cb35a541
commit c2e99a09f5
5 changed files with 27 additions and 5 deletions

View file

@ -9,6 +9,8 @@
* Variables are now allowed as arguments to `url()`. * Variables are now allowed as arguments to `url()`.
* `#{}` interpolation is now allowed within `url()`.
## 3.0.0.beta.3 ## 3.0.0.beta.3
[Tagged on GitHub](http://github.com/nex3/haml/commit/3.0.0.beta.3). [Tagged on GitHub](http://github.com/nex3/haml/commit/3.0.0.beta.3).

View file

@ -5,7 +5,12 @@ module Sass
important || super important || super
end end
def string(*args) def string(re, *args)
if re == :uri
return unless uri = scan(URI)
return [:string, Script::String.new(uri)]
end
return unless scan(STRING) return unless scan(STRING)
[:string, Script::String.new((@scanner[1] || @scanner[2]).gsub(/\\(['"])/, '\1'), :string)] [:string, Script::String.new((@scanner[1] || @scanner[2]).gsub(/\\(['"])/, '\1'), :string)]
end end

View file

@ -120,6 +120,8 @@ module Sass
[:single, false] => string_re("'", "'"), [:single, false] => string_re("'", "'"),
[:double, true] => string_re('', '"'), [:double, true] => string_re('', '"'),
[:single, true] => string_re('', "'"), [:single, true] => string_re('', "'"),
[:uri, false] => /url\(#{W}(#{URLCHAR}*?)(#{W}\)|(?=#\{))/,
[:uri, true] => /(#{URLCHAR}*?)(#{W}\)|(?=#\{))/,
} }
# @param str [String, StringScanner] The source text to lex # @param str [String, StringScanner] The source text to lex
@ -216,8 +218,8 @@ module Sass
end end
variable || string(:double, false) || string(:single, false) || number || variable || string(:double, false) || string(:single, false) || number ||
color || bool || raw(URI) || raw(UNICODERANGE) || special_fun || color || bool || string(:uri, false) || raw(UNICODERANGE) ||
ident_op || ident || op special_fun || ident_op || ident || op
end end
def variable def variable
@ -243,7 +245,13 @@ module Sass
def string(re, open) def string(re, open)
return unless scan(STRING_REGULAR_EXPRESSIONS[[re, open]]) return unless scan(STRING_REGULAR_EXPRESSIONS[[re, open]])
@interpolation_stack << re if @scanner[2].empty? # Started an interpolated section @interpolation_stack << re if @scanner[2].empty? # Started an interpolated section
[:string, Script::String.new(@scanner[1].gsub(/\\(['"]|\#\{)/, '\1'), :string)] str =
if re == :uri
Script::String.new("#{'url(' unless open}#{@scanner[1]}#{')' unless @scanner[2].empty?}")
else
Script::String.new(@scanner[1].gsub(/\\(['"]|\#\{)/, '\1'), :string)
end
[:string, str]
end end
def number def number

View file

@ -65,7 +65,8 @@ module Sass
NAME = /#{NMCHAR}+/ NAME = /#{NMCHAR}+/
NUM = /[0-9]+|[0-9]*.[0-9]+/ NUM = /[0-9]+|[0-9]*.[0-9]+/
STRING = /#{STRING1}|#{STRING2}/ STRING = /#{STRING1}|#{STRING2}/
URL = /((?:[!#%&*-~]|#{NONASCII}|#{ESCAPE})*)/ URLCHAR = /[!#%&*-~]|#{NONASCII}|#{ESCAPE}/
URL = /(#{URLCHAR}*)/
W = /[ \t\r\n\f]*/ W = /[ \t\r\n\f]*/
# This is more liberal than the spec's definition, # This is more liberal than the spec's definition,

View file

@ -150,6 +150,12 @@ SASS
assert_equal "url(foo bar)", resolve("url(foo bar)") assert_equal "url(foo bar)", resolve("url(foo bar)")
end end
def test_url_with_interpolation
assert_equal "url(http://sass-lang.com/images/foo-bar)", resolve("url(http://sass-lang.com/images/\#{foo-bar})")
assert_equal 'url("http://sass-lang.com/images/foo-bar")', resolve("url('http://sass-lang.com/images/\#{foo-bar}')")
assert_equal 'url("http://sass-lang.com/images/foo-bar")', resolve('url("http://sass-lang.com/images/#{foo-bar}")')
end
def test_hyphenated_variables def test_hyphenated_variables
assert_equal("a-b", resolve("$a-b", {}, env("a-b" => Sass::Script::String.new("a-b")))) assert_equal("a-b", resolve("$a-b", {}, env("a-b" => Sass::Script::String.new("a-b"))))
end end