Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.0.X] Fixed #10369 -- Fixed auto-escaping inside "tran" and "blockt…

…rans" tags.

Patch from Andrew Badr.

Backport of r10519 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10520 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 13475ed646ec47d779e416efb8349393c12bcf3f 1 parent f943e2e
Malcolm Tredinnick authored April 11, 2009
19  django/template/__init__.py
@@ -789,6 +789,18 @@ def __repr__(self):
789 789
 
790 790
     def render(self, context):
791 791
         return self.s
  792
+    
  793
+def _render_value_in_context(value, context):
  794
+    """
  795
+    Converts any value to a string to become part of a rendered template. This
  796
+    means escaping, if required, and conversion to a unicode object. If value
  797
+    is a string, it is expected to have already been translated.
  798
+    """
  799
+    value = force_unicode(value)
  800
+    if (context.autoescape and not isinstance(value, SafeData)) or isinstance(value, EscapeData):
  801
+        return escape(value)
  802
+    else:
  803
+        return value
792 804
 
793 805
 class VariableNode(Node):
794 806
     def __init__(self, filter_expression):
@@ -799,15 +811,12 @@ def __repr__(self):
799 811
 
800 812
     def render(self, context):
801 813
         try:
802  
-            output = force_unicode(self.filter_expression.resolve(context))
  814
+            output = self.filter_expression.resolve(context)
803 815
         except UnicodeDecodeError:
804 816
             # Unicode conversion can fail sometimes for reasons out of our
805 817
             # control (e.g. exception rendering). In that case, we fail quietly.
806 818
             return ''
807  
-        if (context.autoescape and not isinstance(output, SafeData)) or isinstance(output, EscapeData):
808  
-            return force_unicode(escape(output))
809  
-        else:
810  
-            return force_unicode(output)
  819
+        return _render_value_in_context(output, context)
811 820
 
812 821
 def generic_tag_compiler(params, defaults, name, node_class, parser, token):
813 822
     "Returns a template.Node subclass."
6  django/templatetags/i18n.py
... ...
@@ -1,6 +1,6 @@
1 1
 import re
2 2
 
3  
-from django.template import Node, Variable, VariableNode
  3
+from django.template import Node, Variable, VariableNode, _render_value_in_context
4 4
 from django.template import TemplateSyntaxError, TokenParser, Library
5 5
 from django.template import TOKEN_TEXT, TOKEN_VAR
6 6
 from django.utils import translation
@@ -43,7 +43,7 @@ def render(self, context):
43 43
         if self.noop:
44 44
             return value
45 45
         else:
46  
-            return translation.ugettext(value)
  46
+            return _render_value_in_context(translation.ugettext(value), context)
47 47
 
48 48
 class BlockTranslateNode(Node):
49 49
     def __init__(self, extra_context, singular, plural=None, countervar=None,
@@ -82,7 +82,7 @@ def render(self, context):
82 82
             result = translation.ugettext(singular)
83 83
         # Escape all isolated '%' before substituting in the context.
84 84
         result = re.sub(u'%(?!\()', u'%%', result)
85  
-        data = dict([(v, force_unicode(context[v])) for v in vars])
  85
+        data = dict([(v, _render_value_in_context(context[v], context)) for v in vars])
86 86
         context.pop()
87 87
         return result % data
88 88
 
12  tests/regressiontests/templates/tests.py
@@ -825,12 +825,16 @@ def get_template_tests(self):
825 825
             'i18n14': ('{% cycle "foo" _("Password") _(\'Password\') as c %} {% cycle c %} {% cycle c %}', {'LANGUAGE_CODE': 'de'}, 'foo Passwort Passwort'),
826 826
             'i18n15': ('{{ absent|default:_("Password") }}', {'LANGUAGE_CODE': 'de', 'absent': ""}, 'Passwort'),
827 827
             'i18n16': ('{{ _("<") }}', {'LANGUAGE_CODE': 'de'}, '<'),
828  
-            'i18n17': ('{{ _("") }}', {'LANGUAGE_CODE': 'de'}, ''),
  828
+            'i18n16a': ('{{ _("") }}', {'LANGUAGE_CODE': 'de'}, ''),
829 829
 
830  
-            # Escaping inside blocktrans works as if it was directly in the
  830
+            # Escaping inside blocktrans and trans works as if it was directly in the
831 831
             # template.
832  
-            'i18n18': ('{% load i18n %}{% blocktrans with anton|escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'),
833  
-            'i18n19': ('{% load i18n %}{% blocktrans with anton|force_escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'),
  832
+            'i18n17': ('{% load i18n %}{% blocktrans with anton|escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'),
  833
+            'i18n18': ('{% load i18n %}{% blocktrans with anton|force_escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'),
  834
+            'i18n19': ('{% load i18n %}{% blocktrans %}{{ andrew }}{% endblocktrans %}', {'andrew': 'a & b'}, u'a &amp; b'),
  835
+            'i18n20': ('{% load i18n %}{% trans andrew %}', {'andrew': 'a & b'}, u'a &amp; b'),
  836
+            'i18n21': ('{% load i18n %}{% blocktrans %}{{ andrew }}{% endblocktrans %}', {'andrew': mark_safe('a & b')}, u'a & b'),
  837
+            'i18n22': ('{% load i18n %}{% trans andrew %}', {'andrew': mark_safe('a & b')}, u'a & b'),
834 838
 
835 839
             ### HANDLING OF TEMPLATE_STRING_IF_INVALID ###################################
836 840
 

0 notes on commit 13475ed

Please sign in to comment.
Something went wrong with that request. Please try again.