SSI_IF = re.compile(r"(?P<header><!--#if expr='(?P<expr>[^']*)'-->)"
r"(?P<value>.*?)(?:<!--#else-->(?P<else>.*?))?"
r"<!--#endif-->", re.S)
- # TODO: escaped?
-SSI_VAR = re.compile(r"\$\{(?P<var>.+)\}") # TODO: escaped?
+SSI_VAR = re.compile(r"\$\{(?P<var>.+)\}")
+
+UNESCAPE = re.compile(r'\\(.)')
class SsiRenderMiddleware(object):
def ssi_set(match):
"""Interprets SSI set statement."""
- variables[match.group('var')] = match.group('value')
+ content = match.group('value')
+ content = re.sub(UNESCAPE, r'\1', content)
+ variables[match.group('var')] = content
if conf.RENDER_VERBOSE:
- return match.group(0)
+ return content
else:
return ""
--- /dev/null
+{% load ugly_var_for_render_test from test_tags %}
+
+{% ugly_var_for_render_test %}
@ssi_variable(register)
def quote_len_odd(request, which):
return bool(len(QUOTES[which]) % 1)
+
+
+@ssi_variable(register)
+def ugly_var_for_render_test(request):
+ return """Zażółć gęślą jaźń\n'"\\ <!--#echo var='test' encoding='none'-->"""
from .test_args import *
from .test_basic import *
from .test_csrf import *
-from .test_locale import *
\ No newline at end of file
+from .test_locale import *
+from .test_render import *
--- /dev/null
+# -*- coding: utf-8 -*-
+# This file is part of django-ssify, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Nowoczesna Polska. See README.md for more information.
+#
+from __future__ import unicode_literals
+
+from django.test import TestCase
+from django.test.utils import override_settings
+
+
+@override_settings(SSIFY_RENDER=True)
+class RenderTestCase(TestCase):
+ def test_render(self):
+ """Renders the complete view using the SsiRenderMiddleware."""
+ response = self.client.get('/render')
+ if hasattr(response, 'render') and callable(response.render):
+ response.render()
+ self.assertEqual(
+ response.content.decode('utf-8').strip(),
+ """Zażółć gęślą jaźń\n'"\\ <!--#echo var='test' encoding='none'-->"""
+ )
url(r'^language/(?P<lang>.+)$', 'language_with_lang', name='language_with_lang'),
url(r'^language$', 'language_without_lang', name='language_without_lang'),
url(r'^bad_language$', 'language_with_lang', name='bad_language_with_lang'),
+
+ # tests.render
+ url(r'^render$',
+ TemplateView.as_view(template_name='tests_render/test_render.html')
+ ),
)