1
0
mirror of https://github.com/django/django.git synced 2025-07-04 01:39:20 +00:00

unicode: Added unicode-aware equivalents of urllib.quote() and

urllib.quote_plus() in django.utils.html.


git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5328 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2007-05-24 06:44:54 +00:00
parent 31d5d55090
commit a09cb2a9e5
2 changed files with 32 additions and 2 deletions

View File

@ -1,7 +1,9 @@
"HTML utilities suitable for global use."
import re, string
from django.utils.encoding import force_unicode
import re
import string
import urllib
from django.utils.encoding import force_unicode, smart_str
from django.utils.functional import allow_lazy
# Configuration for urlize() function
@ -123,3 +125,22 @@ def clean_html(text):
return text
clean_html = allow_lazy(clean_html, unicode)
def urlquote(url, safe='/'):
"""
A version of Python's urllib.quote() function that can operate on unicode
strings. The url is first UTF-8 encoded before quoting. The returned string
can safely be used as part of an argument to a subsequent iri_to_uri() call
without double-quoting occurring.
"""
return force_unicode(urllib.quote(smart_str(url)))
urlquote = allow_lazy(urlquote, unicode)
def urlquote_plus(url, safe=''):
"""
A version of Python's urllib.quote_plus() function that can operate on
unicode strings. The url is first UTF-8 encoded before quoting. The
returned string can safely be used as part of an argument to a subsequent
iri_to_uri() call without double-quoting occurring.
"""
return force_unicode(urllib.quote_plus(smart_str(url), safe))
urlquote_plus = allow_lazy(urlquote_plus, unicode)

View File

@ -16,10 +16,19 @@ r"""
>>> print list(smart_split(r'''all friends' tests'''))[1]
friends'
### urlquote #############################################################
>>> from django.utils.html import urlquote, urlquote_plus
>>> urlquote(u'Paris & Orl\xe9ans')
u'Paris%20%26%20Orl%C3%A9ans'
>>> urlquote_plus(u'Paris & Orl\xe9ans')
u'Paris+%26+Orl%C3%A9ans'
### iri_to_uri ###########################################################
>>> from django.utils.encoding import iri_to_uri
>>> iri_to_uri(u'red%09ros\xe9#red')
'red%09ros%C3%A9#red'
>>> iri_to_uri(u'/blog/for/J\xfcrgen M\xfcnster/')
'/blog/for/J%C3%BCrgen%20M%C3%BCnster/'
>>> iri_to_uri(u'locations/%s' % urlquote_plus(u'Paris & Orl\xe9ans'))
'locations/Paris+%26+Orl%C3%A9ans'
"""