Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

now with css url rewriting!

git-svn-id: http://bundle-fu.googlecode.com/svn/trunk@27 1db77ec0-6337-0410-9320-454da9aca44f
  • Loading branch information...
commit e24239a79e3181277a27d33cc2583fee186421ce 1 parent 27a781a
timcharper authored
Showing with 32 additions and 5 deletions.
  1. +25 −4 lib/bundle_fu.rb
  2. +7 −1 test/fixtures/public/stylesheets/css_3.css
View
29 lib/bundle_fu.rb
@@ -22,11 +22,32 @@ def bundle_js_files(filenames=[], output_filename = "", options={})
}
end
- def rewrite_relative_path(old_filename, new_filename)
+ # rewrites a relative path to an absolute path, removing excess "../" and "./"
+ # rewrite_relative_path("stylesheets/default/global.css", "../image.gif") => "/stylesheets/image.gif"
+ def rewrite_relative_path(source_filename, relative_url)
+ return relative_url if relative_url.first == "/"
+ elements = File.join("/", File.dirname(source_filename)).gsub(/\/+/, '/').split("/")
+ elements += relative_url.gsub(/\/+/, '/').split("/")
+
+ index = 0
+ while(elements[index])
+ if (elements[index]==".")
+ elements.delete_at(index)
+ elsif (elements[index]=="..")
+ next if index==0
+ index-=1
+ 2.times { elements.delete_at(index)}
+
+ else
+ index+=1
+ end
+ end
+
+ elements * "/"
end
- def bundle_css_files(filenames=[], output_filename = "", options = {})
+ def bundle_css_files(filenames=[], options = {})
output = ""
each_read_file(filenames) { |filename, content|
# rewrite the URL reference paths
@@ -35,8 +56,8 @@ def bundle_css_files(filenames=[], output_filename = "", options = {})
# 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);
-
- output << self.send("sanitized_#{options[:type]}", options)
+ content.gsub!(/url *\(([^\)]+)\)/) { "url(#{rewrite_relative_path(filename, $1)})" }
+ output << content
}
output
end
View
8 test/fixtures/public/stylesheets/css_3.css
@@ -1 +1,7 @@
-css_3
+.relative_image_bg {
+ background-image: url(../images/background.gif)
+}
+
+.relative_image_bg_2 {
+ background-image: url(../images/groovy/background_2.gif)
+}
Please sign in to comment.
Something went wrong with that request. Please try again.