Skip to content

Commit 060847b

Browse files
serhiy-storchakaezio-melotti
authored andcommitted
gh-102555: Fix comment parsing in HTMLParser according to the HTML5 standard (GH-135664)
* "--!>" now ends the comment. * "-- >" no longer ends the comment. * Support abnormally ended empty comments "<-->" and "<--->". --------- (cherry picked from commit 8ac7613) Co-authored-by: Serhiy Storchaka <[email protected]> Co-author: Kerim Kabirov <[email protected]> Co-authored-by: Ezio Melotti <[email protected]>
1 parent ab0893f commit 060847b

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

Lib/html/parser.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
starttagopen = re.compile('<[a-zA-Z]')
2828
endtagopen = re.compile('</[a-zA-Z]')
2929
piclose = re.compile('>')
30-
commentclose = re.compile(r'--\s*>')
30+
commentclose = re.compile(r'--!?>')
31+
commentabruptclose = re.compile(r'-?>')
3132
# Note:
3233
# 1) if you change tagfind/attrfind remember to update locatestarttagend too;
3334
# 2) if you change tagfind/attrfind and/or locatestarttagend the parser will
@@ -291,6 +292,21 @@ def parse_html_declaration(self, i):
291292
else:
292293
return self.parse_bogus_comment(i)
293294

295+
# Internal -- parse comment, return length or -1 if not terminated
296+
# see https://html.spec.whatwg.org/multipage/parsing.html#comment-start-state
297+
def parse_comment(self, i, report=True):
298+
rawdata = self.rawdata
299+
assert rawdata.startswith('<!--', i), 'unexpected call to parse_comment()'
300+
match = commentclose.search(rawdata, i+4)
301+
if not match:
302+
match = commentabruptclose.match(rawdata, i+4)
303+
if not match:
304+
return -1
305+
if report:
306+
j = match.start()
307+
self.handle_comment(rawdata[i+4: j])
308+
return match.end()
309+
294310
# Internal -- parse bogus comment, return length or -1 if not terminated
295311
# see http://www.w3.org/TR/html5/tokenization.html#bogus-comment-state
296312
def parse_bogus_comment(self, i, report=1):

Lib/test/test_htmlparser.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,17 +322,45 @@ def test_comments(self):
322322
html = ("<!-- I'm a valid comment -->"
323323
'<!--me too!-->'
324324
'<!------>'
325+
'<!----->'
325326
'<!---->'
327+
# abrupt-closing-of-empty-comment
328+
'<!--->'
329+
'<!-->'
326330
'<!----I have many hyphens---->'
327331
'<!-- I have a > in the middle -->'
328-
'<!-- and I have -- in the middle! -->')
332+
'<!-- and I have -- in the middle! -->'
333+
'<!--incorrectly-closed-comment--!>'
334+
'<!----!>'
335+
'<!----!-->'
336+
'<!---- >-->'
337+
'<!---!>-->'
338+
'<!--!>-->'
339+
# nested-comment
340+
'<!-- <!-- nested --> -->'
341+
'<!--<!-->'
342+
'<!--<!--!>'
343+
)
329344
expected = [('comment', " I'm a valid comment "),
330345
('comment', 'me too!'),
331346
('comment', '--'),
347+
('comment', '-'),
348+
('comment', ''),
349+
('comment', ''),
332350
('comment', ''),
333351
('comment', '--I have many hyphens--'),
334352
('comment', ' I have a > in the middle '),
335-
('comment', ' and I have -- in the middle! ')]
353+
('comment', ' and I have -- in the middle! '),
354+
('comment', 'incorrectly-closed-comment'),
355+
('comment', ''),
356+
('comment', '--!'),
357+
('comment', '-- >'),
358+
('comment', '-!>'),
359+
('comment', '!>'),
360+
('comment', ' <!-- nested '), ('data', ' -->'),
361+
('comment', '<!'),
362+
('comment', '<!'),
363+
]
336364
self._run_check(html, expected)
337365

338366
def test_condcoms(self):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix comment parsing in :class:`html.parser.HTMLParser` according to the
2+
HTML5 standard. ``--!>`` now ends the comment. ``-- >`` no longer ends the
3+
comment. Support abnormally ended empty comments ``<-->`` and ``<--->``.

0 commit comments

Comments
 (0)