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.

Added test of "js inline" with non-ascii character
Test reproduces `'ascii' codec can't encode character u'\u2014'` when using non-ascii characters in `{% compress js inline %}`.



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
@@ -107,7 +107,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.decode('utf-8')
+            return rendered_output
         except Exception:
             if settings.DEBUG or forced:
                 raise


Test patch:
diff --git a/compressor/tests/test_offline.py b/compressor/tests/test_offline.py
--- a/compressor/tests/test_offline.py
+++ b/compressor/tests/test_offline.py
@@ -236,3 +236,23 @@ def test_get_loaders(self):
             self.assertTrue(isinstance(loaders[1], AppDirectoriesLoader))
         finally:
             settings.TEMPLATE_LOADERS = old_loaders
+
+
+class OfflineGenerationInlineNonAsciiTestCase(OfflineTestCaseMixin, TestCase):
+    templates_dir = "test_inline_non_ascii"
+
+    def setUp(self):
+        self.old_offline_context = settings.COMPRESS_OFFLINE_CONTEXT
+        settings.COMPRESS_OFFLINE_CONTEXT = {
+            'test_non_ascii_value': u'\u2014',
+        }
+        super(OfflineGenerationInlineNonAsciiTestCase, self).setUp()
+
+    def tearDown(self):
+        self.COMPRESS_OFFLINE_CONTEXT = self.old_offline_context
+        super(OfflineGenerationInlineNonAsciiTestCase, self).tearDown()
+
+    def test_offline(self):
+        count, result = CompressCommand().compress(log=self.log, verbosity=self.verbosity)
+        rendered_template = self.template.render(Context(settings.COMPRESS_OFFLINE_CONTEXT))
+        self.assertEqual(rendered_template, "".join(result) + "\n")
diff --git a/compressor/tests/test_templates/test_inline_non_ascii/test_compressor_offline.html b/compressor/tests/test_templates/test_inline_non_ascii/test_compressor_offline.html
new file mode 100644
--- /dev/null
+++ b/compressor/tests/test_templates/test_inline_non_ascii/test_compressor_offline.html
@@ -0,0 +1,7 @@
+{% load compress %}{% spaceless %}
+
+{% compress js inline %}
+    <script type="text/javascript">
+        var value = '{{ test_non_ascii_value }}';
+    </script>
+{% endcompress %}{% endspaceless %}