only define the compass url helpers if they are not already defined.

This commit is contained in:
Chris Eppstein 2011-08-27 15:38:38 +08:00
parent f23bf58e8d
commit 210a1d4771
2 changed files with 200 additions and 150 deletions

View File

@ -1,8 +1,27 @@
module Compass::SassExtensions::Functions::Urls
def self.has?(base, instance_method)
Sass::Util.has?(:instance_method, base, instance_method)
end
def self.included(base)
base.send(:include, StylesheetUrl) unless has?(base, :stylesheet_url)
base.send(:include, FontUrl) unless has?(base, :font_url)
base.send(:include, ImageUrl) unless has?(base, :image_url)
base.send(:include, GeneratedImageUrl) unless has?(base, :generated_image_url)
end
module StylesheetUrl
def self.included(base)
if base.respond_to?(:declare)
base.declare :stylesheet_url, [:path]
base.declare :stylesheet_url, [:path, :only_path]
end
end
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_stylesheets_path is not set in the configuration.
# or nil if the http_images_path is not set in the configuration.
http_stylesheets_path = if relative?
compute_relative_path(Compass.configuration.css_path)
elsif Compass.configuration.http_stylesheets_path
@ -18,9 +37,15 @@ module Compass::SassExtensions::Functions::Urls
clean_url(path)
end
end
Sass::Script::Functions.declare :stylesheet_url, [:path]
Sass::Script::Functions.declare :stylesheet_url, [:path, :only_path]
end
module FontUrl
def self.included(base)
if base.respond_to?(:declare)
base.declare :font_url, [:path]
base.declare :font_url, [:path, :only_path]
end
end
def font_url(path, only_path = Sass::Script::Bool.new(false))
path = path.value # get to the string value of the literal.
@ -45,9 +70,16 @@ module Compass::SassExtensions::Functions::Urls
clean_url(path)
end
end
Sass::Script::Functions.declare :font_url, [:path]
Sass::Script::Functions.declare :font_url, [:path, :only_path]
end
module ImageUrl
def self.included(base)
if base.respond_to?(:declare)
base.declare :image_url, [:path]
base.declare :image_url, [:path, :only_path]
base.declare :image_url, [:path, :only_path, :cache_buster]
end
end
def image_url(path, only_path = Sass::Script::Bool.new(false), cache_buster = Sass::Script::Bool.new(true))
path = path.value # get to the string value of the literal.
@ -104,10 +136,15 @@ module Compass::SassExtensions::Functions::Urls
clean_url(path)
end
end
Sass::Script::Functions.declare :image_url, [:path]
Sass::Script::Functions.declare :image_url, [:path, :only_path]
Sass::Script::Functions.declare :image_url, [:path, :only_path, :cache_buster]
end
module GeneratedImageUrl
def self.included(base)
if base.respond_to?(:declare)
base.declare :generated_image_url, [:path]
base.declare :generated_image_url, [:path, :cache_buster]
end
end
def generated_image_url(path, cache_buster = Sass::Script::Bool.new(false))
path = path.value # get to the string value of the literal.
@ -160,8 +197,7 @@ module Compass::SassExtensions::Functions::Urls
clean_url(path)
end
Sass::Script::Functions.declare :generated_image_url, [:path]
Sass::Script::Functions.declare :generated_image_url, [:path, :cache_buster]
end
private

View File

@ -98,6 +98,20 @@ class SassExtensionsTest < Test::Unit::TestCase
assert_equal "true", evaluate("prefixed(-css2, css2-fallback(css3, css2))")
end
%w(stylesheet_url font_url image_url generated_image_url).each do |helper|
class_eval %Q{
def test_#{helper}_helper_defers_to_existing_helper
c = Class.new do
def #{helper}(*args)
:original
end
end
c.send(:include, Compass::SassExtensions::Functions::Urls)
assert_equal :original, c.new.#{helper}("logo.png")
end
}
end
protected
def evaluate(value)
Sass::Script::Parser.parse(value, 0, 0).perform(Sass::Environment.new).to_s