Ticket #2215: django-escape.diff

File django-escape.diff, 1.1 KB (added by Matias Hermanrud Fjeld <mhf@…>, 9 years ago)
  • django/utils/html.py

     
    11"HTML utilities suitable for global use."
    22
    33import re, string
     4from htmlentitydefs import entitydefs
    45
    56# Configuration for urlize() function
    67LEADING_PUNCTUATION  = ['(', '<', '&lt;']
     
    2122trailing_empty_content_re = re.compile(r'(?:<p>(?:&nbsp;|\s|<br \/>)*?</p>\s*)+\Z')
    2223del x # Temporary variable
    2324
     25entitytrans = dict([(value, key) for key, value in entitydefs.iteritems() if len(value) == 1])
     26entitytrans.pop(' ', None)
     27entitytrans["'"] = '#39'
     28
    2429def escape(html):
    2530    "Returns the given HTML with ampersands, quotes and carets encoded"
    26     if not isinstance(html, basestring):
    27         html = str(html)
    28     return html.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;').replace("'", '&#39;')
     31    return ''.join([c in entitytrans and '&%s;' % entitytrans[c] or c for c in html])
    2932
    3033def linebreaks(value):
    3134    "Converts newlines into <p> and <br />s"
Back to Top