Ticket #6474: utils_conditional_escape.diff

File utils_conditional_escape.diff, 1.4 KB (added by Thomas Güttler, 17 years ago)
  • django/utils/html.py

     
    3838    """
    3939    if isinstance(html, SafeData):
    4040        return html
     41    html = force_unicode(html)
     42    if isinstance(html, SafeData):
     43        return html
    4144    else:
    4245        return escape(html)
    4346
  • tests/regressiontests/utils/tests.py

     
    55from unittest import TestCase
    66
    77from django.utils import html, checksums
     8from django.utils.safestring import mark_safe
    89
    910import timesince
    1011import datastructures
     
    4546        # Verify it doesn't double replace &.
    4647        self.check_output(f, '<&', '&lt;&amp;')
    4748
     49    def test_conditional_escape(self):
     50        class BoldHTML(object):
     51            def __init__(self, content):
     52                self.content = content
     53            def __unicode__(self):
     54                return mark_safe(u'<b>%s</b>' % f(self.content))
     55        f = html.conditional_escape
     56        b = BoldHTML('a')
     57        self.check_output(f, b, '<b>a</b>')
     58        b = BoldHTML('<>')
     59        self.check_output(f, b, '<b>&lt;&gt;</b>')
     60
    4861    def test_linebreaks(self):
    4962        f = html.linebreaks
    5063        items = (
Back to Top