Ticket #19251: 19251-1.diff

File 19251-1.diff, 2.3 KB (added by Claude Paroz, 11 years ago)

Normalize newlines from Textarea

  • django/forms/widgets.py

    diff --git a/django/forms/widgets.py b/django/forms/widgets.py
    index 061988c..c4a2792 100644
    a b from django.utils.html import conditional_escape, format_html, format_html_join  
    1919from django.utils.translation import ugettext, ugettext_lazy
    2020from django.utils.encoding import force_text, python_2_unicode_compatible
    2121from django.utils.safestring import mark_safe
     22from django.utils.text import normalize_newlines
    2223from django.utils import datetime_safe, formats, six
    2324
    2425__all__ = (
    class ClearableFileInput(FileInput):  
    392393            return False
    393394        return upload
    394395
     396
    395397class Textarea(Widget):
    396398    def __init__(self, attrs=None):
    397399        # The 'rows' and 'cols' attributes are required for HTML correctness.
    class Textarea(Widget):  
    407409                           flatatt(final_attrs),
    408410                           force_text(value))
    409411
     412    def value_from_datadict(self, data, files, name):
     413        value = super(Textarea, self).value_from_datadict(data, files, name)
     414        return normalize_newlines(value)
     415
    410416
    411417class DateInput(TextInput):
    412418    def __init__(self, attrs=None, format=None):
  • tests/regressiontests/forms/tests/widgets.py

    diff --git a/tests/regressiontests/forms/tests/widgets.py b/tests/regressiontests/forms/tests/widgets.py
    index 88469a7..9e1690a 100644
    a b class FormsWidgetTestCase(TestCase):  
    183183
    184184        self.assertHTMLEqual(w.render('msg', 'ŠĐĆŽćžšđ', attrs={'class': 'fun'}), '<textarea rows="10" cols="40" name="msg" class="fun">\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111</textarea>')
    185185
     186        # Test that newlines are normalized to LF
     187        self.assertEqual(w.value_from_datadict({'test': "Content\r\nwith\r\nCRLF"}, {}, 'test'),
     188            "Content\nwith\nCRLF")
     189        self.assertEqual(w.value_from_datadict({'test': "Content\rwith\rCR"}, {}, 'test'),
     190            "Content\nwith\nCR")
     191        self.assertEqual(w.value_from_datadict({'test': "Content\nwith\nLF"}, {}, 'test'),
     192            "Content\nwith\nLF")
     193
    186194    def test_checkboxinput(self):
    187195        w = CheckboxInput()
    188196        self.assertHTMLEqual(w.render('is_cool', ''), '<input type="checkbox" name="is_cool" />')
Back to Top