Module: Compass::Core::SassExtensions::Functions::Urls::ImageUrl

Defined in:
lib/compass/core/sass_extensions/functions/urls.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/compass/core/sass_extensions/functions/urls.rb', line 105

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

Instance Method Details

#image_url(path, only_path = bool(false), cache_buster = bool(true)) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/compass/core/sass_extensions/functions/urls.rb', line 112

def image_url(path, only_path = bool(false), cache_buster = bool(true))
  path = path.value # get to the string value of the literal.

  if path =~ %r{^#{Regexp.escape(Compass.configuration.http_images_path)}/(.*)}
    # Treat root relative urls (without a protocol) like normal if they start with
    # the images path.
    path = $1
  elsif absolute_path?(path)
    # Short curcuit if they have provided an absolute url.
    return unquoted_string("url(#{path})")
  end

  # 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(Compass.configuration.images_path)
  elsif Compass.configuration.http_images_path
    Compass.configuration.http_images_path
  else
    Compass.configuration.http_root_relative(Compass.configuration.images_dir)
  end

  # Compute the real path to the image on the file stystem if the images_dir is set.
  real_path = if Compass.configuration.images_path
    File.join(Compass.configuration.images_path, path.gsub(/#.*$/,""))
  else
    File.join(Compass.configuration.project_path, path.gsub(/#.*$/,""))
  end

  # prepend the path to the image if there's one
  if http_images_path
    http_images_path = "#{http_images_path}/" unless http_images_path[-1..-1] == "/"
    path = "#{http_images_path}#{path}"
  end

  # Compute the asset host unless in relative mode.
  asset_host = if !relative? && Compass.configuration.asset_host
    Compass.configuration.asset_host.call(path)
  end

  # Compute and append the cache buster if there is one.
  if cache_buster.to_bool
    path, anchor = path.split("#", 2)
    if cache_buster.is_a?(Sass::Script::Value::String)
      path += "#{path["?"] ? "&" : "?"}#{cache_buster.value}"
    else
      path = cache_busted_path(path, real_path)
    end
    path = "#{path}#{"#" if anchor}#{anchor}"
  end

  # prepend the asset host if there is one.
  path = "#{asset_host}#{'/' unless path[0..0] == "/"}#{path}" if asset_host

  if only_path.to_bool
    unquoted_string(clean_path(path))
  else
    clean_url(path)
  end
end