Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Made django.utils.html.escape() work with unicode strings (and unicod…

…e-like

objects). Refs #3897.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@4919 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 5212911b1991aa749d828b31177dce99237068ed 1 parent 1bddac3
@malcolmt malcolmt authored
View
3  django/utils/html.py
@@ -1,6 +1,7 @@
"HTML utilities suitable for global use."
import re, string
+from django.utils.encoding import smart_unicode
# Configuration for urlize() function
LEADING_PUNCTUATION = ['(', '<', '&lt;']
@@ -24,7 +25,7 @@
def escape(html):
"Returns the given HTML with ampersands, quotes and carets encoded"
if not isinstance(html, basestring):
- html = str(html)
+ html = smart_unicode(html)
return html.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;').replace("'", '&#39;')
def linebreaks(value):
View
19 tests/regressiontests/defaultfilters/tests.py
@@ -1,3 +1,5 @@
+# -*- coding: utf-8 -*-
+
r"""
>>> floatformat(7.7)
'7.7'
@@ -87,19 +89,19 @@
>>> truncatewords('A sentence with a few words in it', 'not a number')
'A sentence with a few words in it'
->>> truncatewords_html('<p>one <a href="#">two - three <br>four</a> five</p>', 0)
+>>> truncatewords_html('<p>one <a href="#">two - three <br>four</a> five</p>', 0)
''
-
->>> truncatewords_html('<p>one <a href="#">two - three <br>four</a> five</p>', 2)
+
+>>> truncatewords_html('<p>one <a href="#">two - three <br>four</a> five</p>', 2)
'<p>one <a href="#">two ...</a></p>'
-
->>> truncatewords_html('<p>one <a href="#">two - three <br>four</a> five</p>', 4)
+
+>>> truncatewords_html('<p>one <a href="#">two - three <br>four</a> five</p>', 4)
'<p>one <a href="#">two - three <br>four ...</a></p>'
->>> truncatewords_html('<p>one <a href="#">two - three <br>four</a> five</p>', 5)
+>>> truncatewords_html('<p>one <a href="#">two - three <br>four</a> five</p>', 5)
'<p>one <a href="#">two - three <br>four</a> five</p>'
->>> truncatewords_html('<p>one <a href="#">two - three <br>four</a> five</p>', 100)
+>>> truncatewords_html('<p>one <a href="#">two - three <br>four</a> five</p>', 100)
'<p>one <a href="#">two - three <br>four</a> five</p>'
>>> upper('Mixed case input')
@@ -166,6 +168,9 @@
>>> escape('<some html & special characters > here')
'&lt;some html &amp; special characters &gt; here'
+>>> escape(u'<some html & special characters > here ĐÅ€£')
+u'&lt;some html &amp; special characters &gt; here \xc4\x90\xc3\x85\xe2\x82\xac\xc2\xa3'
+
>>> linebreaks('line 1')
'<p>line 1</p>'
Please sign in to comment.
Something went wrong with that request. Please try again.