| 1 | """ |
| 2 | tests for django.utils.html |
| 3 | """ |
| 4 | |
| 5 | from unittest import TestCase |
| 6 | from django.utils import html |
| 7 | |
| 8 | |
| 9 | class TestUtils(TestCase): |
| 10 | |
| 11 | def test_escape(self): |
| 12 | for token, replacement in (('&','&'), ('<', '<'), ('>', '>'), ('"', '"'), ("'", ''')): |
| 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('<&'), '<&') |
| 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", "b", "&a;", "& &x; ", "asdf"): |
| 54 | self.assertEqual(f(x), x) |
| 55 | self.assertEqual(f("&" + x), "&" + x) |
| 56 | self.assertEqual(f("&%s&" % x), "&" + x + "&") |