﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
32507	assertInHTML('<a>', '<a><b/></a>') fails when tag implicitly closed	Jacob Walls	Jacob Walls	"If the needle ('<a>') is an open tag without children, and the haystack is a closed tag with children ('<a><b/></a>'), no match is found.
Spotted by Antoine Humbert in the patch for #31867.

Something like this appears to work:

{{{
diff --git a/django/test/html.py b/django/test/html.py
index 486a0d358d..4443540a70 100644
--- a/django/test/html.py
+++ b/django/test/html.py
@@ -82,6 +82,9 @@ class Element:
             return 1
         i = 0
         elem_child_idx = 0
+        # Begin search against a childless copy, e.g. '<a>' in '<a><b/></a>'.
+        if Element(self.name, self.attributes) == element:
+            i = 1
         for child in self.children:
             # child is text content and element is also text content, then
             # make a simple ""text"" in ""text""
}}}

But it breaks the fourth assertion here from the original commit 844a24bbb97af663ebf8dbeab4499acafe105943:

{{{
    def test_contains_html(self):
        response = HttpResponse('''<body>
        This is a form: <form method=""get"">
            <input type=""text"" name=""Hello"" />
        </form></body>''')

        self.assertNotContains(response, ""<input name='Hello' type='text'>"")
        self.assertContains(response, '<form method=""get"">')

        self.assertContains(response, ""<input name='Hello' type='text'>"", html=True)
        self.assertNotContains(response, '<form method=""get"">', html=True)
}}}

I'm having trouble seeing why the fourth assertion is correct. I see from the docs for [https://docs.djangoproject.com/en/3.1/topics/testing/tools/#django.test.SimpleTestCase.assertHTMLEqual assertHTMLEqual] that ""All open tags are closed implicitly, e.g. when a surrounding tag is closed or the HTML document ends."", which is fine to insist on for equality, but for contains, I would expect the test case for this ticket to pass.

Or, if we decide not to support it, we should explain in the docs for `assertInHTML` that an implicit closing tag is being added.

"	Cleanup/optimization	closed	Documentation	dev	Normal	fixed		Antoine Humbert	Accepted	1	0	0	0	1	0
