Ticket #14288: 14288.linebreaksbr-normalized-newlines.3.diff

File 14288.linebreaksbr-normalized-newlines.3.diff, 3.2 KB (added by Julien Phalip, 13 years ago)

Patch updated to latest trunk

  • django/template/defaultfilters.py

    diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py
    index 74ae849..ca1ee44 100644
    a b from django.utils.text import truncate_words, truncate_html_words, wrap, phone2n  
    1919from django.utils.safestring import mark_safe, SafeData, mark_for_escaping
    2020from django.utils.timesince import timesince, timeuntil
    2121from django.utils.translation import ugettext, ungettext
     22from django.utils.text import normalize_newlines
    2223
    2324register = Library()
    2425
    def linebreaks_filter(value, autoescape=None):  
    407408    return mark_safe(linebreaks(value, autoescape))
    408409linebreaks_filter.is_safe = True
    409410linebreaks_filter.needs_autoescape = True
     411linebreaks = stringfilter(linebreaks)
    410412
    411413def linebreaksbr(value, autoescape=None):
    412414    """
    413415    Converts all newlines in a piece of plain text to HTML line breaks
    414416    (``<br />``).
    415417    """
    416     if autoescape and not isinstance(value, SafeData):
     418    autoescape = autoescape and not isinstance(value, SafeData)
     419    value = normalize_newlines(value)
     420    if autoescape:
    417421        value = escape(value)
    418422    return mark_safe(value.replace('\n', '<br />'))
    419423linebreaksbr.is_safe = True
  • django/utils/html.py

    diff --git a/django/utils/html.py b/django/utils/html.py
    index 7fda015..2687eb5 100644
    a b from django.utils.safestring import SafeData, mark_safe  
    77from django.utils.encoding import force_unicode
    88from django.utils.functional import allow_lazy
    99from django.utils.http import urlquote
     10from django.utils.text import normalize_newlines
    1011
    1112# Configuration for urlize() function.
    1213LEADING_PUNCTUATION  = ['(', '<', '&lt;']
    def conditional_escape(html):  
    7071
    7172def linebreaks(value, autoescape=False):
    7273    """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)
    7475    paras = re.split('\n{2,}', value)
    7576    if autoescape:
    7677        paras = [u'<p>%s</p>' % escape(p).replace('\n', '<br />') for p in paras]
  • tests/regressiontests/defaultfilters/tests.py

    diff --git a/tests/regressiontests/defaultfilters/tests.py b/tests/regressiontests/defaultfilters/tests.py
    index 77bd2ba..cbd4776 100644
    a b class DefaultFiltersTests(TestCase):  
    266266        self.assertEqual(linebreaks(u'line 1'), u'<p>line 1</p>')
    267267        self.assertEqual(linebreaks(u'line 1\nline 2'),
    268268                          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')
    269281
    270282    def test_removetags(self):
    271283        self.assertEqual(removetags(u'some <b>html</b> with <script>alert'\
Back to Top