both precompiled and standard files rendered without compression when COMPRESS_ENABLED==False and COMPRESS_PRECOMPILERS != []

previously only precompiled files (those with a mimetype in the COMPRESS_PRECOMPILERS setting) were rendered when
COMPRESS_ENABLED == False.
This commit is contained in:
Jervis Whitley
2011-11-24 16:35:34 +11:00
parent 37318acbb7
commit 0e389d7afb
5 changed files with 117 additions and 7 deletions

View File

@@ -215,13 +215,17 @@ class Compressor(object):
if not verbatim_content and not rendered_content: if not verbatim_content and not rendered_content:
return '' return ''
rendered_output = '\n'.join(c.encode(self.charset)
for c in rendered_content)
if settings.COMPRESS_ENABLED or forced: if settings.COMPRESS_ENABLED or forced:
filtered_content = self.filtered_output( filtered_content = self.filtered_output(rendered_output)
'\n'.join((c.encode(self.charset) for c in rendered_content)))
finished_content = self.handle_output(mode, filtered_content, forced) finished_content = self.handle_output(mode, filtered_content, forced)
verbatim_content.append(finished_content) verbatim_content.append(finished_content)
if verbatim_content: if verbatim_content:
if not settings.COMPRESS_ENABLED and rendered_output:
verbatim_content.append(rendered_output)
return '\n'.join(verbatim_content) return '\n'.join(verbatim_content)
return self.content return self.content

View File

@@ -13,9 +13,32 @@ COMPRESS_ENABLED
:Default: the opposite of ``DEBUG`` :Default: the opposite of ``DEBUG``
Boolean that decides if compression will happen. In order to test compression Boolean that decides if compression will happen. To test compression
when ``DEBUG`` is enabled COMPRESS_ENABLED_ needs to explicitly be set to when ``DEBUG`` is ``True`` COMPRESS_ENABLED_ must also be set to ``True``.
``True``.
When COMPRESS_ENABLED_ is ``False`` the input will be rendered without any
compression except for code with a mimetype matching one listed in the
COMPRESS_PRECOMPILERS_ setting. These matching files are still passed to the
precompiler before rendering.
An example for some javascript and coffeescript.
.. code-block:: django
{% load compress %}
{% compress js %}
<script type="text/javascript" src="/static/js/site-base.js" />
<script type="text/coffeescript" charset="utf-8" src="/static/js/awesome.coffee" />
{% endcompress %}
With COMPRESS_ENABLED_ set to ``False`` this would give you something like
this::
<script type="text/javascript" src="/static/js/site-base.js"></script>
<script type="text/javascript" src="/static/CACHE/js/8dd1a2872443.js" charset="utf-8"></script>
.. _compress_url: .. _compress_url:

View File

@@ -0,0 +1 @@
# this is a comment.

View File

@@ -5,4 +5,4 @@ from .offline import OfflineGenerationTestCase
from .parsers import LxmlParserTests, Html5LibParserTests, BeautifulSoupParserTests, HtmlParserTests from .parsers import LxmlParserTests, Html5LibParserTests, BeautifulSoupParserTests, HtmlParserTests
from .signals import PostCompressSignalTestCase from .signals import PostCompressSignalTestCase
from .storages import StorageTestCase from .storages import StorageTestCase
from .templatetags import TemplatetagTestCase from .templatetags import TemplatetagTestCase, PrecompilerTemplatetagTestCase

View File

@@ -1,5 +1,8 @@
from __future__ import with_statement from __future__ import with_statement
import os
import sys
from mock import Mock from mock import Mock
from django.template import Template, Context, TemplateSyntaxError from django.template import Template, Context, TemplateSyntaxError
@@ -8,7 +11,7 @@ from django.test import TestCase
from compressor.conf import settings from compressor.conf import settings
from compressor.signals import post_compress from compressor.signals import post_compress
from .base import css_tag from .base import css_tag, test_dir
def render(template_string, context_dict=None): def render(template_string, context_dict=None):
@@ -122,3 +125,82 @@ class TemplatetagTestCase(TestCase):
args, kwargs = callback.call_args args, kwargs = callback.call_args
context = kwargs['context'] context = kwargs['context']
self.assertEqual('foo', context['name']) self.assertEqual('foo', context['name'])
class PrecompilerTemplatetagTestCase(TestCase):
def setUp(self):
self.old_enabled = settings.COMPRESS_ENABLED
self.old_precompil = settings.COMPRESS_PRECOMPILERS
precompiler = os.path.join(test_dir, 'precompiler.py')
python = sys.executable
settings.COMPRESS_ENABLED = True
settings.COMPRESS_PRECOMPILERS = (
('text/coffeescript', '%s %s' % (python, precompiler)),)
self.context = {'MEDIA_URL': settings.COMPRESS_URL}
def tearDown(self):
settings.COMPRESS_ENABLED = self.old_enabled
settings.COMPRESS_PRECOMPILERS = self.old_precompil
def test_compress_coffeescript_tag(self):
template = u"""{% load compress %}{% compress js %}
<script type="text/coffeescript"># this is a comment.</script>
{% endcompress %}"""
out = script(src="/media/CACHE/js/e920d58f166d.js")
self.assertEqual(out, render(template, self.context))
def test_compress_coffeescript_tag_and_javascript_tag(self):
template = u"""{% load compress %}{% compress js %}
<script type="text/coffeescript"># this is a comment.</script>
<script type="text/javascript"># this too is a comment.</script>
{% endcompress %}"""
out = script(src="/media/CACHE/js/ef6b32a54575.js")
self.assertEqual(out, render(template, self.context))
def test_coffeescript_and_js_tag_with_compress_enabled_equals_false(self):
settings.COMPRESS_ENABLED = False
template = u"""{% load compress %}{% compress js %}
<script type="text/coffeescript"># this is a comment.</script>
<script type="text/javascript"># this too is a comment.</script>
{% endcompress %}"""
out = (script('# this is a comment.\n') + '\n' +
script('# this too is a comment.'))
self.assertEqual(out, render(template, self.context))
def test_compress_coffeescript_tag_compress_enabled_is_false(self):
settings.COMPRESS_ENABLED = False
template = u"""{% load compress %}{% compress js %}
<script type="text/coffeescript"># this is a comment.</script>
{% endcompress %}"""
out = script("# this is a comment.\n")
self.assertEqual(out, render(template, self.context))
def test_compress_coffeescript_file_tag_compress_enabled_is_false(self):
settings.COMPRESS_ENABLED = False
template = u"""
{% load compress %}{% compress js %}
<script type="text/coffeescript" src="{{ MEDIA_URL }}js/one.coffee">
</script>
{% endcompress %}"""
out = script(src="/media/CACHE/js/95cfb869eead.js")
self.assertEqual(out, render(template, self.context))
def script(content="", src="", scripttype="text/javascript"):
"""
returns a unicode text html script element.
>>> script('#this is a comment', scripttype="text/applescript")
'<script type="text/applescript">#this is a comment</script>'
"""
out_script = u'<script '
if scripttype:
out_script += u'type="%s" ' % scripttype
if src:
out_script += u'src="%s" ' % src
return out_script[:-1] + u'>%s</script>' % content