Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
jezdez committed Jul 29, 2011
1 parent 0fbadfd commit 343b4f1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
6 changes: 5 additions & 1 deletion django/template/defaultfilters.py
Expand Up @@ -19,6 +19,7 @@
from django.utils.safestring import mark_safe, SafeData, mark_for_escaping from django.utils.safestring import mark_safe, SafeData, mark_for_escaping
from django.utils.timesince import timesince, timeuntil from django.utils.timesince import timesince, timeuntil
from django.utils.translation import ugettext, ungettext from django.utils.translation import ugettext, ungettext
from django.utils.text import normalize_newlines


register = Library() register = Library()


Expand Down Expand Up @@ -421,13 +422,16 @@ def linebreaks_filter(value, autoescape=None):
return mark_safe(linebreaks(value, autoescape)) return mark_safe(linebreaks(value, autoescape))
linebreaks_filter.is_safe = True linebreaks_filter.is_safe = True
linebreaks_filter.needs_autoescape = True linebreaks_filter.needs_autoescape = True
linebreaks = stringfilter(linebreaks)


def linebreaksbr(value, autoescape=None): def linebreaksbr(value, autoescape=None):
""" """
Converts all newlines in a piece of plain text to HTML line breaks Converts all newlines in a piece of plain text to HTML line breaks
(``<br />``). (``<br />``).
""" """
if autoescape and not isinstance(value, SafeData): autoescape = autoescape and not isinstance(value, SafeData)
value = normalize_newlines(value)
if autoescape:
value = escape(value) value = escape(value)
return mark_safe(value.replace('\n', '<br />')) return mark_safe(value.replace('\n', '<br />'))
linebreaksbr.is_safe = True linebreaksbr.is_safe = True
Expand Down
3 changes: 2 additions & 1 deletion django/utils/html.py
Expand Up @@ -7,6 +7,7 @@
from django.utils.encoding import force_unicode from django.utils.encoding import force_unicode
from django.utils.functional import allow_lazy from django.utils.functional import allow_lazy
from django.utils.http import urlquote from django.utils.http import urlquote
from django.utils.text import normalize_newlines


# Configuration for urlize() function. # Configuration for urlize() function.
LEADING_PUNCTUATION = ['(', '<', '&lt;'] LEADING_PUNCTUATION = ['(', '<', '&lt;']
Expand Down Expand Up @@ -70,7 +71,7 @@ def conditional_escape(html):


def linebreaks(value, autoescape=False): def linebreaks(value, autoescape=False):
"""Converts newlines into <p> and <br />s.""" """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) paras = re.split('\n{2,}', value)
if autoescape: if autoescape:
paras = [u'<p>%s</p>' % escape(p).replace('\n', '<br />') for p in paras] paras = [u'<p>%s</p>' % escape(p).replace('\n', '<br />') for p in paras]
Expand Down
12 changes: 12 additions & 0 deletions tests/regressiontests/defaultfilters/tests.py
Expand Up @@ -266,6 +266,18 @@ def test_linebreaks(self):
self.assertEqual(linebreaks(u'line 1'), u'<p>line 1</p>') self.assertEqual(linebreaks(u'line 1'), u'<p>line 1</p>')
self.assertEqual(linebreaks(u'line 1\nline 2'), self.assertEqual(linebreaks(u'line 1\nline 2'),
u'<p>line 1<br />line 2</p>') 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): def test_removetags(self):
self.assertEqual(removetags(u'some <b>html</b> with <script>alert'\ self.assertEqual(removetags(u'some <b>html</b> with <script>alert'\
Expand Down

0 comments on commit 343b4f1

Please sign in to comment.