[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/pagetemplate/ Made URLQuote usable when quoting unicode strings for use in Zope3 URLs

Bjorn Tillenius bjoti777 at student.liu.se
Thu Aug 26 08:09:15 EDT 2004


Log message for revision 27274:
  Made URLQuote usable when quoting unicode strings for use in Zope3 URLs
  
  Also added missing svn:properties.
  


Changed:
  UU  Zope3/trunk/src/zope/app/pagetemplate/tests/test_urlquote.py
  UU  Zope3/trunk/src/zope/app/pagetemplate/urlquote.py


-=-
Modified: Zope3/trunk/src/zope/app/pagetemplate/tests/test_urlquote.py
===================================================================
--- Zope3/trunk/src/zope/app/pagetemplate/tests/test_urlquote.py	2004-08-25 20:42:43 UTC (rev 27273)
+++ Zope3/trunk/src/zope/app/pagetemplate/tests/test_urlquote.py	2004-08-26 12:09:15 UTC (rev 27274)
@@ -17,9 +17,11 @@
 something (and don't really scramble stuff). We are relying on the python urllib
 to be functional to avoid test duplication.
 
-$Id: test_talesapi.py 25177 2004-06-02 13:17:31Z jim $
+$Id$
 """
 
+import unittest
+
 from zope.testing.doctestunit import DocTestSuite
 from zope.app.pagetemplate.urlquote import URLQuote
 
@@ -33,11 +35,11 @@
     """
     >>> q = URLQuote(u"www.google.de")
     >>> q.quote()
-    u'www.google.de'
+    'www.google.de'
     >>> q.unquote()
     u'www.google.de'
     >>> q.quote_plus()
-    u'www.google.de'
+    'www.google.de'
     >>> q.unquote_plus()
     u'www.google.de'
     """
@@ -48,15 +50,18 @@
     >>> q.quote()
     'www.google.de'
     >>> q.unquote()
-    'www.google.de'
+    u'www.google.de'
     >>> q.quote_plus()
     'www.google.de'
     >>> q.unquote_plus()
-    'www.google.de'
+    u'www.google.de'
     """
 
 def test_suite():
-    return DocTestSuite()
+    return unittest.TestSuite((
+        DocTestSuite(),
+        DocTestSuite('zope.app.pagetemplate.urlquote'),
+        ))
 
 if __name__ == '__main__':
     unittest.main()


Property changes on: Zope3/trunk/src/zope/app/pagetemplate/tests/test_urlquote.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: Zope3/trunk/src/zope/app/pagetemplate/urlquote.py
===================================================================
--- Zope3/trunk/src/zope/app/pagetemplate/urlquote.py	2004-08-25 20:42:43 UTC (rev 27273)
+++ Zope3/trunk/src/zope/app/pagetemplate/urlquote.py	2004-08-26 12:09:15 UTC (rev 27274)
@@ -13,7 +13,7 @@
 ##############################################################################
 """URL quoting for ZPT
 
-$Id: simpleviewclass.py 25177 2004-06-02 13:17:31Z jim $
+$Id$
 """
 __docformat__ = 'restructuredtext'
 
@@ -22,28 +22,70 @@
 from zope.app.traversing.interfaces import IPathAdapter
 
 class URLQuote(object):
+    r"""An adapter for URL quoting.
 
+    It quotes unicode strings according to the recommendation in RFC 2718.
+    Before the unicode string gets quoted, it gets encoded with UTF-8.
+
+        >>> quoter = URLQuote(u'Roki\u0161kis')
+        >>> quoter.quote()
+        'Roki%C5%A1kis'
+        
+        >>> quoter.quote_plus()
+        'Roki%C5%A1kis'
+
+    And when unquoting, it assumes the unquoted string is encoded with
+    UTF-8, and tries to convert it to unicode.
+
+        >>> quoter = URLQuote('Roki%C5%A1kis')
+        >>> quoter.unquote()
+        u'Roki\u0161kis'
+        
+        >>> quoter.unquote_plus()
+        u'Roki\u0161kis'
+
+    If the unquoted string can't be converted to unicode, the unquoted
+    string is returned.
+
+        >>> quoter = URLQuote('S%F6derk%F6ping')
+        >>> quoter.unquote()
+        'S\xf6derk\xf6ping'
+
+        >>> quoter.unquote_plus()
+        'S\xf6derk\xf6ping'
+    """
+
     __used_for__ = basestring
     implements(IPathAdapter)
 
     def __init__(self, context):
         if not isinstance(context, basestring):
             context = str(context)
+        elif isinstance(context, unicode):
+            context = context.encode('utf-8')
         self.context = context
 
     def quote(self):
-        """Return the objects URL quote representation."""
+        """Return the object's URL quote representation."""
         return urllib.quote(self.context)
 
     def quote_plus(self):
-        """Return the objects URL quote_plus representation."""
+        """Return the object's URL quote_plus representation."""
         return urllib.quote_plus(self.context)
 
     def unquote(self):
-        """Return the objects URL unquote representation."""
-        return urllib.unquote(self.context)
+        """Return the object's URL unquote representation."""
+        unquoted = urllib.unquote(self.context)
+        try:
+            return unicode(unquoted, 'utf-8')
+        except UnicodeDecodeError:
+            return unquoted
 
     def unquote_plus(self):
-        """Return the objects URL unquote_plus representation."""
-        return urllib.unquote_plus(self.context)
+        """Return the object's URL unquote_plus representation."""
+        unquoted = urllib.unquote_plus(self.context)
+        try:
+            return unicode(unquoted, 'utf-8')
+        except UnicodeDecodeError:
+            return unquoted
 


Property changes on: Zope3/trunk/src/zope/app/pagetemplate/urlquote.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native



More information about the Zope3-Checkins mailing list