From: Radek Czajka Date: Fri, 17 Oct 2014 23:06:43 +0000 (+0200) Subject: Unescape var values in render middleware. X-Git-Tag: 0.2.5~1^2 X-Git-Url: https://git.mdrn.pl/django-ssify.git/commitdiff_plain/e89d22d4e7a6d5846fe964851b0f1a209bf0a521?ds=sidebyside Unescape var values in render middleware. --- diff --git a/ssify/middleware_debug.py b/ssify/middleware_debug.py index 863d8fe..b0a1e92 100644 --- a/ssify/middleware_debug.py +++ b/ssify/middleware_debug.py @@ -29,8 +29,9 @@ SSI_INCLUDE = re.compile(r"") SSI_IF = re.compile(r"(?P
)" r"(?P.*?)(?:(?P.*?))?" r"", re.S) - # TODO: escaped? -SSI_VAR = re.compile(r"\$\{(?P.+)\}") # TODO: escaped? +SSI_VAR = re.compile(r"\$\{(?P.+)\}") + +UNESCAPE = re.compile(r'\\(.)') class SsiRenderMiddleware(object): @@ -85,9 +86,11 @@ 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 "" diff --git a/tests/templates/tests_render/test_render.html b/tests/templates/tests_render/test_render.html new file mode 100644 index 0000000..baa2fe8 --- /dev/null +++ b/tests/templates/tests_render/test_render.html @@ -0,0 +1,3 @@ +{% load ugly_var_for_render_test from test_tags %} + +{% ugly_var_for_render_test %} diff --git a/tests/templatetags/test_tags.py b/tests/templatetags/test_tags.py index d0a43c1..d23e295 100644 --- a/tests/templatetags/test_tags.py +++ b/tests/templatetags/test_tags.py @@ -24,3 +24,8 @@ def number_of_quotes(request): @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'"\\ """ diff --git a/tests/tests/__init__.py b/tests/tests/__init__.py index 0fb0ebb..ac56434 100644 --- a/tests/tests/__init__.py +++ b/tests/tests/__init__.py @@ -13,4 +13,5 @@ from __future__ import unicode_literals 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 * diff --git a/tests/tests/test_render.py b/tests/tests/test_render.py new file mode 100644 index 0000000..eb64263 --- /dev/null +++ b/tests/tests/test_render.py @@ -0,0 +1,21 @@ +# -*- 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'"\\ """ + ) diff --git a/tests/urls.py b/tests/urls.py index 6b92b09..2c12f8c 100644 --- a/tests/urls.py +++ b/tests/urls.py @@ -53,4 +53,9 @@ urlpatterns = patterns( url(r'^language/(?P.+)$', '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') + ), )