Fixed #33236 -- Fixed assertHTMLEqual() error messages for escaped HTML.

This commit is contained in:
Pratyush Mittal 2021-10-28 23:45:01 +05:30 committed by Mariusz Felisiak
parent 073b7b5915
commit f38458fe56
2 changed files with 25 additions and 3 deletions

View File

@ -1,5 +1,5 @@
"""Compare two HTML documents.""" """Compare two HTML documents."""
import html
from html.parser import HTMLParser from html.parser import HTMLParser
from django.utils.regex_helper import _lazy_re_compile from django.utils.regex_helper import _lazy_re_compile
@ -150,7 +150,10 @@ class Element:
output += ' %s' % key output += ' %s' % key
if self.children: if self.children:
output += '>\n' 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 output += '\n</%s>' % self.name
else: else:
output += '>' output += '>'
@ -165,7 +168,10 @@ class RootElement(Element):
super().__init__(None, ()) super().__init__(None, ())
def __str__(self): 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): class HTMLParseError(Exception):

View File

@ -868,6 +868,11 @@ class HTMLEqualTests(SimpleTestCase):
dom2 = parse_html('<a><b/><b/></a><b/><b/>') dom2 = parse_html('<a><b/><b/></a><b/><b/>')
self.assertEqual(dom2.count(dom1), 2) self.assertEqual(dom2.count(dom1), 2)
def test_root_element_escaped_html(self):
html = '&lt;br&gt;'
parsed = parse_html(html)
self.assertEqual(str(parsed), html)
def test_parsing_errors(self): def test_parsing_errors(self):
with self.assertRaises(AssertionError): with self.assertRaises(AssertionError):
self.assertHTMLEqual('<p>', '') self.assertHTMLEqual('<p>', '')
@ -882,6 +887,17 @@ class HTMLEqualTests(SimpleTestCase):
with self.assertRaises(HTMLParseError): with self.assertRaises(HTMLParseError):
parse_html('</p>') parse_html('</p>')
def test_escaped_html_errors(self):
msg = (
'<p>\n<foo>\n</p>'
' != '
'<p>\n&lt;foo&gt;\n</p>\n'
)
with self.assertRaisesMessage(AssertionError, msg):
self.assertHTMLEqual('<p><foo></p>', '<p>&lt;foo&gt;</p>')
with self.assertRaisesMessage(AssertionError, msg):
self.assertHTMLEqual('<p><foo></p>', '<p>&#60;foo&#62;</p>')
def test_contains_html(self): def test_contains_html(self):
response = HttpResponse('''<body> response = HttpResponse('''<body>
This is a form: <form method="get"> This is a form: <form method="get">