summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJannis Leidel <jannis@leidel.info>2011-07-29 10:22:25 +0000
committerJannis Leidel <jannis@leidel.info>2011-07-29 10:22:25 +0000
commit343b4f1ea5ffc48c03138640a85a1781ccbf2964 (patch)
tree3e1307d11e633e5f63a9494ad83503e21f51d599
parent0fbadfd1c8ce30fd4c9f2857d467047d5bfac63d (diff)
Fixed #14288 -- Fixed linebreaksbr template filter to normalize newlines first. Thanks, Julien Phalip.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16573 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/template/defaultfilters.py6
-rw-r--r--django/utils/html.py3
-rw-r--r--tests/regressiontests/defaultfilters/tests.py12
3 files changed, 19 insertions, 2 deletions
diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py
index b68cc03e6d..13de9d5334 100644
--- a/django/template/defaultfilters.py
+++ b/django/template/defaultfilters.py
@@ -19,6 +19,7 @@ from django.utils.text import Truncator, wrap, phone2numeric
from django.utils.safestring import mark_safe, SafeData, mark_for_escaping
from django.utils.timesince import timesince, timeuntil
from django.utils.translation import ugettext, ungettext
+from django.utils.text import normalize_newlines
register = Library()
@@ -421,13 +422,16 @@ def linebreaks_filter(value, autoescape=None):
return mark_safe(linebreaks(value, autoescape))
linebreaks_filter.is_safe = True
linebreaks_filter.needs_autoescape = True
+linebreaks = stringfilter(linebreaks)
def linebreaksbr(value, autoescape=None):
"""
Converts all newlines in a piece of plain text to HTML line breaks
(``<br />``).
"""
- if autoescape and not isinstance(value, SafeData):
+ autoescape = autoescape and not isinstance(value, SafeData)
+ value = normalize_newlines(value)
+ if autoescape:
value = escape(value)
return mark_safe(value.replace('\n', '<br />'))
linebreaksbr.is_safe = True
diff --git a/django/utils/html.py b/django/utils/html.py
index 7fda015840..2687eb5232 100644
--- a/django/utils/html.py
+++ b/django/utils/html.py
@@ -7,6 +7,7 @@ from django.utils.safestring import SafeData, mark_safe
from django.utils.encoding import force_unicode
from django.utils.functional import allow_lazy
from django.utils.http import urlquote
+from django.utils.text import normalize_newlines
# Configuration for urlize() function.
LEADING_PUNCTUATION = ['(', '<', '&lt;']
@@ -70,7 +71,7 @@ def conditional_escape(html):
def linebreaks(value, autoescape=False):
"""Converts newlines into <p> and <br />s."""
- value = re.sub(r'\r\n|\r|\n', '\n', force_unicode(value)) # normalize newlines
+ value = normalize_newlines(value)
paras = re.split('\n{2,}', value)
if autoescape:
paras = [u'<p>%s</p>' % escape(p).replace('\n', '<br />') for p in paras]
diff --git a/tests/regressiontests/defaultfilters/tests.py b/tests/regressiontests/defaultfilters/tests.py
index 77bd2ba23e..cbd4776a50 100644
--- a/tests/regressiontests/defaultfilters/tests.py
+++ b/tests/regressiontests/defaultfilters/tests.py
@@ -266,6 +266,18 @@ class DefaultFiltersTests(TestCase):
self.assertEqual(linebreaks(u'line 1'), u'<p>line 1</p>')
self.assertEqual(linebreaks(u'line 1\nline 2'),
u'<p>line 1<br />line 2</p>')
+ self.assertEqual(linebreaks(u'line 1\rline 2'),
+ u'<p>line 1<br />line 2</p>')
+ self.assertEqual(linebreaks(u'line 1\r\nline 2'),
+ u'<p>line 1<br />line 2</p>')
+
+ def test_linebreaksbr(self):
+ self.assertEqual(linebreaksbr(u'line 1\nline 2'),
+ u'line 1<br />line 2')
+ self.assertEqual(linebreaksbr(u'line 1\rline 2'),
+ u'line 1<br />line 2')
+ self.assertEqual(linebreaksbr(u'line 1\r\nline 2'),
+ u'line 1<br />line 2')
def test_removetags(self):
self.assertEqual(removetags(u'some <b>html</b> with <script>alert'\