Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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 malcolmt authored
16 django/utils/html.py
View
@@ -53,16 +53,18 @@ def fix_ampersands(value):
def urlize(text, trim_url_limit=None, nofollow=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.
+ 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.
- If trim_url_limit is not None, the URLs in link text will be limited to
- trim_url_limit characters.
+ 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 nofollow is True, the URLs in link text will get a rel="nofollow"
+ attribute.
"""
- trim_url = lambda x, limit=trim_url_limit: limit is not None and (x[:limit] + (len(x) >=limit and '...' or '')) 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
words = word_split_re.split(text)
nofollow_attr = nofollow and ' rel="nofollow"' or ''
for i, word in enumerate(words):
3  docs/templates.txt
View
@@ -1266,7 +1266,8 @@ Converts URLs in plain text into clickable links.
urlizetrunc
~~~~~~~~~~~
-Converts URLs into clickable links, truncating URLs to the given character limit.
+Converts URLs into clickable links, truncating URLs longer than the given
+character limit.
**Argument:** Length to truncate URLs to
13 tests/regressiontests/defaultfilters/tests.py
View
@@ -121,7 +121,18 @@
'<a href="http://short.com/" rel="nofollow">http://short.com/</a>'
>>> urlizetrunc('http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=', 20)
-'<a href="http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=" rel="nofollow">http://www.google.co...</a>'
+'<a href="http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=" rel="nofollow">http://www.google...</a>'
+
+# Check truncating of URIs which are the exact length
+>>> uri = 'http://31characteruri.com/test/'
+>>> len(uri)
+31
+>>> urlizetrunc(uri, 31)
+'<a href="http://31characteruri.com/test/" rel="nofollow">http://31characteruri.com/test/</a>'
+>>> urlizetrunc(uri, 30)
+'<a href="http://31characteruri.com/test/" rel="nofollow">http://31characteruri.com/t...</a>'
+>>> urlizetrunc(uri, 2)
+'<a href="http://31characteruri.com/test/" rel="nofollow">...</a>'
>>> wordcount('')
0
Please sign in to comment.
Something went wrong with that request. Please try again.