[3.14] gh-102555: Fix comment parsing in HTMLParser according to the … · python/cpython@fa7e76e · GitHub
Skip to content

Commit fa7e76e

Browse files
miss-islingtonserhiy-storchakaezio-melotti
authored
[3.14] gh-102555: Fix comment parsing in HTMLParser according to the HTML5 standard (GH-135664) (GH-136271)
* "--!>" now ends the comment. * "-- >" no longer ends the comment. * Support abnormally ended empty comments "<-->" and "<--->". --------- (cherry picked from commit 8ac7613) Co-author: Kerim Kabirov <the.privat33r+gh@pm.me> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Ezio Melotti <ezio.melotti@gmail.com>
1 parent 2449f77 commit fa7e76e

3 files changed

Lines changed: 50 additions & 3 deletions

File tree

Lib/html/parser.py

Lines changed: 17 additions & 1 deletion

Lib/test/test_htmlparser.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,17 +367,45 @@ def test_comments(self):
367367
html = ("<!-- I'm a valid comment -->"
368368
'<!--me too!-->'
369369
'<!------>'
370+
'<!----->'
370371
'<!---->'
372+
# abrupt-closing-of-empty-comment
373+
'<!--->'
374+
'<!-->'
371375
'<!----I have many hyphens---->'
372376
'<!-- I have a > in the middle -->'
373-
'<!-- and I have -- in the middle! -->')
377+
'<!-- and I have -- in the middle! -->'
378+
'<!--incorrectly-closed-comment--!>'
379+
'<!----!>'
380+
'<!----!-->'
381+
'<!---- >-->'
382+
'<!---!>-->'
383+
'<!--!>-->'
384+
# nested-comment
385+
'<!-- <!-- nested --> -->'
386+
'<!--<!-->'
387+
'<!--<!--!>'
388+
)
374389
expected = [('comment', " I'm a valid comment "),
375390
('comment', 'me too!'),
376391
('comment', '--'),
392+
('comment', '-'),
393+
('comment', ''),
394+
('comment', ''),
377395
('comment', ''),
378396
('comment', '--I have many hyphens--'),
379397
('comment', ' I have a > in the middle '),
380-
('comment', ' and I have -- in the middle! ')]
398+
('comment', ' and I have -- in the middle! '),
399+
('comment', 'incorrectly-closed-comment'),
400+
('comment', ''),
401+
('comment', '--!'),
402+
('comment', '-- >'),
403+
('comment', '-!>'),
404+
('comment', '!>'),
405+
('comment', ' <!-- nested '), ('data', ' -->'),
406+
('comment', '<!'),
407+
('comment', '<!'),
408+
]
381409
self._run_check(html, expected)
382410

383411
def test_condcoms(self):
Lines changed: 3 additions & 0 deletions

0 commit comments

Comments
 (0)