diff options
| author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-07-02 19:32:17 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-07-03 10:48:02 +0200 |
| commit | f226bdbf4e06aa8ca787e34b0c626965ac526f28 (patch) | |
| tree | 55f30b22a0c0128f1ae10ab9ab4827ae5e9f505e /django/utils | |
| parent | a5308514fb4bc5086c9a16a8a24a945eeebb073c (diff) | |
Refs #30608 -- Added django.utils.encoding.punycode().
Diffstat (limited to 'django/utils')
| -rw-r--r-- | django/utils/encoding.py | 5 | ||||
| -rw-r--r-- | django/utils/html.py | 5 |
2 files changed, 8 insertions, 2 deletions
diff --git a/django/utils/encoding.py b/django/utils/encoding.py index 94b63762db..2e2ad44e31 100644 --- a/django/utils/encoding.py +++ b/django/utils/encoding.py @@ -218,6 +218,11 @@ def escape_uri_path(path): return quote(path, safe="/:@&+$,-_.!~*'()") +def punycode(domain): + """Return the Punycode of the given domain if it's non-ASCII.""" + return domain.encode('idna').decode('ascii') + + def repercent_broken_unicode(path): """ As per section 3.2 of RFC 3987, step three of converting a URI into an IRI, diff --git a/django/utils/html.py b/django/utils/html.py index fb636600c9..d8fa00dab8 100644 --- a/django/utils/html.py +++ b/django/utils/html.py @@ -8,6 +8,7 @@ from urllib.parse import ( parse_qsl, quote, unquote, urlencode, urlsplit, urlunsplit, ) +from django.utils.encoding import punycode from django.utils.functional import Promise, keep_lazy, keep_lazy_text from django.utils.http import RFC3986_GENDELIMS, RFC3986_SUBDELIMS from django.utils.safestring import SafeData, SafeString, mark_safe @@ -210,7 +211,7 @@ def smart_urlquote(url): return unquote_quote(url) try: - netloc = netloc.encode('idna').decode('ascii') # IDN -> ACE + netloc = punycode(netloc) # IDN -> ACE except UnicodeError: # invalid domain part return unquote_quote(url) @@ -319,7 +320,7 @@ def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False): elif ':' not in middle and is_email_simple(middle): local, domain = middle.rsplit('@', 1) try: - domain = domain.encode('idna').decode('ascii') + domain = punycode(domain) except UnicodeError: continue url = 'mailto:%s@%s' % (local, domain) |
