From 11f751dbb090adecc63482dc60d832f11cd38f28 Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Sat, 22 Sep 2012 20:51:32 -0600 Subject: [PATCH] Support cache-busting suffix on asset URLs with fragments. --- compressor/filters/css_default.py | 7 +++++++ compressor/tests/test_filters.py | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/compressor/filters/css_default.py b/compressor/filters/css_default.py index fea0898..938cb94 100644 --- a/compressor/filters/css_default.py +++ b/compressor/filters/css_default.py @@ -50,6 +50,8 @@ class CssAbsoluteFilter(FilterBase): # COMPRESS_URL had a protocol, # remove it and the hostname from our path. local_path = local_path.replace(self.protocol + self.host, "", 1) + # remove url fragment, if any + local_path = local_path.rsplit("#", 1)[0] # Now, we just need to check if we can find # the path from COMPRESS_URL in our url if local_path.startswith(self.url_path): @@ -73,10 +75,15 @@ class CssAbsoluteFilter(FilterBase): if suffix is None: return url if url.startswith(SCHEMES): + fragment = None + if "#" in url: + url, fragment = url.rsplit("#", 1) if "?" in url: url = "%s&%s" % (url, suffix) else: url = "%s?%s" % (url, suffix) + if fragment is not None: + url = "%s#%s" % (url, fragment) return url def _converter(self, matchobj, group, template): diff --git a/compressor/tests/test_filters.py b/compressor/tests/test_filters.py index 4c8b47c..22298c4 100644 --- a/compressor/tests/test_filters.py +++ b/compressor/tests/test_filters.py @@ -124,6 +124,24 @@ class CssAbsolutizingTestCase(TestCase): "p { filter: Alpha(src='%(url)simg/python.png?%(hash)s') }") % params self.assertEqual(output, filter.input(filename=filename, basename='css/url/test.css')) + def test_css_absolute_filter_url_fragment(self): + filename = os.path.join(settings.COMPRESS_ROOT, 'css/url/test.css') + imagefilename = os.path.join(settings.COMPRESS_ROOT, 'img/python.png') + params = { + 'url': settings.COMPRESS_URL, + 'hash': self.hashing_func(imagefilename), + } + content = "p { background: url('../../img/python.png#foo') }" + + output = "p { background: url('%(url)simg/python.png?%(hash)s#foo') }" % params + filter = CssAbsoluteFilter(content) + self.assertEqual(output, filter.input(filename=filename, basename='css/url/test.css')) + settings.COMPRESS_URL = params['url'] = 'http://media.example.com/' + filter = CssAbsoluteFilter(content) + filename = os.path.join(settings.COMPRESS_ROOT, 'css/url/test.css') + output = "p { background: url('%(url)simg/python.png?%(hash)s#foo') }" % params + self.assertEqual(output, filter.input(filename=filename, basename='css/url/test.css')) + def test_css_absolute_filter_https(self): filename = os.path.join(settings.COMPRESS_ROOT, 'css/url/test.css') imagefilename = os.path.join(settings.COMPRESS_ROOT, 'img/python.png')