Django

Code

Changeset 7755

Show
Ignore:
Timestamp:
06/26/08 00:07:13 (2 months ago)
Author:
adrian
Message:

Fixed #7542 -- Fixed bug in urlize where it was appending 'http://' to the link text. Thanks for the patch and tests, devin

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/utils/html.py

    r7701 r7755  
    7777    Converts any URLs in text into clickable links. 
    7878 
    79     Works on http://, https://, and www. links.  Links can have trailing 
    80     punctuation (periods, commas, close-parens) and leading punctuation 
    81     (opening parens) and it'll still do the right thing. 
     79    Works on http://, https://, www. links and links ending in .org, .net or 
     80    .com. Links can have trailing punctuation (periods, commas, close-parens) 
     81    and leading punctuation (opening parens) and it'll still do the right 
     82    thing. 
    8283 
    8384    If trim_url_limit is not None, the URLs in link text longer than this limit 
     
    8687    If nofollow is True, the URLs in link text will get a rel="nofollow" 
    8788    attribute. 
     89 
     90    If autoescape is True, the link text and URLs will get autoescaped. 
    8891    """ 
    89     if autoescape: 
    90         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) 
    91     else: 
    92         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 
     92    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 
    9393    safe_input = isinstance(text, SafeData) 
    9494    words = word_split_re.split(force_unicode(text)) 
     
    9898        if match: 
    9999            lead, middle, trail = match.groups() 
    100             if safe_input: 
    101                 middle = mark_safe(middle) 
    102             if middle.startswith('www.') or ('@' not in middle and not (middle.startswith('http://') or middle.startswith('https://')) and \ 
     100            # Make URL we want to point to. 
     101            url = None 
     102            if middle.startswith('http://') or middle.startswith('https://'): 
     103                url = urlquote(middle, safe='/&=:;#?+*') 
     104            elif middle.startswith('www.') or ('@' not in middle and \ 
    103105                    len(middle) > 0 and middle[0] in string.ascii_letters + string.digits and \ 
    104106                    (middle.endswith('.org') or middle.endswith('.net') or middle.endswith('.com'))): 
    105                 middle = 'http://%s' % middle 
    106             if middle.startswith('http://') or middle.startswith('https://'): 
    107                 url = urlquote(middle, safe='/&=:;#?+*') 
    108                 if autoescape and not safe_input: 
    109                     url = escape(url) 
    110                 trimmed_url = trim_url(middle) 
    111                 middle = '<a href="%s"%s>%s</a>' % (url, nofollow_attr, 
    112                         trimmed_url) 
    113             elif '@' in middle and not middle.startswith('www.') and \ 
    114                       not ':' in middle and simple_email_re.match(middle): 
    115                 if autoescape: 
    116                     middle = conditional_escape(middle) 
    117                 middle = '<a href="mailto:%s">%s</a>' % (middle, middle) 
    118             if lead + middle + trail != word: 
     107                url = urlquote('http://%s' % middle, safe='/&=:;#?+*') 
     108            elif '@' in middle and not ':' in middle and simple_email_re.match(middle): 
     109                url = 'mailto:%s' % middle 
     110                nofollow_attr = '' 
     111            # Make link. 
     112            if url: 
     113                trimmed = trim_url(middle) 
    119114                if autoescape and not safe_input: 
    120115                    lead, trail = escape(lead), escape(trail) 
     116                    url, trimmed = escape(url), escape(trimmed) 
     117                middle = '<a href="%s"%s>%s</a>' % (url, nofollow_attr, trimmed) 
    121118                words[i] = mark_safe('%s%s%s' % (lead, middle, trail)) 
    122             elif autoescape and not safe_input: 
    123                 words[i] = escape(word) 
     119            else: 
     120                if safe_input: 
     121                    words[i] = mark_safe(word) 
     122                elif autoescape: 
     123                    words[i] = escape(word) 
    124124        elif safe_input: 
    125125            words[i] = mark_safe(word) 
  • django/trunk/tests/regressiontests/defaultfilters/tests.py

    r7701 r7755  
    151151 
    152152>>> urlizetrunc(u'http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=', 20) 
    153 u'<a href="http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=" rel="nofollow">http://www.google....</a>' 
     153u'<a href="http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=" rel="nofollow">http://www.google...</a>' 
    154154 
    155155>>> urlizetrunc('http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=', 20) 
     
    175175 
    176176>>> urlize('www.google.com')  
    177 u'<a href="http://www.google.com" rel="nofollow">http://www.google.com</a>' 
     177u'<a href="http://www.google.com" rel="nofollow">www.google.com</a>' 
    178178 
    179179>>> urlize('djangoproject.org')  
    180 u'<a href="http://djangoproject.org" rel="nofollow">http://djangoproject.org</a>' 
     180u'<a href="http://djangoproject.org" rel="nofollow">djangoproject.org</a>' 
    181181 
    182182>>> urlize('info@djangoproject.org')