]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Make xhtml_escape work in python3; add test.
authorBen Darnell <ben@bendarnell.com>
Sat, 21 May 2011 03:10:06 +0000 (20:10 -0700)
committerBen Darnell <ben@bendarnell.com>
Sat, 21 May 2011 03:10:06 +0000 (20:10 -0700)
tornado/escape.py
tornado/test/escape_test.py

index 5bd627abd0a3b4ca830f00b6e5e20dfd62a557df..318fb96bbf613ed5d7af8f58b7406d854f7a28a7 100644 (file)
@@ -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, {'"': "&quot;"})
+    return xml.sax.saxutils.escape(_unicode(value), {'"': "&quot;"})
 
 
 def xhtml_unescape(value):
index 39f28e96f36b1e10b00feb58426e4b8a43baeac9..c0fde83c73cb74816ec58b74cd20fbbcb45a5df0 100644 (file)
@@ -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>", "&lt;foo&gt;"),
+            (u"<foo>", u"&lt;foo&gt;"),
+            (b("<foo>"), b("&lt;foo&gt;")),
+
+            ("<>&\"", "&lt;&gt;&amp;&quot;"),
+            ("&amp;", "&amp;amp;"),
+            ]
+        for unescaped, escaped in tests:
+            self.assertEqual(utf8(xhtml_escape(unescaped)), utf8(escaped))
+            self.assertEqual(utf8(unescaped), utf8(xhtml_unescape(escaped)))