From 21928907e4ae8f739cc9baf006d580ce7d2aaf07 Mon Sep 17 00:00:00 2001 From: Nathaniel Bibler <git@nathanielbibler.com> Date: Sun, 27 Mar 2011 22:35:49 -0400 Subject: [PATCH] Extend cache busting to allow for path manipulation --- lib/compass/configuration/data.rb | 6 ++++-- lib/compass/sass_extensions/functions/urls.rb | 15 ++++++++++--- test/compass_test.rb | 11 ++++++++++ .../stylesheets/busted_image_urls/config.rb | 20 ++++++++++++++++++ .../busted_image_urls/css/screen.css | 5 +++++ .../busted_image_urls/images/grid.png | Bin 0 -> 199 bytes .../busted_image_urls/sass/screen.sass | 8 +++++++ 7 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/stylesheets/busted_image_urls/config.rb create mode 100644 test/fixtures/stylesheets/busted_image_urls/css/screen.css create mode 100644 test/fixtures/stylesheets/busted_image_urls/images/grid.png create mode 100644 test/fixtures/stylesheets/busted_image_urls/sass/screen.sass diff --git a/lib/compass/configuration/data.rb b/lib/compass/configuration/data.rb index 9cde724d..0173fa93 100644 --- a/lib/compass/configuration/data.rb +++ b/lib/compass/configuration/data.rb @@ -71,8 +71,10 @@ module Compass end # When called with a block, defines the cache buster strategy to be used. - # The block must return nil or a string that can be appended to a url as a query parameter. - # The returned string must not include the starting '?'. + # If the block returns nil or a string, then it is appended to the url as a query parameter. + # In this case, the returned string must not include the starting '?'. + # The block may also return a full path (i.e. /images/grid-BUSTED.png) and it + # will replace the original path and query string with the busted path returned. # The block will be passed the root-relative url of the asset. # If the block accepts two arguments, it will also be passed a File object # that points to the asset on disk -- which may or may not exist. diff --git a/lib/compass/sass_extensions/functions/urls.rb b/lib/compass/sass_extensions/functions/urls.rb index d140c8a5..5fce4b69 100644 --- a/lib/compass/sass_extensions/functions/urls.rb +++ b/lib/compass/sass_extensions/functions/urls.rb @@ -91,9 +91,7 @@ module Compass::SassExtensions::Functions::Urls if cache_buster.is_a?(Sass::Script::String) path += "?#{cache_buster.value}" else - if buster = compute_cache_buster(path, real_path) - path += "?#{buster}" - end + path = cache_busted_path(path, real_path) end end @@ -137,6 +135,17 @@ module Compass::SassExtensions::Functions::Urls end end + def cache_busted_path(path, real_path) + cache_buster = compute_cache_buster(path, real_path) + if cache_buster.nil? + path + elsif cache_buster =~ %r{/} + cache_buster + else + "%s?%s" % [path, cache_buster] + end + end + def compute_cache_buster(path, real_path) if Compass.configuration.asset_cache_buster args = [path] diff --git a/test/compass_test.rb b/test/compass_test.rb index 26491328..69cea09c 100644 --- a/test/compass_test.rb +++ b/test/compass_test.rb @@ -71,6 +71,17 @@ class CompassTest < Test::Unit::TestCase end end + def test_busted_image_urls + within_project('busted_image_urls') do |proj| + each_css_file(proj.css_path) do |css_file| + assert_no_errors css_file, 'busted_image_urls' + end + each_sass_file do |sass_file| + assert_renders_correctly sass_file + end + end + end + def test_image_urls within_project('image_urls') do |proj| each_css_file(proj.css_path) do |css_file| diff --git a/test/fixtures/stylesheets/busted_image_urls/config.rb b/test/fixtures/stylesheets/busted_image_urls/config.rb new file mode 100644 index 00000000..9961c9bd --- /dev/null +++ b/test/fixtures/stylesheets/busted_image_urls/config.rb @@ -0,0 +1,20 @@ +# Require any additional compass plugins here. +project_type = :stand_alone +css_dir = "tmp" +sass_dir = "sass" +images_dir = "images" +output_style = :compact +# To enable relative image paths using the images_url() function: +# http_images_path = :relative +http_images_path = "/images" +line_comments = false + +asset_cache_buster do |path, file| + pathname = Pathname.new(path) + "%s/%s-BUSTED%s" % [pathname.dirname, pathname.basename(pathname.extname), pathname.extname] +end + + +asset_host do |path| + "http://assets%d.example.com" % (path.size % 4) +end diff --git a/test/fixtures/stylesheets/busted_image_urls/css/screen.css b/test/fixtures/stylesheets/busted_image_urls/css/screen.css new file mode 100644 index 00000000..9935c20f --- /dev/null +++ b/test/fixtures/stylesheets/busted_image_urls/css/screen.css @@ -0,0 +1,5 @@ +.showgrid { background-image: url('http://assets0.example.com/images/grid-BUSTED.png'); } + +.inlinegrid { background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAUEAYAAACv1qP4AAAABmJLR0T///////8JWPfcAAAACXBIWXMAAABIAAAASABGyWs+AAAAZ0lEQVRYw+3QwQ2AIBAFUTEUwI3+uzN7gDscsIgxEuO8An52J11X73OudfxMraXkzHfO3Y98nQEhA0IGhAwIGRAyIGRAyICQASEDQgaEDAgZEDIgZEDIgJABoZzSGK3tPuN9ERFP7Nw4fg+c5g8V1wAAAABJRU5ErkJggg=='); } + +.no-buster { background-image: url('http://assets0.example.com/images/grid.png'); } diff --git a/test/fixtures/stylesheets/busted_image_urls/images/grid.png b/test/fixtures/stylesheets/busted_image_urls/images/grid.png new file mode 100644 index 0000000000000000000000000000000000000000..76aaa851a17551b07c608797105bf6e4ba204148 GIT binary patch literal 199 zcmeAS@N?(olHy`uVBq!ia0vp^8bB-}zy>7OUt9bGNU<e(ySw}c1I~!=cYq?C1s;*b zK-vS0-A-oP0U7C@E{-7)hu>Z}$lIVGz#3>Ma-jF$ZsY0(YncrlhC+|`Fx8c*$A-Tz zURV0ZXYJA_XUfmr?XQ{3sL1TZ*22RfDPW|K;&7s20;a&iIhQ2XzO{Q?D=6sy=8i=j W|D0$1qSt{|GI+ZBxvX<aXaWF=nM2<I literal 0 HcmV?d00001 diff --git a/test/fixtures/stylesheets/busted_image_urls/sass/screen.sass b/test/fixtures/stylesheets/busted_image_urls/sass/screen.sass new file mode 100644 index 00000000..e16697bc --- /dev/null +++ b/test/fixtures/stylesheets/busted_image_urls/sass/screen.sass @@ -0,0 +1,8 @@ +.showgrid + background-image: image-url(unquote("grid.png")) + +.inlinegrid + background-image: inline-image(unquote("grid.png")) + +.no-buster + background-image: image-url("grid.png", $only-path: false, $cache-buster: false)