Ticket #4310: strip-entities.patch

File strip-entities.patch, 3.3 KB (added by (removed), 17 years ago)

fix (from above), and tests for utils.html, sans urlize and clean_html (original authors can write tests for those beasts).

  • tests/regressiontests/utils/models.py

    === added directory 'tests/regressiontests/utils'
    === added file 'tests/regressiontests/utils/__init__.py'
    === added file 'tests/regressiontests/utils/models.py'
     
     1# pointless, but needed due to the runner being special
  • tests/regressiontests/utils/tests.py

    === added file 'tests/regressiontests/utils/tests.py'
     
     1"""
     2tests for django.utils.html
     3"""
     4
     5from unittest import TestCase
     6from django.utils import html
     7
     8
     9class TestUtils(TestCase):
     10
     11    def test_escape(self):
     12        for token, replacement in (('&','&amp;'), ('<', '&lt;'), ('>', '&gt;'), ('"', '&quot;'), ("'", '&#39;')):
     13            for pattern in ("%s", "asdf%sfdsa", "%s", "%s1", "1%sb"):
     14                self.assertEqual(html.escape(pattern % token), pattern % replacement)
     15            self.assertEqual(html.escape(token *2), replacement * 2)
     16        # verify it doesn't double replace &
     17        self.assertEqual(html.escape('<&'), '&lt;&amp;')
     18
     19    def test_linebreaks(self):
     20        text = "para1\nsub1\nsub2\rsub3\n\npara2\r\n\r\npara3\r\rpara4\rsub1"
     21        self.assertEqual(html.linebreaks(text),
     22            "<p>para1<br />sub1<br />sub2<br />sub3</p>\n\n<p>para2</p>\n\n<p>para3</p>\n\n<p>para4<br />sub1</p>")
     23
     24    def test_strip_tags(self):
     25        for x, y in (('<adf>a', 'a'), ('</adf>a', 'a'),
     26            ('<asdf><asdf>e', 'e'), ('<f', '<f'), ('</fe', '</fe'),
     27            ('<x>b<y>', 'b')):
     28            self.assertEqual(html.strip_tags(x), y)
     29
     30    def test_strip_spaces_between_tags(self):
     31        f = html.strip_spaces_between_tags
     32        for data in (' <adf>', '<adf> ', ' </adf> ',
     33            ('<d> </d>', '<d></d>'), ' <f> x</f>'):
     34            if isinstance(data, basestring):
     35                data = [data, data]
     36            self.assertEqual(f(data[0]), data[1])
     37
     38    def test_strip_entities(self):
     39        f = html.strip_entities
     40        self.assertEqual(f("&"), "&")
     41        self.assertEqual(f("&a"), "&a")
     42        self.assertEqual(f("&a"), "&a")
     43        self.assertEqual(f("a&#a"), "a&#a")
     44        for x in ("#1", "#12", "a", "fdasdfasdfasdf"):
     45            entity = "&%s;" % x
     46            self.assertEqual(f("asdf %s " % entity), "asdf  ")
     47            self.assertEqual(f("%s%s" % (entity, entity)), "")
     48            self.assertEqual(f("&%s%s" % (entity, entity)), "&")
     49            self.assertEqual(f("%s3" % entity), "3")
     50
     51    def test_fix_ampersands(self):
     52        f = html.fix_ampersands
     53        for x in ("a&#1;", "b", "&a;", "&amp; &x; ", "asdf"):
     54            self.assertEqual(f(x), x)
     55            self.assertEqual(f("&" + x), "&amp;" + x)
     56            self.assertEqual(f("&%s&" % x), "&amp;" + x + "&amp;")
  • django/utils/html.py

    === modified file 'django/utils/html.py'
     
    4545
    4646def strip_entities(value):
    4747    "Returns the given HTML with all entities (&something;) stripped"
    48     return re.sub(r'&(?:\w+|#\d);', '', value)
     48    return re.sub(r'&(?:\w+|#\d+);', '', value)
    4949
    5050def fix_ampersands(value):
    5151    "Returns the given HTML with all unencoded ampersands encoded correctly"
Back to Top