Support cache-busting suffix on asset URLs with fragments.

This commit is contained in:
Carl Meyer
2012-09-22 20:51:32 -06:00
parent 8279b90942
commit 11f751dbb0
2 changed files with 25 additions and 0 deletions

View File

@@ -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):

View File

@@ -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')