diff --git a/Doc/library/html.parser.rst b/Doc/library/html.parser.rst index 11f851d4f6c4b74..6b0b7f7ff0e44b7 100644 --- a/Doc/library/html.parser.rst +++ b/Doc/library/html.parser.rst @@ -101,6 +101,12 @@ The output will then be: :meth:`close` is called. *data* must be :class:`str`. +.. method:: HTMLParser.flush() + + Process all data fed insofar as it consists of complete elements. Incomplete + data remains buffered. + + .. method:: HTMLParser.close() Force processing of all buffered data as if it were followed by an end-of-file diff --git a/Lib/html/parser.py b/Lib/html/parser.py index fbe0d3665e073cc..d4877247cf56b86 100644 --- a/Lib/html/parser.py +++ b/Lib/html/parser.py @@ -192,6 +192,17 @@ def feed(self, data): # Nothing was parsed; wait until the buffer doubles. self._parse_threshold = len(self.rawdata) + def flush(self): + """Process all data fed insofar as it consists of complete elements. + Incomplete data remains buffered. + """ + if self._pending: + self.rawdata += ''.join(self._pending) + self._pending.clear() + self._pending_len = 0 + self._parse_threshold = 1 + self.goahead(0) + def close(self): """Handle any buffered data.""" if self._pending: diff --git a/Lib/test/test_htmlparser.py b/Lib/test/test_htmlparser.py index 3fdaed4ff46b9d0..b4452e7ad0551b4 100644 --- a/Lib/test/test_htmlparser.py +++ b/Lib/test/test_htmlparser.py @@ -42,7 +42,9 @@ def get_events(self): else: L.append(event) prevtype = type + # Reset events and append for re-testing self.events = L + self.append = self.events.append return L # structure markup @@ -1021,6 +1023,24 @@ def test_convert_charrefs_dropped_text(self): ('endtag', 'a'), ('data', ' bar & baz')] ) + def test_flush(self): + attrs = [(f"a{i}", str(i)) for i in range(8)] + parts = ["