Class: WebResourceBundler::CssUrlRewriter

Inherits:
Object
  • Object
show all
Defined in:
lib/web_resource_bundler/content_management/css_url_rewriter.rb

Constant Summary collapse

URL_TAG_PATTERN =
/url\s*\(['|"]?([^\)'"]+)['|"]?\)/i

Class Method Summary collapse

Class Method Details

.rewrite_content_urls!(filename, content) ⇒ Object

rewrite the URL reference paths url(../../../images/active_scaffold/default/add.gif); url(/stylesheets/active_scaffold/default/../../../images/active_scaffold/default/add.gif); url(/stylesheets/active_scaffold/../../images/active_scaffold/default/add.gif); url(/stylesheets/../images/active_scaffold/default/add.gif); url(‘/images/active_scaffold/default/add.gif’);



17
18
19
20
# File 'lib/web_resource_bundler/content_management/css_url_rewriter.rb', line 17

def rewrite_content_urls!(filename, content)
  content.gsub!(URL_TAG_PATTERN) { "url('#{rewrite_relative_path(filename, $1)}')" }
  content
end

.rewrite_relative_path(source_url, relative_url) ⇒ Object

rewrites a relative path to an absolute path, removing excess “../” and “./” rewrite_relative_path(“stylesheets/default/global.css”, “../image.gif”) => “/stylesheets/image.gif”



6
7
8
9
# File 'lib/web_resource_bundler/content_management/css_url_rewriter.rb', line 6

def rewrite_relative_path(source_url, relative_url)
  return relative_url if URI.parse(relative_url).absolute?
  File.expand_path(relative_url, File.dirname(source_url))
end