Skip to content
Open
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
1 change: 1 addition & 0 deletions .spell-dict
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ refactor
refactored
refactors
registerExtension
rescan
resettable
richeland
RSS
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ See the [Contributing Guide](contributing.md) for details.
consisting only of `{` and some text with many trailing spaces (#1656).
* Fix quadratic-time regex backtracking in hash headers when a long run of `#`
characters is followed by more text on the same line.
* Fix quadratic time when a document contains many unclosed HTML comments (`<!--`
with no matching `-->`).
* Fix regression in `BacktickInlineProcessor` where text could be lost when opening
and closing backticks did not match (#1668).
* Escape the language name in the `codehilite` extension's non-Pygments output to
Expand Down
11 changes: 10 additions & 1 deletion markdown/htmlparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ def reset(self):
self._cache: list[str] = []
self.cleandoc: list[str] = []
self.lineno_start_cache = [0]
# The `rawdata` and position of the last failed search for a comment close.
self._no_commentclose: tuple[str | None, int] = (None, 0)

super().reset()

Expand Down Expand Up @@ -359,8 +361,15 @@ def parse_pi(self, i: int) -> int:
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)
# If no close was found after an earlier position in the same `rawdata`, there
# is none after this one either. Don't rescan the rest of the document again.
searched, pos = self._no_commentclose
if searched is rawdata and i + 4 >= pos:
match = None
else:
match = commentclose.search(rawdata, i+4)
if not match:
self._no_commentclose = (rawdata, i + 4)
self.handle_data('<')
return i + 1
if report:
Expand Down
7 changes: 7 additions & 0 deletions tests/test_syntax/blocks/test_html_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1860,6 +1860,13 @@ def test_multiple_unclosed_comments_no_hang(self):
'<p>&lt;!-- and &lt;!--</p>'
)

def test_many_unclosed_comments(self):
"""Test that each unclosed comment does not rescan the rest of the document."""
self.assertMarkdownRenders(
'<!--' * 50000,
'<p>' + '&lt;!--' * 50000 + '</p>'
)

def test_no_hang_issue_1586(self):
"""Test no hang condition for issue #1586."""

Expand Down
Loading