From: Ben Darnell Date: Sat, 21 May 2011 03:10:06 +0000 (-0700) Subject: Make xhtml_escape work in python3; add test. X-Git-Tag: v2.0.0~64 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=381f01f1d530a763fd808eae2bf13a741bc1b529;p=thirdparty%2Ftornado.git Make xhtml_escape work in python3; add test. --- diff --git a/tornado/escape.py b/tornado/escape.py index 5bd627abd..318fb96bb 100644 --- a/tornado/escape.py +++ b/tornado/escape.py @@ -53,7 +53,7 @@ except: def xhtml_escape(value): """Escapes a string so it is valid within XML or XHTML.""" - return xml.sax.saxutils.escape(value, {'"': """}) + return xml.sax.saxutils.escape(_unicode(value), {'"': """}) def xhtml_unescape(value): diff --git a/tornado/test/escape_test.py b/tornado/test/escape_test.py index 39f28e96f..c0fde83c7 100644 --- a/tornado/test/escape_test.py +++ b/tornado/test/escape_test.py @@ -3,6 +3,9 @@ import tornado.escape import unittest +from tornado.escape import utf8, xhtml_escape, xhtml_unescape +from tornado.util import b + linkify_tests = [ # (input, linkify_kwargs, expected_output) @@ -125,3 +128,15 @@ class EscapeTestCase(unittest.TestCase): linked = tornado.escape.linkify(text, **kwargs) self.assertEqual(linked, html) + def test_xhtml_escape(self): + tests = [ + ("", "<foo>"), + (u"", u"<foo>"), + (b(""), b("<foo>")), + + ("<>&\"", "<>&""), + ("&", "&amp;"), + ] + for unescaped, escaped in tests: + self.assertEqual(utf8(xhtml_escape(unescaped)), utf8(escaped)) + self.assertEqual(utf8(unescaped), utf8(xhtml_unescape(escaped)))