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:
parent
45cb35a541
commit
c2e99a09f5
5 changed files with 27 additions and 5 deletions
|
@ -9,6 +9,8 @@
|
|||
|
||||
* Variables are now allowed as arguments to `url()`.
|
||||
|
||||
* `#{}` interpolation is now allowed within `url()`.
|
||||
|
||||
## 3.0.0.beta.3
|
||||
|
||||
[Tagged on GitHub](http://github.com/nex3/haml/commit/3.0.0.beta.3).
|
||||
|
|
|
@ -5,7 +5,12 @@ module Sass
|
|||
important || super
|
||||
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)
|
||||
[:string, Script::String.new((@scanner[1] || @scanner[2]).gsub(/\\(['"])/, '\1'), :string)]
|
||||
end
|
||||
|
|
|
@ -120,6 +120,8 @@ module Sass
|
|||
[:single, false] => string_re("'", "'"),
|
||||
[:double, 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
|
||||
|
@ -216,8 +218,8 @@ module Sass
|
|||
end
|
||||
|
||||
variable || string(:double, false) || string(:single, false) || number ||
|
||||
color || bool || raw(URI) || raw(UNICODERANGE) || special_fun ||
|
||||
ident_op || ident || op
|
||||
color || bool || string(:uri, false) || raw(UNICODERANGE) ||
|
||||
special_fun || ident_op || ident || op
|
||||
end
|
||||
|
||||
def variable
|
||||
|
@ -243,7 +245,13 @@ module Sass
|
|||
def string(re, open)
|
||||
return unless scan(STRING_REGULAR_EXPRESSIONS[[re, open]])
|
||||
@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
|
||||
|
||||
def number
|
||||
|
|
|
@ -65,7 +65,8 @@ module Sass
|
|||
NAME = /#{NMCHAR}+/
|
||||
NUM = /[0-9]+|[0-9]*.[0-9]+/
|
||||
STRING = /#{STRING1}|#{STRING2}/
|
||||
URL = /((?:[!#%&*-~]|#{NONASCII}|#{ESCAPE})*)/
|
||||
URLCHAR = /[!#%&*-~]|#{NONASCII}|#{ESCAPE}/
|
||||
URL = /(#{URLCHAR}*)/
|
||||
W = /[ \t\r\n\f]*/
|
||||
|
||||
# This is more liberal than the spec's definition,
|
||||
|
|
|
@ -150,6 +150,12 @@ SASS
|
|||
assert_equal "url(foo bar)", resolve("url(foo bar)")
|
||||
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
|
||||
assert_equal("a-b", resolve("$a-b", {}, env("a-b" => Sass::Script::String.new("a-b"))))
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue