Commit 366aee21 authored by Jens Vagelpohl's avatar Jens Vagelpohl

- LP #143273: Enable the dtml-var modifiers url_quote, url_unquote,

  url_quote_plus and url_unquote_plus to handle unicode strings.
parent 47590946
...@@ -11,6 +11,9 @@ http://docs.zope.org/zope2/releases/. ...@@ -11,6 +11,9 @@ http://docs.zope.org/zope2/releases/.
Bugs Fixed Bugs Fixed
++++++++++ ++++++++++
- LP #143273: Enable the dtml-var modifiers url_quote, url_unquote,
url_quote_plus and url_unquote_plus to handle unicode strings.
2.12.9 (2010-07-13) 2.12.9 (2010-07-13)
------------------- -------------------
......
...@@ -342,15 +342,31 @@ class Call: ...@@ -342,15 +342,31 @@ class Call:
def url_quote(v, name='(Unknown name)', md={}): def url_quote(v, name='(Unknown name)', md={}):
if isinstance(v, unicode):
# quote does not handle unicode. Encoding to a "safe"
# intermediate encoding before quoting, then unencoding the result.
return quote(v.encode('utf-8')).decode('UTF-8')
return quote(str(v)) return quote(str(v))
def url_quote_plus(v, name='(Unknown name)', md={}): def url_quote_plus(v, name='(Unknown name)', md={}):
if isinstance(v, unicode):
# quote_plus does not handle unicode. Encoding to a "safe"
# intermediate encoding before quoting, then unencoding the result.
return quote_plus(v.encode('utf-8')).decode('UTF-8')
return quote_plus(str(v)) return quote_plus(str(v))
def url_unquote(v, name='(Unknown name)', md={}): def url_unquote(v, name='(Unknown name)', md={}):
if isinstance(v, unicode):
# unquote does not handle unicode. Encoding to a "safe"
# intermediate encoding before quoting, then unencoding the result.
return unquote(v.encode('utf-8')).decode('UTF-8')
return unquote(str(v)) return unquote(str(v))
def url_unquote_plus(v, name='(Unknown name)', md={}): def url_unquote_plus(v, name='(Unknown name)', md={}):
if isinstance(v, unicode):
# unquote_plus does not handle unicode. Encoding to a "safe"
# intermediate encoding before quoting, then unencoding the result.
return unquote_plus(v.encode('utf-8')).decode('UTF-8')
return unquote_plus(str(v)) return unquote_plus(str(v))
def newline_to_br(v, name='(Unknown name)', md={}): def newline_to_br(v, name='(Unknown name)', md={}):
......
...@@ -62,9 +62,43 @@ class TestNewlineToBr(doctest.DocTestCase): ...@@ -62,9 +62,43 @@ class TestNewlineToBr(doctest.DocTestCase):
""" """
class TestUrlQuoting(unittest.TestCase):
def test_url_quoting(self):
from DocumentTemplate.DT_Var import url_quote
from DocumentTemplate.DT_Var import url_unquote
unicode_value = u'G\xfcnther M\xfcller'
quoted_unicode_value = u'G%C3%BCnther%20M%C3%BCller'
utf8_value = unicode_value.encode('UTF-8')
quoted_utf8_value = 'G%C3%BCnther%20M%C3%BCller'
self.assertEquals(url_quote(unicode_value), quoted_unicode_value)
self.assertEquals(url_quote(utf8_value), quoted_utf8_value)
self.assertEquals(url_unquote(quoted_unicode_value), unicode_value)
self.assertEquals(url_unquote(quoted_utf8_value), utf8_value)
def test_url_quoting_plus(self):
from DocumentTemplate.DT_Var import url_quote_plus
from DocumentTemplate.DT_Var import url_unquote_plus
unicode_value = u'G\xfcnther M\xfcller'
quoted_unicode_value = u'G%C3%BCnther+M%C3%BCller'
utf8_value = unicode_value.encode('UTF-8')
quoted_utf8_value = 'G%C3%BCnther+M%C3%BCller'
self.assertEquals(url_quote_plus(unicode_value), quoted_unicode_value)
self.assertEquals(url_quote_plus(utf8_value), quoted_utf8_value)
self.assertEquals(url_unquote_plus(quoted_unicode_value), unicode_value)
self.assertEquals(url_unquote_plus(quoted_utf8_value), utf8_value)
def test_suite(): def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTest(doctest.DocTestSuite()) suite.addTest(doctest.DocTestSuite())
suite.addTest(unittest.makeSuite(TestUrlQuoting))
return suite return suite
if __name__ == '__main__': if __name__ == '__main__':
......
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