Skip to content
Browse files

Merge pull request #194 from peterlundberg/develop

Handle css correct If COMPRESS_PRECOMPILERS is used and COMPRESS_ENABLED is False
  • Loading branch information...
2 parents 1c40b53 + ea96281 commit 78d1db28da2a6d160eb597d83eddb86cbb33a601 Jannis Leidel committed Jan 30, 2012
Showing with 66 additions and 7 deletions.
  1. +4 −3 compressor/css.py
  2. +22 −4 compressor/tests/base.py
  3. +40 −0 compressor/tests/templatetags.py
View
7 compressor/css.py
@@ -30,9 +30,10 @@ def split_contents(self):
if data:
self.split_content.append(data)
media = elem_attribs.get('media', None)
- # Append to the previous node if it had the same media type,
- # otherwise create a new node.
- if self.media_nodes and self.media_nodes[-1][0] == media:
+ # Append to the previous node if it had the same media type
+ append_to_previous = self.media_nodes and self.media_nodes[-1][0] == media
+ # and we are not just precompiling, otherwise create a new node.
+ if append_to_previous and settings.COMPRESS_ENABLED:
self.media_nodes[-1][1].split_content.append(data)
else:
node = CssCompressor(content=self.parser.elem_str(elem),
View
26 compressor/tests/base.py
@@ -142,21 +142,39 @@ def setUp(self):
<style type="text/css" media="print">p { border:5px solid green;}</style>
<link rel="stylesheet" href="/media/css/two.css" type="text/css" media="all">
<style type="text/css">h1 { border:5px solid green;}</style>"""
- self.css_node = CssCompressor(self.css)
def test_css_output(self):
- links = BeautifulSoup(self.css_node.output()).findAll('link')
+ css_node = CssCompressor(self.css)
+ links = BeautifulSoup(css_node.output()).findAll('link')
media = [u'screen', u'print', u'all', None]
self.assertEqual(len(links), 4)
self.assertEqual(media, [l.get('media', None) for l in links])
def test_avoid_reordering_css(self):
css = self.css + '<style type="text/css" media="print">p { border:10px solid red;}</style>'
- node = CssCompressor(css)
+ css_node = CssCompressor(css)
media = [u'screen', u'print', u'all', None, u'print']
- links = BeautifulSoup(node.output()).findAll('link')
+ links = BeautifulSoup(css_node.output()).findAll('link')
self.assertEqual(media, [l.get('media', None) for l in links])
+ def test_passthough_when_compress_disabled(self):
+ original_precompilers = settings.COMPRESS_PRECOMPILERS
+ settings.COMPRESS_ENABLED = False
+ settings.COMPRESS_PRECOMPILERS = (
+ ('text/foobar', 'python %s {infile} {outfile}' % os.path.join(test_dir, 'precompiler.py')),
+ )
+ css = """\
+<link rel="stylesheet" href="/media/css/one.css" type="text/css" media="screen">
+<link rel="stylesheet" href="/media/css/two.css" type="text/css" media="screen">
+<style type="text/foobar" media="screen">h1 { border:5px solid green;}</style>"""
+ css_node = CssCompressor(css)
+ output = BeautifulSoup(css_node.output()).findAll(['link','style'])
+ self.assertEqual([u'/media/css/one.css', u'/media/css/two.css', None],
+ [l.get('href', None) for l in output])
+ self.assertEqual([u'screen', u'screen', u'screen'],
+ [l.get('media', None) for l in output])
+ settings.COMPRESS_PRECOMPILERS = original_precompilers
+
class VerboseTestCase(CompressorTestCase):
View
40 compressor/tests/templatetags.py
@@ -137,6 +137,7 @@ def setUp(self):
settings.COMPRESS_ENABLED = True
settings.COMPRESS_PRECOMPILERS = (
('text/coffeescript', '%s %s' % (python, precompiler)),
+ ('text/less', '%s %s' % (python, precompiler)),
)
self.context = {'MEDIA_URL': settings.COMPRESS_URL}
@@ -222,6 +223,45 @@ def test_multiple_file_order_conserved(self):
finally:
settings.COMPRESS_ENABLED = self.old_enabled
+ def test_css_multiple_files_disabled_compression(self):
+ self.old_enabled = settings.COMPRESS_ENABLED
+ settings.COMPRESS_ENABLED = False
+ assert(settings.COMPRESS_PRECOMPILERS)
+ try:
+ template = u"""
+ {% load compress %}{% compress css %}
+ <link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}css/one.css"></link>
+ <link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}css/two.css"></link>
+ {% endcompress %}"""
+
+ out = '\n'.join([
+ '<link rel="stylesheet" type="text/css" href="/media/css/one.css" />',
+ '<link rel="stylesheet" type="text/css" href="/media/css/two.css" />'])
+
+ self.assertEqual(out, render(template, self.context))
+ finally:
+ settings.COMPRESS_ENABLED = self.old_enabled
+
+ def test_css_multiple_files_mixed_precompile_disabled_compression(self):
+ self.old_enabled = settings.COMPRESS_ENABLED
+ settings.COMPRESS_ENABLED = False
+ assert(settings.COMPRESS_PRECOMPILERS)
+ try:
+ template = u"""
+ {% load compress %}{% compress css %}
+ <link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}css/one.css"/>
+ <link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}css/two.css"/>
+ <link rel="stylesheet" type="text/less" href="{{ MEDIA_URL }}css/url/test.css"/>
+ {% endcompress %}"""
+
+ out = '\n'.join([
+ '<link rel="stylesheet" type="text/css" href="/media/css/one.css" />',
+ '<link rel="stylesheet" type="text/css" href="/media/css/two.css" />',
+ '<link rel="stylesheet" href="/media/CACHE/css/test.c4f8a285c249.css" type="text/css" />'])
+ self.assertEqual(out, render(template, self.context))
+ finally:
+ settings.COMPRESS_ENABLED = self.old_enabled
+
def script(content="", src="", scripttype="text/javascript"):
"""
returns a unicode text html script element.

0 comments on commit 78d1db2

Please sign in to comment.
Something went wrong with that request. Please try again.