Repository: django-compressor/django-compressor

Issue:
UnicodeDecodeError with inline css or javascript (jinja2 CompressorExtension)
With `COMPRESS_ENABLED = False`

``` html
{% compress css %}
    <style type="text/css">
            /* няня */
    </style>
{% endcompress %}
```

or

``` html
{% compress js %}
        <script type="text/javascript">
            // ололо
        </script>
{% endcompress %}
```

raises

```
Traceback:
File "/project/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  136.                     response = response.render()
File "/project/lib/python2.7/site-packages/django/template/response.py" in render
  97.             self._set_content(self.rendered_content)
File "/project/lib/python2.7/site-packages/django/template/response.py" in rendered_content
  75.         content = template.render(context)
File "/project/nazya/libs/jinja2ext/loader.py" in render
  54.         return super(Template, self).render(context_dict)
File "/project/lib/python2.7/site-packages/jinja2/environment.py" in render
  894.         return self.environment.handle_exception(exc_info, True)
File "/project/lib/python2.7/site-packages/jinja2/environment.py" in handle_exception
  664.         traceback = _make_traceback(exc_info, source_hint)
File "/project/lib/python2.7/site-packages/jinja2/debug.py" in make_traceback
  139.     return translate_exception(exc_info, initial_skip)
File "/project/lib/python2.7/site-packages/jinja2/environment.py" in render
  891.             return concat(self.root_render_func(self.new_context(vars)))

Exception Type: UnicodeDecodeError at /mail/
Exception Value: 'ascii' codec can't decode byte 0xd0 in position 576: ordinal not in range(128)
```

I use Jinja2==2.6 and django-compressor from git 354ecdc30bc0fc6eca9455879c6a0bb1c992b242.



Discussion:

Resolving patch:
diff --git a/compressor/templatetags/compress.py b/compressor/templatetags/compress.py
--- a/compressor/templatetags/compress.py
+++ b/compressor/templatetags/compress.py
@@ -108,7 +108,7 @@ def render_compressed(self, context, kind, mode, forced=False):
             rendered_output = self.render_output(compressor, mode, forced=forced)
             if cache_key:
                 cache_set(cache_key, rendered_output)
-            return rendered_output
+            return rendered_output.decode('utf-8')
         except Exception:
             if settings.DEBUG or forced:
                 raise


Test patch:
diff --git a/compressor/tests/test_jinja2ext.py b/compressor/tests/test_jinja2ext.py
--- a/compressor/tests/test_jinja2ext.py
+++ b/compressor/tests/test_jinja2ext.py
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
 from __future__ import with_statement
 
 from django.test import TestCase
@@ -123,3 +124,15 @@ def test_js_inline(self):
         context = {'MEDIA_URL': settings.COMPRESS_URL}
         out = '<script type="text/javascript">obj={};obj.value="value";</script>'
         self.assertEqual(out, template.render(context))
+
+    def test_nonascii_inline_css(self):
+        org_COMPRESS_ENABLED = settings.COMPRESS_ENABLED
+        settings.COMPRESS_ENABLED = False
+        template = self.env.from_string(u'{% compress css %}'
+                                        u'<style type="text/css">'
+                                        u'/* русский текст */'
+                                        u'</style>{% endcompress %}')
+        out = u'<link rel="stylesheet" href="/media/CACHE/css/b2cec0f8cb24.css" type="text/css" />'
+        settings.COMPRESS_ENABLED = org_COMPRESS_ENABLED
+        context = {'MEDIA_URL': settings.COMPRESS_URL}
+        self.assertEqual(out, template.render(context))