Ticket #16395: urlize_malformed_urls.diff

File urlize_malformed_urls.diff, 2.3 KB (added by Bernhard Essl, 8 years ago)
  • django/utils/html.py

     
    2525html_gunk_re = re.compile(r'(?:<br clear="all">|<i><\/i>|<b><\/b>|<em><\/em>|<strong><\/strong>|<\/?smallcaps>|<\/?uppercase>)', re.IGNORECASE)
    2626hard_coded_bullets_re = re.compile(r'((?:<p>(?:%s).*?[a-zA-Z].*?</p>\s*)+)' % '|'.join([re.escape(x) for x in DOTS]), re.DOTALL)
    2727trailing_empty_content_re = re.compile(r'(?:<p>(?:&nbsp;|\s|<br \/>)*?</p>\s*)+\Z')
     28startswith_http_re = re.compile(r'^https?://\w')
    2829del x # Temporary variable
    2930
    3031def escape(html):
     
    128129            lead, middle, trail = match.groups()
    129130            # Make URL we want to point to.
    130131            url = None
    131             if middle.startswith('http://') or middle.startswith('https://'):
     132            if re.match(startswith_http_re, middle):
    132133                url = urlquote(middle, safe='/&=:;#?+*')
    133134            elif middle.startswith('www.') or ('@' not in middle and \
    134135                    middle and middle[0] in string.ascii_letters + string.digits and \
    135                     (middle.endswith('.org') or middle.endswith('.net') or middle.endswith('.com'))):
     136                    (middle.endswith('.org') or middle.endswith('.net') or middle.endswith('.com')) and \
     137                    (not middle.startswith('http'))):
    136138                url = urlquote('http://%s' % middle, safe='/&=:;#?+*')
    137139            elif '@' in middle and not ':' in middle and simple_email_re.match(middle):
    138140                url = 'mailto:%s' % middle
  • tests/regressiontests/defaultfilters/tests.py

     
    217217        self.assertEqual(urlize('https://google.com'),
    218218            u'<a href="https://google.com" rel="nofollow">https://google.com</a>')
    219219
     220        # malformed URIs
     221        self.assertEqual(urlize('http://///www.google.fr/?f=1'),
     222            u'http://///www.google.fr/?f=1')
     223        self.assertEqual(urlize('http://....google.com'),
     224            u'http://....google.com')
     225
    220226    def test_wordcount(self):
    221227        self.assertEqual(wordcount(''), 0)
    222228        self.assertEqual(wordcount(u'oneword'), 1)
Back to Top