From 49bf14e79a601a2fca5783857a217992ec326e58 Mon Sep 17 00:00:00 2001 From: Scimonster Date: Sun, 7 May 2017 14:32:52 +0300 Subject: [PATCH] Fix #1624 -- Unicode in XML sanitizing causes error Add unit test for it --- gluon/html.py | 4 ++-- gluon/tests/test_html.py | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/gluon/html.py b/gluon/html.py index ceef7bd2..bc6f5fd1 100644 --- a/gluon/html.py +++ b/gluon/html.py @@ -596,10 +596,10 @@ class XML(XmlComponent): for A, IMG and BlockQuote). The key is the tag; the value is a list of allowed attributes. """ - if sanitize: - text = sanitizer.sanitize(text, permitted_tags, allowed_attributes) if isinstance(text, unicodeT): text = to_native(text.encode('utf8', 'xmlcharrefreplace')) + if sanitize: + text = sanitizer.sanitize(text, permitted_tags, allowed_attributes) elif isinstance(text, bytes): text = to_native(text) elif not isinstance(text, str): diff --git a/gluon/tests/test_html.py b/gluon/tests/test_html.py index e091d1ce..ad611ed3 100644 --- a/gluon/tests/test_html.py +++ b/gluon/tests/test_html.py @@ -168,6 +168,8 @@ class TestBareHelpers(unittest.TestCase): # seams that __repr__ is no longer enough ##self.assertEqual(XML('1.3'), '1.3') self.assertEqual(XML(u'
è
').xml(), b'
\xc3\xa8
') + # make sure unicode works with sanitize + self.assertEqual(XML(u'
è
', sanitize=True).xml(), b'
\xc3\xa8
') # you can calc len on the class, that equals the xml() and the str() ##self.assertEqual(len(XML('1.3')), len('1.3')) self.assertEqual(len(XML('1.3').xml()), len('1.3'))