From ad967f5e6d69378b5d9394a38bd03c74640ad558 Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Thu, 5 Aug 2010 20:55:48 -0700 Subject: [PATCH] [Compass Core] All url helpers now accept a second argument that when true will cause only the path to be emited. --- .../reference/compass/helpers/urls.haml | 15 ++++++-- lib/compass/sass_extensions/functions/urls.rb | 37 ++++++++++++++----- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/doc-src/content/reference/compass/helpers/urls.haml b/doc-src/content/reference/compass/helpers/urls.haml index 69fb818e..d0ffed70 100644 --- a/doc-src/content/reference/compass/helpers/urls.haml +++ b/doc-src/content/reference/compass/helpers/urls.haml @@ -21,24 +21,33 @@ classnames: #stylesheet-url.helper %h3 %a(href="#stylesheet-url") - stylesheet-url($path) + stylesheet-url($path, $only-path: false) .details %p Generates a path to an asset found relative to the project's css directory. + %br + Passing a true value as the second argument will cause the only the path to be returned + instead of a `url()` function #font-url.helper %h3 %a(href="#font-url") - font-url($path) + font-url($path, $only-path: false) .details %p Generates a path to an asset found relative to the project's font directory. + %br + Passing a true value as the second argument will cause the only the path to be returned + instead of a `url()` function #image-url.helper %h3 %a(href="#image-url") - image-url($path) + image-url($path, $only-path: false) .details %p Generates a path to an asset found relative to the project's images directory. + %br + Passing a true value as the second argument will cause the only the path to be returned + instead of a `url()` function diff --git a/lib/compass/sass_extensions/functions/urls.rb b/lib/compass/sass_extensions/functions/urls.rb index 624cb9f0..85754d37 100644 --- a/lib/compass/sass_extensions/functions/urls.rb +++ b/lib/compass/sass_extensions/functions/urls.rb @@ -1,6 +1,6 @@ module Compass::SassExtensions::Functions::Urls - def stylesheet_url(path) + def stylesheet_url(path, only_path = Sass::Script::Bool.new(false)) # 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? @@ -11,10 +11,15 @@ module Compass::SassExtensions::Functions::Urls Compass.configuration.http_root_relative(Compass.configuration.css_dir) end - clean_url("#{http_stylesheets_path}/#{path}") + path = "#{http_stylesheets_path}/#{path}" + if only_path.to_bool + Sass::Script::String.new(clean_path(path)) + else + clean_url(path) + end end - def font_url(path) + def font_url(path, only_path = Sass::Script::Bool.new(false)) path = path.value # get to the string value of the literal. # Short curcuit if they have provided an absolute url. @@ -30,10 +35,16 @@ module Compass::SassExtensions::Functions::Urls Compass.configuration.http_fonts_path end - clean_url("#{http_fonts_path}/#{path}") + path = "#{http_fonts_path}/#{path}" + + if only_path.to_bool + Sass::Script::String.new(clean_path(path)) + else + clean_url(path) + end end - def image_url(path) + def image_url(path, only_path = Sass::Script::Bool.new(false)) path = path.value # get to the string value of the literal. if path =~ %r{^#{Regexp.escape(Compass.configuration.http_images_path)}/(.*)} @@ -79,16 +90,24 @@ module Compass::SassExtensions::Functions::Urls # prepend the asset host if there is one. path = "#{asset_host}#{'/' unless path[0..0] == "/"}#{path}" if asset_host - clean_url(path) + if only_path.to_bool + Sass::Script::String.new(clean_path(path)) + else + clean_url(path) + end end private - # Emits a url, taking off any leading "./" - def clean_url(url) + # Emits a path, taking off any leading "./" + def clean_path(url) url = url.to_s url = url[0..1] == "./" ? url[2..-1] : url - Sass::Script::String.new("url('#{url}')") + end + + # Emits a url, taking off any leading "./" + def clean_url(url) + Sass::Script::String.new("url('#{clean_path(url)}')") end def relative?