diff --git a/lib/compass/configuration.rb b/lib/compass/configuration.rb index 642cb4ac..8714420a 100644 --- a/lib/compass/configuration.rb +++ b/lib/compass/configuration.rb @@ -143,8 +143,9 @@ module Compass end end - def mk_http_path(path) - hp = http_path[0..-2] if http_path[-1..-1] == "/" + def root_relative(path) + hp = http_path || default_http_path + hp = hp[0..-2] if hp[-1..-1] == "/" "#{hp}/#{path}" end diff --git a/lib/compass/sass_extensions/functions.rb b/lib/compass/sass_extensions/functions.rb index 5dd49a88..75c8eb2f 100644 --- a/lib/compass/sass_extensions/functions.rb +++ b/lib/compass/sass_extensions/functions.rb @@ -1,14 +1,14 @@ module Compass::SassExtensions::Functions end -['selectors', 'enumerate', 'image_url', 'display', 'inline_image'].each do |func| +['selectors', 'enumerate', 'urls', 'display', 'inline_image'].each do |func| require File.join(File.dirname(__FILE__), 'functions', func) end module Sass::Script::Functions include Compass::SassExtensions::Functions::Selectors include Compass::SassExtensions::Functions::Enumerate - include Compass::SassExtensions::Functions::ImageUrl + include Compass::SassExtensions::Functions::Urls include Compass::SassExtensions::Functions::Display include Compass::SassExtensions::Functions::InlineImage end diff --git a/lib/compass/sass_extensions/functions/image_url.rb b/lib/compass/sass_extensions/functions/urls.rb similarity index 66% rename from lib/compass/sass_extensions/functions/image_url.rb rename to lib/compass/sass_extensions/functions/urls.rb index 61e3bf56..2a96b6dc 100644 --- a/lib/compass/sass_extensions/functions/image_url.rb +++ b/lib/compass/sass_extensions/functions/urls.rb @@ -1,4 +1,19 @@ -module Compass::SassExtensions::Functions::ImageUrl +module Compass::SassExtensions::Functions::Urls + + def stylesheet_url(path) + # Compute the path to the stylesheet, either root relative or stylesheet relative + # or nil if the http_images_path is not set in the configuration. + http_stylesheets_path = if relative? + compute_relative_path(Compass.configuration.css_dir) + elsif Compass.configuration.http_stylesheets_path + Compass.configuration.http_stylesheets_path + else + Compass.configuration.root_relative(Compass.configuration.css_dir) + end + + url("#{http_stylesheets_path}/#{path}") + end + def image_url(path) path = path.value # get to the string value of the literal. # Short curcuit if they have provided an absolute url. @@ -10,9 +25,11 @@ module Compass::SassExtensions::Functions::ImageUrl # Compute the path to the image, either root relative or stylesheet relative # or nil if the http_images_path is not set in the configuration. http_images_path = if relative? - compute_relative_path - else + compute_relative_path(Compass.configuration.images_dir) + elsif Compass.configuration.http_stylesheets_path Compass.configuration.http_images_path + else + Compass.configuration.root_relative(Compass.configuration.images_dir) end # Compute the real path to the image on the file stystem if the images_dir is set. @@ -37,23 +54,30 @@ module Compass::SassExtensions::Functions::ImageUrl # prepend the asset host if there is one. path = "#{asset_host}#{'/' unless path[0..0] == "/"}#{path}" if asset_host - Sass::Script::String.new("url(#{path})") + url(path) + end + + # Emits a url, taking off any leading "./" + def url(url) + url = url.to_s + url = url[0..1] == "./" ? url[2..-1] : url + Sass::Script::String.new("url('#{url}')") end private def relative? - Compass.configuration.http_images_path == :relative + Compass.configuration.relative_assets? end def absolute_path?(path) path[0..0] == "/" || path[0..3] == "http" end - def compute_relative_path + def compute_relative_path(dir) if (target_css_file = options[:css_filename]) - images_path = File.join(Compass.configuration.project_path, Compass.configuration.images_dir) - Pathname.new(images_path).relative_path_from(Pathname.new(File.dirname(target_css_file))).to_s + path = File.join(Compass.configuration.project_path, dir) + Pathname.new(path).relative_path_from(Pathname.new(File.dirname(target_css_file))).to_s end end diff --git a/test/fixtures/stylesheets/image_urls/css/screen.css b/test/fixtures/stylesheets/image_urls/css/screen.css index 90c4101a..c7de0c65 100644 --- a/test/fixtures/stylesheets/image_urls/css/screen.css +++ b/test/fixtures/stylesheets/image_urls/css/screen.css @@ -1,3 +1,3 @@ -.showgrid { background-image: url(http://assets2.example.com/images/grid.png?busted=true); } +.showgrid { background-image: url('http://assets2.example.com/images/grid.png?busted=true'); } .inlinegrid { background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAUEAYAAACv1qP4AAAABmJLR0T///////8JWPfcAAAACXBIWXMAAABIAAAASABGyWs+AAAAZ0lEQVRYw+3QwQ2AIBAFUTEUwI3+uzN7gDscsIgxEuO8An52J11X73OudfxMraXkzHfO3Y98nQEhA0IGhAwIGRAyIGRAyICQASEDQgaEDAgZEDIgZEDIgJABoZzSGK3tPuN9ERFP7Nw4fg+c5g8V1wAAAABJRU5ErkJggg=='); }