Ticket #4555: django-unescape.patch

File django-unescape.patch, 2.0 KB (added by Johan Bergström <bugs@…>, 13 years ago)

Patch and test case for unescape

  • tests/regressiontests/defaultfilters/tests.py

     
    171171>>> escape(u'<some html & special characters > here ĐÅ€£')
    172172u'&lt;some html &amp; special characters &gt; here \xc4\x90\xc3\x85\xe2\x82\xac\xc2\xa3'
    173173
     174>>> unescape('&lt;some html &amp; special characters &gt; here')
     175'<some html & special characters > here'
     176
     177>>> unescape(u'&lt;some html &amp; special characters &gt; here \xc4\x90\xc3\x85\xe2\x82\xac\xc2\xa3')
     178u'<some html & special characters > here ĐÅ€£'
     179
    174180>>> linebreaks('line 1')
    175181'<p>line 1</p>'
    176182
  • django/utils/html.py

     
    2828        html = str(html)
    2929    return html.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;').replace("'", '&#39;')
    3030
     31def unescape(html):
     32    "Returns the given HTML with ampersands, quotes and carets decoded"
     33    if not isinstance(html, basestring):
     34        html = str(html)
     35    return html.replace('&amp;', '&').replace('&lt;', '<').replace('&gt;', '>').replace('&quot;', '"').replace('&#39;',"'")
     36
    3137def linebreaks(value):
    3238    "Converts newlines into <p> and <br />s"
    3339    value = re.sub(r'\r\n|\r|\n', '\n', value) # normalize newlines
  • django/template/defaultfilters.py

     
    259268    return escape(value)
    260269escape = stringfilter(escape)
    261270
     271def unescape(value):
     272    "Unescapes a string's HTML"
     273    from django.utils.html import unescape
     274    return unescape(value)
     275unescape = stringfilter(unescape)
     276
    262277def linebreaks(value):
    263278    "Converts newlines into <p> and <br />s"
    264279    from django.utils.html import linebreaks
Back to Top