From 381f01f1d530a763fd808eae2bf13a741bc1b529 Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Fri, 20 May 2011 20:10:06 -0700 Subject: [PATCH] Make xhtml_escape work in python3; add test. --- tornado/escape.py | 2 +- tornado/test/escape_test.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) 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))) -- 2.47.2