Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add EOF abrupted comment tag case handling and tests
  • Loading branch information
Privat33r-dev committed Apr 6, 2024
commit caba26781b36a0e2e7309d361b128434f6a64419
10 changes: 8 additions & 2 deletions Lib/_markupbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,19 @@ def parse_marked_section(self, i, report=1):
self.unknown_decl(rawdata[i+3: j])
return match.end(0)

# Internal -- parse comment, return length or -1 if not terminated
def parse_comment(self, i, report=1):
# Internal -- parse comment
# if end is True, returns EOF location if no close tag is found, otherwise
# return length or -1 if not terminated
def parse_comment(self, i, report=1, end=False):
rawdata = self.rawdata
if rawdata[i:i+4] != '<!--':
raise AssertionError('unexpected call to parse_comment()')
match = _commentclose.search(rawdata, i+2)
if not match:
if end:
if report:
self.handle_comment(rawdata[i+4:])
return len(rawdata)
return -1
if report:
j = match.start(0)
Expand Down
2 changes: 1 addition & 1 deletion Lib/html/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def goahead(self, end):
elif startswith("</", i):
k = self.parse_endtag(i)
elif startswith("<!--", i):
k = self.parse_comment(i)
k = self.parse_comment(i, end=end)
elif startswith("<?", i):
k = self.parse_pi(i)
elif startswith("<!", i):
Expand Down
7 changes: 4 additions & 3 deletions Lib/test/test_htmlparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ def test_comments(self):
'<!---!>'
'<!--I have invalid attempt to close (space) -- >-->'
'<!--Me too (invalid character) --x>-->'
'<!--Me too (invalid characters) --cheese>-->')
'<!--Me too (invalid characters) --cheese>-->'
'<!--EOF comment')
expected = [('comment', " I'm a valid comment "),
('comment', 'me too!'),
('comment', '--'),
Expand All @@ -349,8 +350,8 @@ def test_comments(self):
('comment', ''),
('comment', 'I have invalid attempt to close (space) -- >'),
('comment', 'Me too (invalid character) --x>'),
('comment', 'Me too (invalid characters) --cheese>')
]
('comment', 'Me too (invalid characters) --cheese>'),
('comment', 'EOF comment')]
self._run_check(html, expected)

def test_condcoms(self):
Expand Down