summaryrefslogtreecommitdiff
path: root/bs4/testing.py
diff options
context:
space:
mode:
authorLeonard Richardson <leonardr@segfault.org>2020-05-30 15:03:49 -0400
committerLeonard Richardson <leonardr@segfault.org>2020-05-30 15:03:49 -0400
commitcd2a59fbd92fd78cde913b19bc14c2fda6122750 (patch)
treecaeee46a372e7f1e23c6d9630407ea18e4aab012 /bs4/testing.py
parentb80c4201fc48617742c49411278b1853fb779a35 (diff)
Fixed a bug that caused too many tags to be popped from the tag
stack during tree building, when encountering a closing tag that had no matching opening tag. [bug=1880420]
Diffstat (limited to 'bs4/testing.py')
-rw-r--r--bs4/testing.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/bs4/testing.py b/bs4/testing.py
index 660cccb..a2f83a1 100644
--- a/bs4/testing.py
+++ b/bs4/testing.py
@@ -86,8 +86,22 @@ class SoupTest(unittest.TestCase):
if compare_parsed_to is None:
compare_parsed_to = to_parse
+ # Verify that the documents come out the same.
self.assertEqual(obj.decode(), self.document_for(compare_parsed_to))
+ # Also run some checks on the BeautifulSoup object itself:
+
+ # Verify that every tag that was opened was eventually closed.
+
+ # There are no tags in the open tag counter.
+ assert all(v==0 for v in obj.open_tag_counter.values())
+
+ # The only tag in the tag stack is the one for the root
+ # document.
+ self.assertEqual(
+ [obj.ROOT_TAG_NAME], [x.name for x in obj.tagStack]
+ )
+
def assertConnectedness(self, element):
"""Ensure that next_element and previous_element are properly
set for all descendants of the given element.
@@ -850,6 +864,16 @@ Hello, world!
data.a['foo'] = 'bar'
self.assertEqual('<a foo="bar">text</a>', data.a.decode())
+ def test_closing_tag_with_no_opening_tag(self):
+ # Without BeautifulSoup.open_tag_counter, the </span> tag will
+ # cause _popToTag to be called over and over again as we look
+ # for a <span> tag that wasn't there. The result is that 'text2'
+ # will show up outside the body of the document.
+ soup = self.soup("<body><div><p>text1</p></span>text2</div></body>")
+ self.assertEqual(
+ "<body><div><p>text1</p>text2</div></body>", soup.body.decode()
+ )
+
def test_worst_case(self):
"""Test the worst case (currently) for linking issues."""