Skip to content

Commit caba267

Browse files
committed
Add EOF abrupted comment tag case handling and tests
1 parent 9e687bf commit caba267

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

Lib/_markupbase.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,19 @@ def parse_marked_section(self, i, report=1):
161161
self.unknown_decl(rawdata[i+3: j])
162162
return match.end(0)
163163

164-
# Internal -- parse comment, return length or -1 if not terminated
165-
def parse_comment(self, i, report=1):
164+
# Internal -- parse comment
165+
# if end is True, returns EOF location if no close tag is found, otherwise
166+
# return length or -1 if not terminated
167+
def parse_comment(self, i, report=1, end=False):
166168
rawdata = self.rawdata
167169
if rawdata[i:i+4] != '<!--':
168170
raise AssertionError('unexpected call to parse_comment()')
169171
match = _commentclose.search(rawdata, i+2)
170172
if not match:
173+
if end:
174+
if report:
175+
self.handle_comment(rawdata[i+4:])
176+
return len(rawdata)
171177
return -1
172178
if report:
173179
j = match.start(0)

Lib/html/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def goahead(self, end):
172172
elif startswith("</", i):
173173
k = self.parse_endtag(i)
174174
elif startswith("<!--", i):
175-
k = self.parse_comment(i)
175+
k = self.parse_comment(i, end=end)
176176
elif startswith("<?", i):
177177
k = self.parse_pi(i)
178178
elif startswith("<!", i):

Lib/test/test_htmlparser.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,8 @@ def test_comments(self):
333333
'<!---!>'
334334
'<!--I have invalid attempt to close (space) -- >-->'
335335
'<!--Me too (invalid character) --x>-->'
336-
'<!--Me too (invalid characters) --cheese>-->')
336+
'<!--Me too (invalid characters) --cheese>-->'
337+
'<!--EOF comment')
337338
expected = [('comment', " I'm a valid comment "),
338339
('comment', 'me too!'),
339340
('comment', '--'),
@@ -349,8 +350,8 @@ def test_comments(self):
349350
('comment', ''),
350351
('comment', 'I have invalid attempt to close (space) -- >'),
351352
('comment', 'Me too (invalid character) --x>'),
352-
('comment', 'Me too (invalid characters) --cheese>')
353-
]
353+
('comment', 'Me too (invalid characters) --cheese>'),
354+
('comment', 'EOF comment')]
354355
self._run_check(html, expected)
355356

356357
def test_condcoms(self):

0 commit comments

Comments
 (0)