Fixed #33236 -- Fixed assertHTMLEqual() error messages for escaped HTML.
This commit is contained in:
parent
073b7b5915
commit
f38458fe56
|
@ -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):
|
||||
|
|
|
@ -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">
|
||||
|
|
Loading…
Reference in New Issue