1
# -*- coding: utf-8 -*-
2
from __future__ import unicode_literals
6
from django.utils import html
8
class TestUtilsHtml(unittest.TestCase):
10
def check_output(self, function, value, output=None):
12
Check that function(value) equals output. If output is None,
13
check that function(value) equals value.
17
self.assertEqual(function(value), output)
19
def test_escape(self):
28
# Substitution patterns for testing the above items.
29
patterns = ("%s", "asdf%sfdsa", "%s1", "1%sb")
30
for value, output in items:
31
for pattern in patterns:
32
self.check_output(f, pattern % value, pattern % output)
33
# Check repeated values.
34
self.check_output(f, value * 2, output * 2)
35
# Verify it doesn't double replace &.
36
self.check_output(f, '<&', '<&')
38
def test_format_html(self):
40
html.format_html("{0} {1} {third} {fourth}",
42
html.mark_safe("<b>safe</b>"),
43
third="< dangerous again",
44
fourth=html.mark_safe("<i>safe again</i>")
46
"< Dangerous > <b>safe</b> < dangerous again <i>safe again</i>"
49
def test_linebreaks(self):
52
("para1\n\npara2\r\rpara3", "<p>para1</p>\n\n<p>para2</p>\n\n<p>para3</p>"),
53
("para1\nsub1\rsub2\n\npara2", "<p>para1<br />sub1<br />sub2</p>\n\n<p>para2</p>"),
54
("para1\r\n\r\npara2\rsub1\r\rpara4", "<p>para1</p>\n\n<p>para2<br />sub1</p>\n\n<p>para4</p>"),
55
("para1\tmore\n\npara2", "<p>para1\tmore</p>\n\n<p>para2</p>"),
57
for value, output in items:
58
self.check_output(f, value, output)
60
def test_strip_tags(self):
65
('<asdf><asdf>e', 'e'),
69
('a<p a >b</p>c', 'abc'),
70
('d<a:b c:d>e</p>f', 'def'),
72
for value, output in items:
73
self.check_output(f, value, output)
75
def test_strip_spaces_between_tags(self):
76
f = html.strip_spaces_between_tags
77
# Strings that should come out untouched.
78
items = (' <adf>', '<adf> ', ' </adf> ', ' <f> x</f>')
80
self.check_output(f, value)
81
# Strings that have spaces to strip.
83
('<d> </d>', '<d></d>'),
84
('<p>hello </p>\n<p> world</p>', '<p>hello </p><p> world</p>'),
85
('\n<p>\t</p>\n<p> </p>\n', '\n<p></p><p></p>\n'),
87
for value, output in items:
88
self.check_output(f, value, output)
90
def test_strip_entities(self):
91
f = html.strip_entities
92
# Strings that should come out untouched.
93
values = ("&", "&a", "&a", "a&#a")
95
self.check_output(f, value)
96
# Valid entities that should be stripped from the patterns.
97
entities = ("", "", "&a;", "&fdasdfasdfasdf;")
99
("asdf %(entity)s ", "asdf "),
100
("%(entity)s%(entity)s", ""),
101
("&%(entity)s%(entity)s", "&"),
102
("%(entity)s3", "3"),
104
for entity in entities:
105
for in_pattern, output in patterns:
106
self.check_output(f, in_pattern % {'entity': entity}, output)
108
def test_fix_ampersands(self):
109
f = html.fix_ampersands
110
# Strings without ampersands or with ampersands already encoded.
111
values = ("a", "b", "&a;", "& &x; ", "asdf")
115
("&%s&", "&%s&"),
118
for in_pattern, out_pattern in patterns:
119
self.check_output(f, in_pattern % value, out_pattern % value)
120
# Strings with ampersands that need encoding.
123
("ͫ ;", "&#875 ;"),
124
("abc;", "&#4abc;"),
126
for value, output in items:
127
self.check_output(f, value, output)
129
def test_escapejs(self):
132
('"double quotes" and \'single quotes\'', '\\u0022double quotes\\u0022 and \\u0027single quotes\\u0027'),
133
(r'\ : backslashes, too', '\\u005C : backslashes, too'),
134
('and lots of whitespace: \r\n\t\v\f\b', 'and lots of whitespace: \\u000D\\u000A\\u0009\\u000B\\u000C\\u0008'),
135
(r'<script>and this</script>', '\\u003Cscript\\u003Eand this\\u003C/script\\u003E'),
136
('paragraph separator:\u2029and line separator:\u2028', 'paragraph separator:\\u2029and line separator:\\u2028'),
138
for value, output in items:
139
self.check_output(f, value, output)
141
def test_clean_html(self):
144
('<p>I <i>believe</i> in <b>semantic markup</b>!</p>', '<p>I <em>believe</em> in <strong>semantic markup</strong>!</p>'),
145
('I escape & I don\'t <a href="#" target="_blank">target</a>', 'I escape & I don\'t <a href="#" >target</a>'),
146
('<p>I kill whitespace</p><br clear="all"><p> </p>', '<p>I kill whitespace</p>'),
147
# also a regression test for #7267: this used to raise an UnicodeDecodeError
148
('<p>* foo</p><p>* bar</p>', '<ul>\n<li> foo</li><li> bar</li>\n</ul>'),
150
for value, output in items:
151
self.check_output(f, value, output)
153
def test_remove_tags(self):
156
("<b><i>Yes</i></b>", "b i", "Yes"),
157
("<a>x</a> <p><b>y</b></p>", "a b", "x <p>y</p>"),
159
for value, tags, output in items:
160
self.assertEqual(f(value, tags), output)
162
def test_smart_urlquote(self):
163
quote = html.smart_urlquote
164
# Ensure that IDNs are properly quoted
165
self.assertEqual(quote('http://öäü.com/'), 'http://xn--4ca9at.com/')
166
self.assertEqual(quote('http://öäü.com/öäü/'), 'http://xn--4ca9at.com/%C3%B6%C3%A4%C3%BC/')
167
# Ensure that everything unsafe is quoted, !*'();:@&=+$,/?#[]~ is considered safe as per RFC
168
self.assertEqual(quote('http://example.com/path/öäü/'), 'http://example.com/path/%C3%B6%C3%A4%C3%BC/')
169
self.assertEqual(quote('http://example.com/%C3%B6/ä/'), 'http://example.com/%C3%B6/%C3%A4/')
170
self.assertEqual(quote('http://example.com/?x=1&y=2'), 'http://example.com/?x=1&y=2')