From 15038b22ea020ea79928af2831c4b1dff869bcd4 Mon Sep 17 00:00:00 2001 From: Leonard Richardson Date: Sun, 15 Jul 2018 08:27:40 -0400 Subject: Stop data loss when encountering an empty numeric entity, and possibly in other cases. Thanks to tos.kamiya for the fix. [bug=1698503] --- NEWS.txt | 3 +++ bs4/builder/_htmlparser.py | 1 + bs4/testing.py | 2 +- bs4/tests/test_htmlparser.py | 5 +++++ 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/NEWS.txt b/NEWS.txt index 909de65..4788489 100644 --- a/NEWS.txt +++ b/NEWS.txt @@ -1,5 +1,8 @@ = Unreleased +* Stop data loss when encountering an empty numeric entity, and + possibly in other cases. Thanks to tos.kamiya for the fix. [bug=1698503] + * Improved the warning given when no parser is specified. [bug=1780571] * Fixed code that was causing deprecation warnings in recent Python 3 diff --git a/bs4/builder/_htmlparser.py b/bs4/builder/_htmlparser.py index 71604c5..ef9fd1e 100644 --- a/bs4/builder/_htmlparser.py +++ b/bs4/builder/_htmlparser.py @@ -224,6 +224,7 @@ class HTMLParserTreeBuilder(HTMLTreeBuilder): parser.soup = self.soup try: parser.feed(markup) + parser.close() except HTMLParseError, e: warnings.warn(RuntimeWarning( "Python's built-in HTMLParser cannot parse the given document. This is not a bug in Beautiful Soup. The best solution is to install an external parser (lxml or html5lib), and use Beautiful Soup with that parser. See http://www.crummy.com/software/BeautifulSoup/bs4/doc/#installing-a-parser for help.")) diff --git a/bs4/testing.py b/bs4/testing.py index 6ba2506..9d42702 100644 --- a/bs4/testing.py +++ b/bs4/testing.py @@ -334,7 +334,7 @@ Hello, world! self.assertSoupEquals("�", expect) self.assertSoupEquals("�", expect) self.assertSoupEquals("�", expect) - + def test_multipart_strings(self): "Mostly to prevent a recurrence of a bug in the html5lib treebuilder." soup = self.soup("

\nfoo

") diff --git a/bs4/tests/test_htmlparser.py b/bs4/tests/test_htmlparser.py index c13d59f..0381c7d 100644 --- a/bs4/tests/test_htmlparser.py +++ b/bs4/tests/test_htmlparser.py @@ -34,6 +34,11 @@ class HTMLParserTreeBuilderSmokeTest(SoupTest, HTMLTreeBuilderSmokeTest): self.assertSoupEquals('





', "


") self.assertSoupEquals('


', "") + def test_empty_element(self): + # This verifies that any buffered data present when the parser + # finishes working is handled. + self.assertSoupEquals("foo &# bar", "foo &# bar") + class TestHTMLParserSubclass(SoupTest): def test_error(self): -- cgit v1.2.3