1. import os
    
  2. from datetime import datetime
    
  3. 
    
  4. from django.test import SimpleTestCase
    
  5. from django.utils.functional import lazystr
    
  6. from django.utils.html import (
    
  7.     conditional_escape,
    
  8.     escape,
    
  9.     escapejs,
    
  10.     format_html,
    
  11.     html_safe,
    
  12.     json_script,
    
  13.     linebreaks,
    
  14.     smart_urlquote,
    
  15.     strip_spaces_between_tags,
    
  16.     strip_tags,
    
  17.     urlize,
    
  18. )
    
  19. from django.utils.safestring import mark_safe
    
  20. 
    
  21. 
    
  22. class TestUtilsHtml(SimpleTestCase):
    
  23.     def check_output(self, function, value, output=None):
    
  24.         """
    
  25.         function(value) equals output. If output is None, function(value)
    
  26.         equals value.
    
  27.         """
    
  28.         if output is None:
    
  29.             output = value
    
  30.         self.assertEqual(function(value), output)
    
  31. 
    
  32.     def test_escape(self):
    
  33.         items = (
    
  34.             ("&", "&"),
    
  35.             ("<", "&lt;"),
    
  36.             (">", "&gt;"),
    
  37.             ('"', "&quot;"),
    
  38.             ("'", "&#x27;"),
    
  39.         )
    
  40.         # Substitution patterns for testing the above items.
    
  41.         patterns = ("%s", "asdf%sfdsa", "%s1", "1%sb")
    
  42.         for value, output in items:
    
  43.             with self.subTest(value=value, output=output):
    
  44.                 for pattern in patterns:
    
  45.                     with self.subTest(value=value, output=output, pattern=pattern):
    
  46.                         self.check_output(escape, pattern % value, pattern % output)
    
  47.                         self.check_output(
    
  48.                             escape, lazystr(pattern % value), pattern % output
    
  49.                         )
    
  50.                 # Check repeated values.
    
  51.                 self.check_output(escape, value * 2, output * 2)
    
  52.         # Verify it doesn't double replace &.
    
  53.         self.check_output(escape, "<&", "&lt;&amp;")
    
  54. 
    
  55.     def test_format_html(self):
    
  56.         self.assertEqual(
    
  57.             format_html(
    
  58.                 "{} {} {third} {fourth}",
    
  59.                 "< Dangerous >",
    
  60.                 mark_safe("<b>safe</b>"),
    
  61.                 third="< dangerous again",
    
  62.                 fourth=mark_safe("<i>safe again</i>"),
    
  63.             ),
    
  64.             "&lt; Dangerous &gt; <b>safe</b> &lt; dangerous again <i>safe again</i>",
    
  65.         )
    
  66. 
    
  67.     def test_linebreaks(self):
    
  68.         items = (
    
  69.             ("para1\n\npara2\r\rpara3", "<p>para1</p>\n\n<p>para2</p>\n\n<p>para3</p>"),
    
  70.             (
    
  71.                 "para1\nsub1\rsub2\n\npara2",
    
  72.                 "<p>para1<br>sub1<br>sub2</p>\n\n<p>para2</p>",
    
  73.             ),
    
  74.             (
    
  75.                 "para1\r\n\r\npara2\rsub1\r\rpara4",
    
  76.                 "<p>para1</p>\n\n<p>para2<br>sub1</p>\n\n<p>para4</p>",
    
  77.             ),
    
  78.             ("para1\tmore\n\npara2", "<p>para1\tmore</p>\n\n<p>para2</p>"),
    
  79.         )
    
  80.         for value, output in items:
    
  81.             with self.subTest(value=value, output=output):
    
  82.                 self.check_output(linebreaks, value, output)
    
  83.                 self.check_output(linebreaks, lazystr(value), output)
    
  84. 
    
  85.     def test_strip_tags(self):
    
  86.         items = (
    
  87.             (
    
  88.                 "<p>See: &#39;&eacute; is an apostrophe followed by e acute</p>",
    
  89.                 "See: &#39;&eacute; is an apostrophe followed by e acute",
    
  90.             ),
    
  91.             (
    
  92.                 "<p>See: &#x27;&eacute; is an apostrophe followed by e acute</p>",
    
  93.                 "See: &#x27;&eacute; is an apostrophe followed by e acute",
    
  94.             ),
    
  95.             ("<adf>a", "a"),
    
  96.             ("</adf>a", "a"),
    
  97.             ("<asdf><asdf>e", "e"),
    
  98.             ("hi, <f x", "hi, <f x"),
    
  99.             ("234<235, right?", "234<235, right?"),
    
  100.             ("a4<a5 right?", "a4<a5 right?"),
    
  101.             ("b7>b2!", "b7>b2!"),
    
  102.             ("</fe", "</fe"),
    
  103.             ("<x>b<y>", "b"),
    
  104.             ("a<p onclick=\"alert('<test>')\">b</p>c", "abc"),
    
  105.             ("a<p a >b</p>c", "abc"),
    
  106.             ("d<a:b c:d>e</p>f", "def"),
    
  107.             ('<strong>foo</strong><a href="http://example.com">bar</a>', "foobar"),
    
  108.             # caused infinite loop on Pythons not patched with
    
  109.             # https://bugs.python.org/issue20288
    
  110.             ("&gotcha&#;<>", "&gotcha&#;<>"),
    
  111.             ("<sc<!-- -->ript>test<<!-- -->/script>", "ript>test"),
    
  112.             ("<script>alert()</script>&h", "alert()h"),
    
  113.             ("><!" + ("&" * 16000) + "D", "><!" + ("&" * 16000) + "D"),
    
  114.             ("X<<<<br>br>br>br>X", "XX"),
    
  115.         )
    
  116.         for value, output in items:
    
  117.             with self.subTest(value=value, output=output):
    
  118.                 self.check_output(strip_tags, value, output)
    
  119.                 self.check_output(strip_tags, lazystr(value), output)
    
  120. 
    
  121.     def test_strip_tags_files(self):
    
  122.         # Test with more lengthy content (also catching performance regressions)
    
  123.         for filename in ("strip_tags1.html", "strip_tags2.txt"):
    
  124.             with self.subTest(filename=filename):
    
  125.                 path = os.path.join(os.path.dirname(__file__), "files", filename)
    
  126.                 with open(path) as fp:
    
  127.                     content = fp.read()
    
  128.                     start = datetime.now()
    
  129.                     stripped = strip_tags(content)
    
  130.                     elapsed = datetime.now() - start
    
  131.                 self.assertEqual(elapsed.seconds, 0)
    
  132.                 self.assertIn("Test string that has not been stripped.", stripped)
    
  133.                 self.assertNotIn("<", stripped)
    
  134. 
    
  135.     def test_strip_spaces_between_tags(self):
    
  136.         # Strings that should come out untouched.
    
  137.         items = (" <adf>", "<adf> ", " </adf> ", " <f> x</f>")
    
  138.         for value in items:
    
  139.             with self.subTest(value=value):
    
  140.                 self.check_output(strip_spaces_between_tags, value)
    
  141.                 self.check_output(strip_spaces_between_tags, lazystr(value))
    
  142. 
    
  143.         # Strings that have spaces to strip.
    
  144.         items = (
    
  145.             ("<d> </d>", "<d></d>"),
    
  146.             ("<p>hello </p>\n<p> world</p>", "<p>hello </p><p> world</p>"),
    
  147.             ("\n<p>\t</p>\n<p> </p>\n", "\n<p></p><p></p>\n"),
    
  148.         )
    
  149.         for value, output in items:
    
  150.             with self.subTest(value=value, output=output):
    
  151.                 self.check_output(strip_spaces_between_tags, value, output)
    
  152.                 self.check_output(strip_spaces_between_tags, lazystr(value), output)
    
  153. 
    
  154.     def test_escapejs(self):
    
  155.         items = (
    
  156.             (
    
  157.                 "\"double quotes\" and 'single quotes'",
    
  158.                 "\\u0022double quotes\\u0022 and \\u0027single quotes\\u0027",
    
  159.             ),
    
  160.             (r"\ : backslashes, too", "\\u005C : backslashes, too"),
    
  161.             (
    
  162.                 "and lots of whitespace: \r\n\t\v\f\b",
    
  163.                 "and lots of whitespace: \\u000D\\u000A\\u0009\\u000B\\u000C\\u0008",
    
  164.             ),
    
  165.             (
    
  166.                 r"<script>and this</script>",
    
  167.                 "\\u003Cscript\\u003Eand this\\u003C/script\\u003E",
    
  168.             ),
    
  169.             (
    
  170.                 "paragraph separator:\u2029and line separator:\u2028",
    
  171.                 "paragraph separator:\\u2029and line separator:\\u2028",
    
  172.             ),
    
  173.             ("`", "\\u0060"),
    
  174.         )
    
  175.         for value, output in items:
    
  176.             with self.subTest(value=value, output=output):
    
  177.                 self.check_output(escapejs, value, output)
    
  178.                 self.check_output(escapejs, lazystr(value), output)
    
  179. 
    
  180.     def test_json_script(self):
    
  181.         tests = (
    
  182.             # "<", ">" and "&" are quoted inside JSON strings
    
  183.             (
    
  184.                 (
    
  185.                     "&<>",
    
  186.                     '<script id="test_id" type="application/json">'
    
  187.                     '"\\u0026\\u003C\\u003E"</script>',
    
  188.                 )
    
  189.             ),
    
  190.             # "<", ">" and "&" are quoted inside JSON objects
    
  191.             (
    
  192.                 {"a": "<script>test&ing</script>"},
    
  193.                 '<script id="test_id" type="application/json">'
    
  194.                 '{"a": "\\u003Cscript\\u003Etest\\u0026ing\\u003C/script\\u003E"}'
    
  195.                 "</script>",
    
  196.             ),
    
  197.             # Lazy strings are quoted
    
  198.             (
    
  199.                 lazystr("&<>"),
    
  200.                 '<script id="test_id" type="application/json">"\\u0026\\u003C\\u003E"'
    
  201.                 "</script>",
    
  202.             ),
    
  203.             (
    
  204.                 {"a": lazystr("<script>test&ing</script>")},
    
  205.                 '<script id="test_id" type="application/json">'
    
  206.                 '{"a": "\\u003Cscript\\u003Etest\\u0026ing\\u003C/script\\u003E"}'
    
  207.                 "</script>",
    
  208.             ),
    
  209.         )
    
  210.         for arg, expected in tests:
    
  211.             with self.subTest(arg=arg):
    
  212.                 self.assertEqual(json_script(arg, "test_id"), expected)
    
  213. 
    
  214.     def test_json_script_without_id(self):
    
  215.         self.assertHTMLEqual(
    
  216.             json_script({"key": "value"}),
    
  217.             '<script type="application/json">{"key": "value"}</script>',
    
  218.         )
    
  219. 
    
  220.     def test_smart_urlquote(self):
    
  221.         items = (
    
  222.             ("http://öäü.com/", "http://xn--4ca9at.com/"),
    
  223.             ("http://öäü.com/öäü/", "http://xn--4ca9at.com/%C3%B6%C3%A4%C3%BC/"),
    
  224.             # Everything unsafe is quoted, !*'();:@&=+$,/?#[]~ is considered
    
  225.             # safe as per RFC.
    
  226.             (
    
  227.                 "http://example.com/path/öäü/",
    
  228.                 "http://example.com/path/%C3%B6%C3%A4%C3%BC/",
    
  229.             ),
    
  230.             ("http://example.com/%C3%B6/ä/", "http://example.com/%C3%B6/%C3%A4/"),
    
  231.             ("http://example.com/?x=1&y=2+3&z=", "http://example.com/?x=1&y=2+3&z="),
    
  232.             ("http://example.com/?x=<>\"'", "http://example.com/?x=%3C%3E%22%27"),
    
  233.             (
    
  234.                 "http://example.com/?q=http://example.com/?x=1%26q=django",
    
  235.                 "http://example.com/?q=http%3A%2F%2Fexample.com%2F%3Fx%3D1%26q%3D"
    
  236.                 "django",
    
  237.             ),
    
  238.             (
    
  239.                 "http://example.com/?q=http%3A%2F%2Fexample.com%2F%3Fx%3D1%26q%3D"
    
  240.                 "django",
    
  241.                 "http://example.com/?q=http%3A%2F%2Fexample.com%2F%3Fx%3D1%26q%3D"
    
  242.                 "django",
    
  243.             ),
    
  244.             ("http://.www.f oo.bar/", "http://.www.f%20oo.bar/"),
    
  245.         )
    
  246.         # IDNs are properly quoted
    
  247.         for value, output in items:
    
  248.             with self.subTest(value=value, output=output):
    
  249.                 self.assertEqual(smart_urlquote(value), output)
    
  250. 
    
  251.     def test_conditional_escape(self):
    
  252.         s = "<h1>interop</h1>"
    
  253.         self.assertEqual(conditional_escape(s), "&lt;h1&gt;interop&lt;/h1&gt;")
    
  254.         self.assertEqual(conditional_escape(mark_safe(s)), s)
    
  255.         self.assertEqual(conditional_escape(lazystr(mark_safe(s))), s)
    
  256. 
    
  257.     def test_html_safe(self):
    
  258.         @html_safe
    
  259.         class HtmlClass:
    
  260.             def __str__(self):
    
  261.                 return "<h1>I'm a html class!</h1>"
    
  262. 
    
  263.         html_obj = HtmlClass()
    
  264.         self.assertTrue(hasattr(HtmlClass, "__html__"))
    
  265.         self.assertTrue(hasattr(html_obj, "__html__"))
    
  266.         self.assertEqual(str(html_obj), html_obj.__html__())
    
  267. 
    
  268.     def test_html_safe_subclass(self):
    
  269.         class BaseClass:
    
  270.             def __html__(self):
    
  271.                 # defines __html__ on its own
    
  272.                 return "some html content"
    
  273. 
    
  274.             def __str__(self):
    
  275.                 return "some non html content"
    
  276. 
    
  277.         @html_safe
    
  278.         class Subclass(BaseClass):
    
  279.             def __str__(self):
    
  280.                 # overrides __str__ and is marked as html_safe
    
  281.                 return "some html safe content"
    
  282. 
    
  283.         subclass_obj = Subclass()
    
  284.         self.assertEqual(str(subclass_obj), subclass_obj.__html__())
    
  285. 
    
  286.     def test_html_safe_defines_html_error(self):
    
  287.         msg = "can't apply @html_safe to HtmlClass because it defines __html__()."
    
  288.         with self.assertRaisesMessage(ValueError, msg):
    
  289. 
    
  290.             @html_safe
    
  291.             class HtmlClass:
    
  292.                 def __html__(self):
    
  293.                     return "<h1>I'm a html class!</h1>"
    
  294. 
    
  295.     def test_html_safe_doesnt_define_str(self):
    
  296.         msg = "can't apply @html_safe to HtmlClass because it doesn't define __str__()."
    
  297.         with self.assertRaisesMessage(ValueError, msg):
    
  298. 
    
  299.             @html_safe
    
  300.             class HtmlClass:
    
  301.                 pass
    
  302. 
    
  303.     def test_urlize(self):
    
  304.         tests = (
    
  305.             (
    
  306.                 "Search for google.com/?q=! and see.",
    
  307.                 'Search for <a href="http://google.com/?q=">google.com/?q=</a>! and '
    
  308.                 "see.",
    
  309.             ),
    
  310.             (
    
  311.                 "Search for google.com/?q=1&lt! and see.",
    
  312.                 'Search for <a href="http://google.com/?q=1%3C">google.com/?q=1&lt'
    
  313.                 "</a>! and see.",
    
  314.             ),
    
  315.             (
    
  316.                 lazystr("Search for google.com/?q=!"),
    
  317.                 'Search for <a href="http://google.com/?q=">google.com/?q=</a>!',
    
  318.             ),
    
  319.             ("[email protected]", '<a href="mailto:[email protected]">[email protected]</a>'),
    
  320.         )
    
  321.         for value, output in tests:
    
  322.             with self.subTest(value=value):
    
  323.                 self.assertEqual(urlize(value), output)
    
  324. 
    
  325.     def test_urlize_unchanged_inputs(self):
    
  326.         tests = (
    
  327.             ("a" + "@a" * 50000) + "a",  # simple_email_re catastrophic test
    
  328.             ("a" + "." * 1000000) + "a",  # trailing_punctuation catastrophic test
    
  329.             "foo@",
    
  330.             "@foo.com",
    
  331.             "[email protected]",
    
  332.             "foo@localhost",
    
  333.             "foo@localhost.",
    
  334.         )
    
  335.         for value in tests:
    
  336.             with self.subTest(value=value):
    
  337.                 self.assertEqual(urlize(value), value)