Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Malcolm Tredinnick authored April 04, 2007
3  django/utils/html.py
... ...
@@ -1,6 +1,7 @@
1 1
 "HTML utilities suitable for global use."
2 2
 
3 3
 import re, string
  4
+from django.utils.encoding import smart_unicode
4 5
 
5 6
 # Configuration for urlize() function
6 7
 LEADING_PUNCTUATION  = ['(', '<', '&lt;']
@@ -24,7 +25,7 @@
24 25
 def escape(html):
25 26
     "Returns the given HTML with ampersands, quotes and carets encoded"
26 27
     if not isinstance(html, basestring):
27  
-        html = str(html)
  28
+        html = smart_unicode(html)
28 29
     return html.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;').replace("'", '&#39;')
29 30
 
30 31
 def linebreaks(value):
19  tests/regressiontests/defaultfilters/tests.py
... ...
@@ -1,3 +1,5 @@
  1
+# -*- coding: utf-8 -*-
  2
+
1 3
 r"""
2 4
 >>> floatformat(7.7)
3 5
 '7.7'
@@ -87,19 +89,19 @@
87 89
 >>> truncatewords('A sentence with a few words in it', 'not a number')
88 90
 'A sentence with a few words in it'
89 91
 
90  
->>> truncatewords_html('<p>one <a href="#">two - three <br>four</a> five</p>', 0) 
  92
+>>> truncatewords_html('<p>one <a href="#">two - three <br>four</a> five</p>', 0)
91 93
 ''
92  
- 
93  
->>> truncatewords_html('<p>one <a href="#">two - three <br>four</a> five</p>', 2) 
  94
+
  95
+>>> truncatewords_html('<p>one <a href="#">two - three <br>four</a> five</p>', 2)
94 96
 '<p>one <a href="#">two ...</a></p>'
95  
- 
96  
->>> truncatewords_html('<p>one <a href="#">two - three <br>four</a> five</p>', 4) 
  97
+
  98
+>>> truncatewords_html('<p>one <a href="#">two - three <br>four</a> five</p>', 4)
97 99
 '<p>one <a href="#">two - three <br>four ...</a></p>'
98 100
 
99  
->>> truncatewords_html('<p>one <a href="#">two - three <br>four</a> five</p>', 5) 
  101
+>>> truncatewords_html('<p>one <a href="#">two - three <br>four</a> five</p>', 5)
100 102
 '<p>one <a href="#">two - three <br>four</a> five</p>'
101 103
 
102  
->>> truncatewords_html('<p>one <a href="#">two - three <br>four</a> five</p>', 100) 
  104
+>>> truncatewords_html('<p>one <a href="#">two - three <br>four</a> five</p>', 100)
103 105
 '<p>one <a href="#">two - three <br>four</a> five</p>'
104 106
 
105 107
 >>> upper('Mixed case input')
@@ -166,6 +168,9 @@
166 168
 >>> escape('<some html & special characters > here')
167 169
 '&lt;some html &amp; special characters &gt; here'
168 170
 
  171
+>>> escape(u'<some html & special characters > here ĐÅ€£')
  172
+u'&lt;some html &amp; special characters &gt; here \xc4\x90\xc3\x85\xe2\x82\xac\xc2\xa3'
  173
+
169 174
 >>> linebreaks('line 1')
170 175
 '<p>line 1</p>'
171 176
 

0 notes on commit 5212911

Please sign in to comment.
Something went wrong with that request. Please try again.