Ticket #10369: i18n_autoescape_t10369_r10178.diff

File i18n_autoescape_t10369_r10178.diff, 4.1 KB (added by Andrew Badr, 15 years ago)

Fix with tests

  • django/templatetags/i18n.py

     
    11import re
    22
    3 from django.template import Node, Variable, VariableNode
     3from django.template import Node, Variable, VariableNode, render_value_in_context
    44from django.template import TemplateSyntaxError, TokenParser, Library
    55from django.template import TOKEN_TEXT, TOKEN_VAR
    66from django.utils import translation
     
    4343        if self.noop:
    4444            return value
    4545        else:
    46             return translation.ugettext(value)
     46            return render_value_in_context(translation.ugettext(value), context)
    4747
    4848class BlockTranslateNode(Node):
    4949    def __init__(self, extra_context, singular, plural=None, countervar=None,
     
    8282            result = translation.ugettext(singular)
    8383        # Escape all isolated '%' before substituting in the context.
    8484        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])
    8686        context.pop()
    8787        return result % data
    8888
  • django/template/__init__.py

     
    802802
    803803    def render(self, context):
    804804        return self.s
     805   
     806def render_value_in_context(value, context):
     807    """
     808    Converts any value to a string to become part of a rendered template. This
     809    means escaping, if required, and conversion to a unicode object. If value
     810    is a string, it is expected to have already been translated.
     811    """
     812    value = force_unicode(value)
     813    if (context.autoescape and not isinstance(value, SafeData)) or isinstance(value, EscapeData):
     814        return escape(value)
     815    else:
     816        return value
    805817
    806818class VariableNode(Node):
    807819    def __init__(self, filter_expression):
     
    812824
    813825    def render(self, context):
    814826        try:
    815             output = force_unicode(self.filter_expression.resolve(context))
     827            output = self.filter_expression.resolve(context)
    816828        except UnicodeDecodeError:
    817829            # Unicode conversion can fail sometimes for reasons out of our
    818830            # control (e.g. exception rendering). In that case, we fail quietly.
    819831            return ''
    820         if (context.autoescape and not isinstance(output, SafeData)) or isinstance(output, EscapeData):
    821             return force_unicode(escape(output))
    822         else:
    823             return force_unicode(output)
     832        return render_value_in_context(output, context)
    824833
    825834def generic_tag_compiler(params, defaults, name, node_class, parser, token):
    826835    "Returns a template.Node subclass."
  • tests/regressiontests/templates/tests.py

     
    806806            'i18n15': ('{{ absent|default:_("Password") }}', {'LANGUAGE_CODE': 'de', 'absent': ""}, 'Passwort'),
    807807            'i18n16': ('{{ _("<") }}', {'LANGUAGE_CODE': 'de'}, '<'),
    808808
    809             # Escaping inside blocktrans works as if it was directly in the
     809            # Escaping inside blocktrans and trans works as if it was directly in the
    810810            # template.
    811811            'i18n17': ('{% load i18n %}{% blocktrans with anton|escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'),
    812812            'i18n18': ('{% load i18n %}{% blocktrans with anton|force_escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'),
     813            'i18n19': ('{% load i18n %}{% blocktrans %}{{ andrew }}{% endblocktrans %}', {'andrew': 'a & b'}, u'a &amp; b'),
     814            'i18n20': ('{% load i18n %}{% trans andrew %}', {'andrew': 'a & b'}, u'a &amp; b'),
    813815
    814816            ### HANDLING OF TEMPLATE_STRING_IF_INVALID ###################################
    815817
Back to Top