From 68e5565dd1be82b0f3e981abd8b5419f9d8258b8 Mon Sep 17 00:00:00 2001 From: Leonard Richardson Date: Mon, 11 Nov 2019 13:51:41 -0500 Subject: The html.parser tree builder now correctly handles DOCTYPEs that are not uppercase. [bug=1848401] --- bs4/testing.py | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) (limited to 'bs4/testing.py') diff --git a/bs4/testing.py b/bs4/testing.py index 9f12e8d..a162778 100644 --- a/bs4/testing.py +++ b/bs4/testing.py @@ -250,18 +250,21 @@ class HTMLTreeBuilderSmokeTest(object): doctype = soup.contents[0] self.assertEqual(doctype.__class__, Doctype) self.assertEqual(doctype, doctype_fragment) - self.assertEqual(str(soup)[:len(doctype_str)], doctype_str) + self.assertEqual( + soup.encode("utf8")[:len(doctype_str)], + doctype_str + ) # Make sure that the doctype was correctly associated with the # parse tree and that the rest of the document parsed. self.assertEqual(soup.p.contents[0], 'foo') - def _document_with_doctype(self, doctype_fragment): + def _document_with_doctype(self, doctype_fragment, doctype_string="DOCTYPE"): """Generate and parse a document with the given doctype.""" - doctype = '' % doctype_fragment + doctype = '' % (doctype_string, doctype_fragment) markup = doctype + '\n

foo

' soup = self.soup(markup) - return doctype, soup + return doctype.encode("utf8"), soup def test_normal_doctypes(self): """Make sure normal, everyday HTML doctypes are handled correctly.""" @@ -274,6 +277,27 @@ class HTMLTreeBuilderSmokeTest(object): doctype = soup.contents[0] self.assertEqual("", doctype.strip()) + def test_mixed_case_doctype(self): + # A lowercase or mixed-case doctype becomes a Doctype. + for doctype_fragment in ("doctype", "DocType"): + doctype_str, soup = self._document_with_doctype( + "html", doctype_fragment + ) + + # Make sure a Doctype object was created and that the DOCTYPE + # is uppercase. + doctype = soup.contents[0] + self.assertEqual(doctype.__class__, Doctype) + self.assertEqual(doctype, "html") + self.assertEqual( + soup.encode("utf8")[:len(doctype_str)], + b"" + ) + + # Make sure that the doctype was correctly associated with the + # parse tree and that the rest of the document parsed. + self.assertEqual(soup.p.contents[0], 'foo') + def test_public_doctype_with_url(self): doctype = 'html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"' self.assertDoctypeHandled(doctype) @@ -828,7 +852,7 @@ class XMLTreeBuilderSmokeTest(object): soup = self.soup(markup) self.assertEqual( soup.encode("utf-8"), markup) - + def test_nested_namespaces(self): doc = b""" -- cgit v1.2.3