Commit 788e9dd1 authored by Sylvain Thénault's avatar Sylvain Thénault
Browse files

fix for xml_escape called with unicode characters, string.translate want an unicode map...

parent 7b93cad27804
......@@ -95,6 +95,7 @@ TR_CONTROL_CHARS[ord('\f')] = '\n'
TR_CONTROL_CHARS[ord('\v')] = '\n'
ESC_CAR_TABLE = string.maketrans(''.join(CONTROL_CHARS),
''.join(TR_CONTROL_CHARS))
ESC_UCAR_TABLE = unicode(ESC_CAR_TABLE, 'latin1')
# XXX deprecate at some point (once less used :)
#@obsolete('use xml_escape')
......@@ -103,7 +104,10 @@ def html_escape(data):
def xml_escape(data):
"""escapes XML forbidden characters in attributes and PCDATA"""
data = data.translate(ESC_CAR_TABLE)
if isinstance(data, unicode):
data = data.translate(ESC_UCAR_TABLE)
else:
data = data.translate(ESC_CAR_TABLE)
return (data.replace('&','&amp;').replace('<','&lt;').replace('>','&gt;')
.replace('"','&quot;').replace("'",'&#39;'))
......
......@@ -34,6 +34,16 @@ class HtmlEscapeTC(TestCase):
if car in SPECIAL_CHARS:
continue
yield self.assertEquals, xml_escape(car), ' '
yield self.assertEquals, xml_escape(u'é'), u'é'
def test_escape_special_chars_unicode(self):
for car, trcar in SPECIAL_CHARS.items():
yield self.assertEquals, xml_escape(unicode(car)), trcar
for carnum in xrange(32):
car = chr(carnum)
if car in SPECIAL_CHARS:
continue
yield self.assertEquals, xml_escape(unicode(car)), ' '
def test_html_unescape(self):
for data, expected in [('toto', 'toto'),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment