Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #316 from carljm/smarter-css-absolute-filter

Smarter css absolute filter
  • Loading branch information...
commit 7348af6e823a4969d25a50e038fc8d8443f5ad53 2 parents 3b9332d + 255700c
@diox diox authored
View
9 compressor/filters/css_default.py
@@ -50,6 +50,10 @@ def guess_filename(self, url):
# 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]
+ # remove querystring, 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 +77,15 @@ def add_suffix(self, url):
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
36 compressor/tests/test_filters.py
@@ -124,6 +124,42 @@ def test_css_absolute_filter(self):
"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_querystring(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?foo&%(hash)s') }" % 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?foo&%(hash)s') }" % 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')
Please sign in to comment.
Something went wrong with that request. Please try again.