Permalink
Browse files

Make CssAbsoluteFilter replace src="..." patterns with absolute paths

The workarounds for many CSS3 properties in IE use proprietary "Filter"
expressions. These often need a "src" variable with an image path. For
some reason, that path is intepreted as relative to the HTML file
including the CSS, not the CSS file itself.

This patch makes those paths absolute as well.
  • Loading branch information...
jonasvp committed Sep 26, 2011
1 parent ce6221e commit f42b683cf259fb9329859500192397fbfb4cc804
Showing with 13 additions and 1 deletion.
  1. +13 −1 compressor/filters/css_default.py
@@ -8,6 +8,7 @@
from compressor.utils import staticfiles
URL_PATTERN = re.compile(r'url\(([^\)]+)\)')
+SRC_PATTERN = re.compile(r'src=([\'"])(.+?)\1')
class CssAbsoluteFilter(FilterBase):
@@ -35,7 +36,8 @@ def input(self, filename=None, basename=None, **kwargs):
self.protocol = '%s/' % '/'.join(parts[:2])
self.host = parts[2]
self.directory_name = '/'.join((self.url, os.path.dirname(self.path)))
- return URL_PATTERN.sub(self.url_converter, self.content)
+ return SRC_PATTERN.sub(self.src_converter,
+ URL_PATTERN.sub(self.url_converter, self.content))
def find(self, basename):
if settings.DEBUG and basename and staticfiles.finders:
@@ -86,3 +88,13 @@ def url_converter(self, matchobj):
if self.has_scheme:
full_url = "%s%s" % (self.protocol, full_url)
return "url('%s')" % self.add_suffix(full_url)
+
+ def src_converter(self, matchobj):
+ url = matchobj.group(2)
+ url = url.strip(' \'"')
+ if url.startswith(('http://', 'https://', '/', 'data:')):
+ return "src='%s'" % self.add_suffix(url)
+ full_url = posixpath.normpath('/'.join([str(self.directory_name), url]))
+ if self.has_scheme:
+ full_url = "%s%s" % (self.protocol, full_url)
+ return "src='%s'" % self.add_suffix(full_url)

0 comments on commit f42b683

Please sign in to comment.