Ticket #7025: 7025.diff

File 7025.diff, 3.3 KB (added by guettli, 7 years ago)

Improved patch: Overwrite splitlines() and not split()

  • django/utils/safestring.py

     
    5555            return SafeUnicode(data)
    5656
    5757    decode = curry(_proxy_method, method = str.decode)
     58    lstrip = curry(_proxy_method, method = str.lstrip)
     59    rstrip = curry(_proxy_method, method = str.rstrip)
     60    strip = curry(_proxy_method, method = str.strip)
     61   
     62    def splitlines(self, keepends=False):
     63        """
     64        This fails on newlines in CDATA sections.
     65        """
     66        return [SafeString(part) for part in str.splitlines(self, keepends)]
    5867
    5968class SafeUnicode(unicode, SafeData):
    6069    """
     
    8594            return SafeUnicode(data)
    8695
    8796    encode = curry(_proxy_method, method = unicode.encode)
     97    lstrip = curry(_proxy_method, method = unicode.lstrip)
     98    rstrip = curry(_proxy_method, method = unicode.rstrip)
     99    strip = curry(_proxy_method, method = unicode.strip)
     100   
     101    def splitlines(self, keepends=False):
     102        """
     103        This fails on newlines in CDATA sections.
     104        """
     105        return [SafeUnicode(part) for part in unicode.splitlines(self, keepends)]
    88106
    89107def mark_safe(s):
    90108    """
  • tests/regressiontests/utils/tests.py

     
    44
    55from unittest import TestCase
    66
    7 from django.utils import html, checksums
     7from django.utils import html, checksums, safestring
    88
    99import timesince
    1010import datastructures
     
    150150        for value, output in items:
    151151            self.check_output(f, value, output)
    152152
     153class TestUtilsSafeString(TestCase):
     154
     155    def test_safestring_splitlines(self):
     156        safe = safestring.SafeString('one\ntwo\three\n')
     157        for sub in safe.splitlines():
     158            self.failUnless(isinstance(sub, safestring.SafeString), repr(sub))
     159
     160    def test_safeunicode_splitlines(self):
     161        safe = safestring.SafeUnicode(u'one\ntwo\three\n')
     162        for sub in safe.splitlines():
     163            self.failUnless(isinstance(sub, safestring.SafeUnicode), repr(sub))
     164           
     165    def test_safestring_strip(self):
     166        safe = safestring.SafeString(' \none\n ')
     167
     168        stripped = safe.strip()
     169        self.failUnless(isinstance(stripped, safestring.SafeString) and stripped=='one')
     170
     171        stripped = safe.rstrip()
     172        self.failUnless(isinstance(stripped, safestring.SafeString) and stripped==' \none')
     173
     174        stripped = safe.lstrip()
     175        self.failUnless(isinstance(stripped, safestring.SafeString) and stripped=='one\n ')
     176       
     177    def test_safeunicode_strip(self):
     178        safe = safestring.SafeUnicode(' \none\n ')
     179       
     180        stripped = safe.strip()
     181        self.failUnless(isinstance(stripped, safestring.SafeUnicode) and stripped=='one')
     182       
     183        stripped = safe.rstrip()
     184        self.failUnless(isinstance(stripped, safestring.SafeUnicode) and stripped==' \none')
     185       
     186        stripped = safe.lstrip()
     187        self.failUnless(isinstance(stripped, safestring.SafeUnicode) and stripped=='one\n ')
     188       
     189
    153190if __name__ == "__main__":
    154191    import doctest
    155192    doctest.testmod()
Back to Top