Author: jezdez Date: 2011-07-29 03:22:25 -0700 (Fri, 29 Jul 2011) New Revision: 16573
Modified: django/trunk/django/template/defaultfilters.py django/trunk/django/utils/html.py django/trunk/tests/regressiontests/defaultfilters/tests.py Log: Fixed #14288 -- Fixed linebreaksbr template filter to normalize newlines first. Thanks, Julien Phalip. Modified: django/trunk/django/template/defaultfilters.py =================================================================== --- django/trunk/django/template/defaultfilters.py 2011-07-29 10:20:16 UTC (rev 16572) +++ django/trunk/django/template/defaultfilters.py 2011-07-29 10:22:25 UTC (rev 16573) @@ -19,6 +19,7 @@ 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 @@ 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 Modified: django/trunk/django/utils/html.py =================================================================== --- django/trunk/django/utils/html.py 2011-07-29 10:20:16 UTC (rev 16572) +++ django/trunk/django/utils/html.py 2011-07-29 10:22:25 UTC (rev 16573) @@ -7,6 +7,7 @@ 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 = ['(', '<', '<'] @@ -70,7 +71,7 @@ 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] Modified: django/trunk/tests/regressiontests/defaultfilters/tests.py =================================================================== --- django/trunk/tests/regressiontests/defaultfilters/tests.py 2011-07-29 10:20:16 UTC (rev 16572) +++ django/trunk/tests/regressiontests/defaultfilters/tests.py 2011-07-29 10:22:25 UTC (rev 16573) @@ -266,7 +266,19 @@ 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'\ u'("You smell")</script> disallowed <img /> tags', 'script img'), -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-updates@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.