Ticket #9655: 9655.patch

File 9655.patch, 2.1 KB (added by Aymeric Augustin, 12 years ago)
  • django/utils/html.py

     
    1717DOTS = [u'·', u'*', u'\u2022', u'•', u'•', u'•']
    1818
    1919unencoded_ampersands_re = re.compile(r'&(?!(\w+|#\d+);)')
     20unquoted_percents_re = re.compile(r'%(?![0-9A-Fa-f]{2})')
    2021word_split_re = re.compile(r'(\s+)')
    2122punctuation_re = re.compile('^(?P<lead>(?:%s)*)(?P<middle>.*?)(?P<trail>(?:%s)*)$' % \
    2223    ('|'.join([re.escape(x) for x in LEADING_PUNCTUATION]),
     
    100101    return unencoded_ampersands_re.sub('&amp;', force_unicode(value))
    101102fix_ampersands = allow_lazy(fix_ampersands, unicode)
    102103
     104def smart_urlquote(url):
     105    """Quotes an URL if it isn't already quoted."""
     106    # An URL is considered unquoted if it contains no % character, or if it
     107    # contains a % not followed by two hexadecimal digits. See #9655.
     108    if '%' not in url or unquoted_percents_re.search(url):
     109        # See http://bugs.python.org/issue2637
     110        return urlquote(url, safe='!*\'();:@&=+$,/?#[]~')
     111    return url
     112
    103113def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False):
    104114    """
    105115    Converts any URLs in text into clickable links.
     
    130140            # Make URL we want to point to.
    131141            url = None
    132142            if middle.startswith('http://') or middle.startswith('https://'):
    133                 url = urlquote(middle, safe='/&=:;#?+*')
     143                url = smart_urlquote(url)
    134144            elif middle.startswith('www.') or ('@' not in middle and \
    135145                    middle and middle[0] in string.ascii_letters + string.digits and \
    136146                    (middle.endswith('.org') or middle.endswith('.net') or middle.endswith('.com'))):
    137                 url = urlquote('http://%s' % middle, safe='/&=:;#?+*')
     147                url = smart_urlquote('http://%s' % middle)
    138148            elif '@' in middle and not ':' in middle and simple_email_re.match(middle):
    139149                url = 'mailto:%s' % middle
    140150                nofollow_attr = ''
Back to Top