From b7fea9409618ac23485a1048f4435f6afbc11739 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Thu, 26 Jun 2008 05:07:13 +0000 Subject: [PATCH] Fixed #7542 -- Fixed bug in urlize where it was appending 'http://' to the link text. Thanks for the patch and tests, devin git-svn-id: http://code.djangoproject.com/svn/django/trunk@7755 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/utils/html.py | 52 +++++++++---------- tests/regressiontests/defaultfilters/tests.py | 6 +-- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/django/utils/html.py b/django/utils/html.py index 07e4f0d3f4..747af52879 100644 --- a/django/utils/html.py +++ b/django/utils/html.py @@ -76,20 +76,20 @@ def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False): """ Converts any URLs in text into clickable links. - Works on http://, https://, and www. links. Links can have trailing - punctuation (periods, commas, close-parens) and leading punctuation - (opening parens) and it'll still do the right thing. + Works on http://, https://, www. links and links ending in .org, .net or + .com. Links can have trailing punctuation (periods, commas, close-parens) + and leading punctuation (opening parens) and it'll still do the right + thing. If trim_url_limit is not None, the URLs in link text longer than this limit will truncated to trim_url_limit-3 characters and appended with an elipsis. If nofollow is True, the URLs in link text will get a rel="nofollow" attribute. + + If autoescape is True, the link text and URLs will get autoescaped. """ - if autoescape: - trim_url = lambda x, limit=trim_url_limit: conditional_escape(limit is not None and (len(x) > limit and ('%s...' % x[:max(0, limit - 3)])) or x) - else: - trim_url = lambda x, limit=trim_url_limit: limit is not None and (len(x) > limit and ('%s...' % x[:max(0, limit - 3)])) or x + trim_url = lambda x, limit=trim_url_limit: limit is not None and (len(x) > limit and ('%s...' % x[:max(0, limit - 3)])) or x safe_input = isinstance(text, SafeData) words = word_split_re.split(force_unicode(text)) nofollow_attr = nofollow and ' rel="nofollow"' or '' @@ -97,30 +97,30 @@ def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False): match = punctuation_re.match(word) if match: lead, middle, trail = match.groups() - if safe_input: - middle = mark_safe(middle) - if middle.startswith('www.') or ('@' not in middle and not (middle.startswith('http://') or middle.startswith('https://')) and \ - len(middle) > 0 and middle[0] in string.ascii_letters + string.digits and \ - (middle.endswith('.org') or middle.endswith('.net') or middle.endswith('.com'))): - middle = 'http://%s' % middle + # Make URL we want to point to. + url = None if middle.startswith('http://') or middle.startswith('https://'): url = urlquote(middle, safe='/&=:;#?+*') - if autoescape and not safe_input: - url = escape(url) - trimmed_url = trim_url(middle) - middle = '%s' % (url, nofollow_attr, - trimmed_url) - elif '@' in middle and not middle.startswith('www.') and \ - not ':' in middle and simple_email_re.match(middle): - if autoescape: - middle = conditional_escape(middle) - middle = '%s' % (middle, middle) - if lead + middle + trail != word: + elif middle.startswith('www.') or ('@' not in middle and \ + len(middle) > 0 and middle[0] in string.ascii_letters + string.digits and \ + (middle.endswith('.org') or middle.endswith('.net') or middle.endswith('.com'))): + url = urlquote('http://%s' % middle, safe='/&=:;#?+*') + elif '@' in middle and not ':' in middle and simple_email_re.match(middle): + url = 'mailto:%s' % middle + nofollow_attr = '' + # Make link. + if url: + trimmed = trim_url(middle) if autoescape and not safe_input: lead, trail = escape(lead), escape(trail) + url, trimmed = escape(url), escape(trimmed) + middle = '%s' % (url, nofollow_attr, trimmed) words[i] = mark_safe('%s%s%s' % (lead, middle, trail)) - elif autoescape and not safe_input: - words[i] = escape(word) + else: + if safe_input: + words[i] = mark_safe(word) + elif autoescape: + words[i] = escape(word) elif safe_input: words[i] = mark_safe(word) elif autoescape: diff --git a/tests/regressiontests/defaultfilters/tests.py b/tests/regressiontests/defaultfilters/tests.py index 62403935d4..b56c33a652 100644 --- a/tests/regressiontests/defaultfilters/tests.py +++ b/tests/regressiontests/defaultfilters/tests.py @@ -150,7 +150,7 @@ u'fran%C3%A7ois%20%26%20jill' u'http://short.com/' >>> urlizetrunc(u'http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=', 20) -u'http://www.google....' +u'http://www.google...' >>> urlizetrunc('http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=', 20) u'http://www.google...' @@ -174,10 +174,10 @@ u'http://google.com' u'http://google.com/' >>> urlize('www.google.com') -u'http://www.google.com' +u'www.google.com' >>> urlize('djangoproject.org') -u'http://djangoproject.org' +u'djangoproject.org' >>> urlize('info@djangoproject.org') u'info@djangoproject.org'