Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Lib/html/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,11 @@ def parse_html_declaration(self, i):
def parse_comment(self, i, report=True):
rawdata = self.rawdata
assert rawdata.startswith('<!--', i), 'unexpected call to parse_comment()'
match = commentclose.search(rawdata, i+4)
# An empty comment is abruptly closed by the first ">" or "->",
# taking priority over a later "-->" or "--!>" close.
match = commentabruptclose.match(rawdata, i+4)
if not match:
match = commentabruptclose.match(rawdata, i+4)
match = commentclose.search(rawdata, i+4)
if not match:
return -1
if report:
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_htmlparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ def _run_check(self, source, expected_events,
*, collector=None, convert_charrefs=False):
if collector is None:
collector = self.get_collector(convert_charrefs=convert_charrefs)
if isinstance(source, str):
# Also feed the whole string at once, not just character by
# character (below), to exercise different input buffering.
self._run_check([source], expected_events,
convert_charrefs=convert_charrefs)
parser = collector
for s in source:
parser.feed(s)
Expand Down Expand Up @@ -593,6 +598,9 @@ def test_comments(self):
'<!-- <!-- nested --> -->'
'<!--<!-->'
'<!--<!--!>'
# abruptly closed empty comment must not swallow later text
'<!-->x-->'
'<!--->y-->'
)
expected = [('comment', " I'm a valid comment "),
('comment', 'me too!'),
Expand All @@ -613,6 +621,8 @@ def test_comments(self):
('comment', ' <!-- nested '), ('data', ' -->'),
('comment', '<!'),
('comment', '<!'),
('comment', ''), ('data', 'x-->'),
('comment', ''), ('data', 'y-->'),
]
self._run_check(html, expected)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :class:`html.parser.HTMLParser`: an abruptly closed empty comment
(``<!-->`` or ``<!--->``) no longer extends up to a later ``-->`` in the same
:meth:`~html.parser.HTMLParser.feed` call.
Loading