diff --git a/django/test/html.py b/django/test/html.py
index 36b44b0466..3b04217822 100644
--- a/django/test/html.py
+++ b/django/test/html.py
@@ -86,6 +86,7 @@ class Element:
if self.children == element.children:
return 1
i = 0
+ elem_child_idx = 0
for child in self.children:
# child is text content and element is also text content, then
# make a simple "text" in "text"
@@ -96,9 +97,26 @@ class Element:
elif element in child:
return 1
else:
+ # Look for element wholly within this child.
i += child._count(element, count=count)
if not count and i:
return i
+ # Also look for a sequence of element's children among self's
+ # children. self.children == element.children is tested above,
+ # but will fail if self has additional children. Ex: ''
+ # is contained in '
foo
bar
') self.assertEqual(dom2.count(dom1), 0) - # html with a root element contains the same html with no root element + # HTML with a root element contains the same HTML with no root element. dom1 = parse_html('foo
bar
') dom2 = parse_html('foo
bar
foo
bar
foo
bar
', '')