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
3 changes: 3 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ See the [Contributing Guide](contributing.md) for details.
* Fix quadratic processing time in the `tables` extension for long blocks which
are split into many smaller blocks, such as many headers with no blank lines
between them (#1704).
* Fix quadratic processing time in emphasis handling for many unclosed openers
which the rule of 3 keeps an ambiguous closer from pairing with, such as
`*a **b` repeated.

## [3.11.3] - 2026-11-05

Expand Down
16 changes: 15 additions & 1 deletion markdown/inlinepatterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,8 @@ def __init__(
self.stack: list[tuple[int, int, bool, int, int]] = []
# Position of every delimiter token (valid or not) encountered while pairing, in ascending order.
self.positions: list[int] = []
# For each token and run length, how deep the last failed rule of 3 search went and the entry at that depth.
self.searched: dict[tuple[str, int], tuple[int, tuple[int, int, bool, int, int]]] = {}
self.tokens: list[str] = []
self.delimiters: dict[str, Delimiter] = {}
self.cache_index = 0
Expand Down Expand Up @@ -710,6 +712,7 @@ def reset(self) -> None:
self.cache.clear()
self.stack.clear()
self.positions.clear()
self.searched.clear()
self.cache_index = 0
self.cache_pos = 0
self.remainder = None
Expand Down Expand Up @@ -994,12 +997,23 @@ def _handle_match(
stack and is_ambiguous and current != last and
self.rule_of_3(stack[-1][3], current)
):
for i in range(len(stack) - 2, -1, -1):
# Skip the entries an earlier search for this length already ruled out. They are unchanged
# as long as the last of them is still in place, as entries are only ever appended or popped.
lowest = 0
key = (token, current)
if key in self.searched:
depth, entry = self.searched[key]
if depth < len(stack) and stack[depth - 1] is entry:
lowest = depth
for i in range(len(stack) - 2, lowest - 1, -1):
if stack[i][-1] == current and not self.rule_of_3(stack[i][3], current):
while len(stack) > i + 1:
stack.pop()
last = current
break
else:
if len(stack) > 1:
self.searched[key] = (len(stack) - 1, stack[-2])

# Find closing tokens
# Looking for:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_syntax/inline/test_emphasis.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,15 @@ def test_rule_of_3_skipped_opener_removed(self):
'<p><em><strong>a</strong>b</em>cd<em>e**f</em>g<em>h</em>**</p>'
)

def test_rule_of_3_many_unclosed_openers(self):
"""Test that unclosed openers rejected by the rule of 3 are not searched again for every closer."""

count = 10000
self.assertMarkdownRenders(
'*a **b*a **b' * count,
'<p>' + '<em>a **b</em>a **b' * count + '</p>'
)


class TestProcessorRemoval(TestCase):

Expand Down
Loading