Django

Code

Ticket #5270: 5270-empty-string-template-w-tests.diff

File 5270-empty-string-template-w-tests.diff, 2.9 kB (added by jdunck, 5 months ago)

Added tests, and now handling the empty i18n constant specially for translation.

  • django/template/__init__.py

    old new  
    485485                                           (token[:upto], token[upto:start], token[start:])) 
    486486            if var == None: 
    487487                var, constant, i18n_constant = match.group("var", "constant", "i18n_constant") 
    488                 if i18n_constant: 
    489                     var = '"%s"' %  _(i18n_constant.replace(r'\"', '"')) 
    490                 elif constant: 
     488                if i18n_constant is not None: 
     489                    if i18n_constant == "": 
     490                        var = '""' 
     491                    else: 
     492                        var = '"%s"' %  _(i18n_constant.replace(r'\"', '"')) 
     493                elif constant is not None: 
    491494                    var = '"%s"' % constant.replace(r'\"', '"') 
    492495                upto = match.end() 
    493496                if var == None: 
  • tests/regressiontests/templates/tests.py

    old new  
    365365 
    366366            # Numbers as filter arguments should work 
    367367            'filter-syntax19': ('{{ var|truncatewords:1 }}', {"var": "hello world"}, "hello ..."), 
     368             
     369            #filters should accept empty string constants 
     370            'filter-syntax20': ('{{ ""|default_if_none:"was none" }}', {}, ""), 
    368371 
    369372            ### COMMENT SYNTAX ######################################################## 
    370373            'comment-syntax01': ("{# this is hidden #}hello", {}, "hello"), 
     
    770773            'i18n14': ('{% cycle "foo" _("Password") _(\'Password\') as c %} {% cycle c %} {% cycle c %}', {'LANGUAGE_CODE': 'de'}, 'foo Passwort Passwort'), 
    771774            'i18n15': ('{{ absent|default:_("Password") }}', {'LANGUAGE_CODE': 'de', 'absent': ""}, 'Passwort'), 
    772775            'i18n16': ('{{ _("<") }}', {'LANGUAGE_CODE': 'de'}, '<'), 
     776            'i18n17': ('{{ _("") }}', {'LANGUAGE_CODE': 'de'}, ''), 
    773777 
    774778            # Escaping inside blocktrans works as if it was directly in the 
    775779            # template. 
    776             'i18n17': ('{% load i18n %}{% blocktrans with anton|escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'), 
    777             'i18n18': ('{% load i18n %}{% blocktrans with anton|force_escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'), 
     780            'i18n18': ('{% load i18n %}{% blocktrans with anton|escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'), 
     781            'i18n19': ('{% load i18n %}{% blocktrans with anton|force_escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α &amp; β'), 
    778782 
    779783            ### HANDLING OF TEMPLATE_STRING_IF_INVALID ################################### 
    780784