summaryrefslogtreecommitdiff
path: root/django/test/html.py
diff options
context:
space:
mode:
authorJacob Walls <jacobtylerwalls@gmail.com>2020-09-20 10:14:54 -0400
committerCarlton Gibson <carlton@noumenal.es>2020-09-24 12:52:41 +0200
commite26a7a8ef41f0d69951affb21655cdc2cf94a209 (patch)
treedfaf58c61a3b23d25e61e21148bdb948663da25d /django/test/html.py
parent01974d7f7549b2dca2a729c3c1a1ea7d4585eb3a (diff)
Fixed #27906 -- Fixed test tools counting of HTML matches for subsets of elements.
Previously examples such as '<a/><b/>' would not match in '<a/><b/><c/>'.
Diffstat (limited to 'django/test/html.py')
-rw-r--r--django/test/html.py18
1 files changed, 18 insertions, 0 deletions
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: '<a/><b/>'
+ # is contained in '<a/><b/><c/>'.
+ if isinstance(element, RootElement) and element.children:
+ elem_child = element.children[elem_child_idx]
+ # Start or continue match, advance index.
+ if elem_child == child:
+ elem_child_idx += 1
+ # Match found, reset index.
+ if elem_child_idx == len(element.children):
+ i += 1
+ elem_child_idx = 0
+ # No match, reset index.
+ else:
+ elem_child_idx = 0
return i
def __contains__(self, element):