Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14288 -- Fixed linebreaksbr template filter to normalize newli…

…nes first. Thanks, Julien Phalip.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16573 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 343b4f1ea5ffc48c03138640a85a1781ccbf2964 1 parent 0fbadfd
Jannis Leidel authored July 29, 2011
6  django/template/defaultfilters.py
@@ -19,6 +19,7 @@
19 19
 from django.utils.safestring import mark_safe, SafeData, mark_for_escaping
20 20
 from django.utils.timesince import timesince, timeuntil
21 21
 from django.utils.translation import ugettext, ungettext
  22
+from django.utils.text import normalize_newlines
22 23
 
23 24
 register = Library()
24 25
 
@@ -421,13 +422,16 @@ def linebreaks_filter(value, autoescape=None):
421 422
     return mark_safe(linebreaks(value, autoescape))
422 423
 linebreaks_filter.is_safe = True
423 424
 linebreaks_filter.needs_autoescape = True
  425
+linebreaks = stringfilter(linebreaks)
424 426
 
425 427
 def linebreaksbr(value, autoescape=None):
426 428
     """
427 429
     Converts all newlines in a piece of plain text to HTML line breaks
428 430
     (``<br />``).
429 431
     """
430  
-    if autoescape and not isinstance(value, SafeData):
  432
+    autoescape = autoescape and not isinstance(value, SafeData)
  433
+    value = normalize_newlines(value)
  434
+    if autoescape:
431 435
         value = escape(value)
432 436
     return mark_safe(value.replace('\n', '<br />'))
433 437
 linebreaksbr.is_safe = True
3  django/utils/html.py
@@ -7,6 +7,7 @@
7 7
 from django.utils.encoding import force_unicode
8 8
 from django.utils.functional import allow_lazy
9 9
 from django.utils.http import urlquote
  10
+from django.utils.text import normalize_newlines
10 11
 
11 12
 # Configuration for urlize() function.
12 13
 LEADING_PUNCTUATION  = ['(', '<', '&lt;']
@@ -70,7 +71,7 @@ def conditional_escape(html):
70 71
 
71 72
 def linebreaks(value, autoescape=False):
72 73
     """Converts newlines into <p> and <br />s."""
73  
-    value = re.sub(r'\r\n|\r|\n', '\n', force_unicode(value)) # normalize newlines
  74
+    value = normalize_newlines(value)
74 75
     paras = re.split('\n{2,}', value)
75 76
     if autoescape:
76 77
         paras = [u'<p>%s</p>' % escape(p).replace('\n', '<br />') for p in paras]
12  tests/regressiontests/defaultfilters/tests.py
@@ -266,6 +266,18 @@ def test_linebreaks(self):
266 266
         self.assertEqual(linebreaks(u'line 1'), u'<p>line 1</p>')
267 267
         self.assertEqual(linebreaks(u'line 1\nline 2'),
268 268
                           u'<p>line 1<br />line 2</p>')
  269
+        self.assertEqual(linebreaks(u'line 1\rline 2'),
  270
+                          u'<p>line 1<br />line 2</p>')
  271
+        self.assertEqual(linebreaks(u'line 1\r\nline 2'),
  272
+                          u'<p>line 1<br />line 2</p>')
  273
+
  274
+    def test_linebreaksbr(self):
  275
+        self.assertEqual(linebreaksbr(u'line 1\nline 2'),
  276
+                          u'line 1<br />line 2')
  277
+        self.assertEqual(linebreaksbr(u'line 1\rline 2'),
  278
+                          u'line 1<br />line 2')
  279
+        self.assertEqual(linebreaksbr(u'line 1\r\nline 2'),
  280
+                          u'line 1<br />line 2')
269 281
 
270 282
     def test_removetags(self):
271 283
         self.assertEqual(removetags(u'some <b>html</b> with <script>alert'\

0 notes on commit 343b4f1

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