diff options
| author | Pratyush Mittal <pratyushmittal@gmail.com> | 2021-10-28 23:45:01 +0530 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-10-29 12:09:23 +0200 |
| commit | f38458fe56bf8850da72a924bd2e8ff59c6adf06 (patch) | |
| tree | 5da6146a43001407d96703fbf8ff2b527e3f9e56 | |
| parent | 073b7b5915fdfb89a144e81173176ee13ff92a25 (diff) | |
Fixed #33236 -- Fixed assertHTMLEqual() error messages for escaped HTML.
| -rw-r--r-- | django/test/html.py | 12 | ||||
| -rw-r--r-- | tests/test_utils/tests.py | 16 |
2 files changed, 25 insertions, 3 deletions
diff --git a/django/test/html.py b/django/test/html.py index f1cbadfc54..07e986439b 100644 --- a/django/test/html.py +++ b/django/test/html.py @@ -1,5 +1,5 @@ """Compare two HTML documents.""" - +import html from html.parser import HTMLParser from django.utils.regex_helper import _lazy_re_compile @@ -150,7 +150,10 @@ class Element: output += ' %s' % key if self.children: output += '>\n' - output += ''.join(str(c) for c in self.children) + output += ''.join([ + html.escape(c) if isinstance(c, str) else str(c) + for c in self.children + ]) output += '\n</%s>' % self.name else: output += '>' @@ -165,7 +168,10 @@ class RootElement(Element): super().__init__(None, ()) def __str__(self): - return ''.join(str(c) for c in self.children) + return ''.join([ + html.escape(c) if isinstance(c, str) else str(c) + for c in self.children + ]) class HTMLParseError(Exception): diff --git a/tests/test_utils/tests.py b/tests/test_utils/tests.py index ce328cd90a..9c003e5a6f 100644 --- a/tests/test_utils/tests.py +++ b/tests/test_utils/tests.py @@ -868,6 +868,11 @@ class HTMLEqualTests(SimpleTestCase): dom2 = parse_html('<a><b/><b/></a><b/><b/>') self.assertEqual(dom2.count(dom1), 2) + def test_root_element_escaped_html(self): + html = '<br>' + parsed = parse_html(html) + self.assertEqual(str(parsed), html) + def test_parsing_errors(self): with self.assertRaises(AssertionError): self.assertHTMLEqual('<p>', '') @@ -882,6 +887,17 @@ class HTMLEqualTests(SimpleTestCase): with self.assertRaises(HTMLParseError): parse_html('</p>') + def test_escaped_html_errors(self): + msg = ( + '<p>\n<foo>\n</p>' + ' != ' + '<p>\n<foo>\n</p>\n' + ) + with self.assertRaisesMessage(AssertionError, msg): + self.assertHTMLEqual('<p><foo></p>', '<p><foo></p>') + with self.assertRaisesMessage(AssertionError, msg): + self.assertHTMLEqual('<p><foo></p>', '<p><foo></p>') + def test_contains_html(self): response = HttpResponse('''<body> This is a form: <form method="get"> |
