Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #4657 -- Fixed an error in an edge case of the urlizetrunc filter.

Thanks, SmileyChris.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@5513 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 284c6ba44b2285904f4ad4ad90d0c123b888c906 1 parent 880e3cf
Malcolm Tredinnick authored June 23, 2007
16  django/utils/html.py
@@ -53,16 +53,18 @@ def fix_ampersands(value):
53 53
 
54 54
 def urlize(text, trim_url_limit=None, nofollow=False):
55 55
     """
56  
-    Converts any URLs in text into clickable links. Works on http://, https:// and
57  
-    www. links. Links can have trailing punctuation (periods, commas, close-parens)
58  
-    and leading punctuation (opening parens) and it'll still do the right thing.
  56
+    Converts any URLs in text into clickable links. Works on http://, https://
  57
+    and www. links. Links can have trailing punctuation (periods, commas,
  58
+    close-parens) and leading punctuation (opening parens) and it'll still do
  59
+    the right thing.
59 60
 
60  
-    If trim_url_limit is not None, the URLs in link text will be limited to
61  
-    trim_url_limit characters.
  61
+    If trim_url_limit is not None, the URLs in link text longer than this limit
  62
+    will truncated to trim_url_limit-3 characters and appended with an elipsis.
62 63
 
63  
-    If nofollow is True, the URLs in link text will get a rel="nofollow" attribute.
  64
+    If nofollow is True, the URLs in link text will get a rel="nofollow"
  65
+    attribute.
64 66
     """
65  
-    trim_url = lambda x, limit=trim_url_limit: limit is not None and (x[:limit] + (len(x) >=limit and '...' or ''))  or x
  67
+    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
66 68
     words = word_split_re.split(text)
67 69
     nofollow_attr = nofollow and ' rel="nofollow"' or ''
68 70
     for i, word in enumerate(words):
3  docs/templates.txt
@@ -1266,7 +1266,8 @@ Converts URLs in plain text into clickable links.
1266 1266
 urlizetrunc
1267 1267
 ~~~~~~~~~~~
1268 1268
 
1269  
-Converts URLs into clickable links, truncating URLs to the given character limit.
  1269
+Converts URLs into clickable links, truncating URLs longer than the given
  1270
+character limit.
1270 1271
 
1271 1272
 **Argument:** Length to truncate URLs to
1272 1273
 
13  tests/regressiontests/defaultfilters/tests.py
@@ -121,7 +121,18 @@
121 121
 '<a href="http://short.com/" rel="nofollow">http://short.com/</a>'
122 122
 
123 123
 >>> urlizetrunc('http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=', 20)
124  
-'<a href="http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=" rel="nofollow">http://www.google.co...</a>'
  124
+'<a href="http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=" rel="nofollow">http://www.google...</a>'
  125
+
  126
+# Check truncating of URIs which are the exact length
  127
+>>> uri = 'http://31characteruri.com/test/'
  128
+>>> len(uri)
  129
+31
  130
+>>> urlizetrunc(uri, 31)
  131
+'<a href="http://31characteruri.com/test/" rel="nofollow">http://31characteruri.com/test/</a>'
  132
+>>> urlizetrunc(uri, 30)
  133
+'<a href="http://31characteruri.com/test/" rel="nofollow">http://31characteruri.com/t...</a>'
  134
+>>> urlizetrunc(uri, 2)
  135
+'<a href="http://31characteruri.com/test/" rel="nofollow">...</a>'
125 136
 
126 137
 >>> wordcount('')
127 138
 0

0 notes on commit 284c6ba

Please sign in to comment.
Something went wrong with that request. Please try again.