|
1 import unittest |
|
2 |
|
3 from django.utils import html |
|
4 |
|
5 class TestUtilsHtml(unittest.TestCase): |
|
6 |
|
7 def check_output(self, function, value, output=None): |
|
8 """ |
|
9 Check that function(value) equals output. If output is None, |
|
10 check that function(value) equals value. |
|
11 """ |
|
12 if output is None: |
|
13 output = value |
|
14 self.assertEqual(function(value), output) |
|
15 |
|
16 def test_escape(self): |
|
17 f = html.escape |
|
18 items = ( |
|
19 ('&','&'), |
|
20 ('<', '<'), |
|
21 ('>', '>'), |
|
22 ('"', '"'), |
|
23 ("'", '''), |
|
24 ) |
|
25 # Substitution patterns for testing the above items. |
|
26 patterns = ("%s", "asdf%sfdsa", "%s1", "1%sb") |
|
27 for value, output in items: |
|
28 for pattern in patterns: |
|
29 self.check_output(f, pattern % value, pattern % output) |
|
30 # Check repeated values. |
|
31 self.check_output(f, value * 2, output * 2) |
|
32 # Verify it doesn't double replace &. |
|
33 self.check_output(f, '<&', '<&') |
|
34 |
|
35 def test_linebreaks(self): |
|
36 f = html.linebreaks |
|
37 items = ( |
|
38 ("para1\n\npara2\r\rpara3", "<p>para1</p>\n\n<p>para2</p>\n\n<p>para3</p>"), |
|
39 ("para1\nsub1\rsub2\n\npara2", "<p>para1<br />sub1<br />sub2</p>\n\n<p>para2</p>"), |
|
40 ("para1\r\n\r\npara2\rsub1\r\rpara4", "<p>para1</p>\n\n<p>para2<br />sub1</p>\n\n<p>para4</p>"), |
|
41 ("para1\tmore\n\npara2", "<p>para1\tmore</p>\n\n<p>para2</p>"), |
|
42 ) |
|
43 for value, output in items: |
|
44 self.check_output(f, value, output) |
|
45 |
|
46 def test_strip_tags(self): |
|
47 f = html.strip_tags |
|
48 items = ( |
|
49 ('<adf>a', 'a'), |
|
50 ('</adf>a', 'a'), |
|
51 ('<asdf><asdf>e', 'e'), |
|
52 ('<f', '<f'), |
|
53 ('</fe', '</fe'), |
|
54 ('<x>b<y>', 'b'), |
|
55 ) |
|
56 for value, output in items: |
|
57 self.check_output(f, value, output) |
|
58 |
|
59 def test_strip_spaces_between_tags(self): |
|
60 f = html.strip_spaces_between_tags |
|
61 # Strings that should come out untouched. |
|
62 items = (' <adf>', '<adf> ', ' </adf> ', ' <f> x</f>') |
|
63 for value in items: |
|
64 self.check_output(f, value) |
|
65 # Strings that have spaces to strip. |
|
66 items = ( |
|
67 ('<d> </d>', '<d></d>'), |
|
68 ('<p>hello </p>\n<p> world</p>', '<p>hello </p><p> world</p>'), |
|
69 ('\n<p>\t</p>\n<p> </p>\n', '\n<p></p><p></p>\n'), |
|
70 ) |
|
71 for value, output in items: |
|
72 self.check_output(f, value, output) |
|
73 |
|
74 def test_strip_entities(self): |
|
75 f = html.strip_entities |
|
76 # Strings that should come out untouched. |
|
77 values = ("&", "&a", "&a", "a&#a") |
|
78 for value in values: |
|
79 self.check_output(f, value) |
|
80 # Valid entities that should be stripped from the patterns. |
|
81 entities = ("", "", "&a;", "&fdasdfasdfasdf;") |
|
82 patterns = ( |
|
83 ("asdf %(entity)s ", "asdf "), |
|
84 ("%(entity)s%(entity)s", ""), |
|
85 ("&%(entity)s%(entity)s", "&"), |
|
86 ("%(entity)s3", "3"), |
|
87 ) |
|
88 for entity in entities: |
|
89 for in_pattern, output in patterns: |
|
90 self.check_output(f, in_pattern % {'entity': entity}, output) |
|
91 |
|
92 def test_fix_ampersands(self): |
|
93 f = html.fix_ampersands |
|
94 # Strings without ampersands or with ampersands already encoded. |
|
95 values = ("a", "b", "&a;", "& &x; ", "asdf") |
|
96 patterns = ( |
|
97 ("%s", "%s"), |
|
98 ("&%s", "&%s"), |
|
99 ("&%s&", "&%s&"), |
|
100 ) |
|
101 for value in values: |
|
102 for in_pattern, out_pattern in patterns: |
|
103 self.check_output(f, in_pattern % value, out_pattern % value) |
|
104 # Strings with ampersands that need encoding. |
|
105 items = ( |
|
106 ("&#;", "&#;"), |
|
107 ("ͫ ;", "&#875 ;"), |
|
108 ("abc;", "&#4abc;"), |
|
109 ) |
|
110 for value, output in items: |
|
111 self.check_output(f, value, output) |