Skip to content

Commit f117453

Browse files
authored
🐛 FIX: texmath infinite loop on block math closed outside blockquote (#157)
The texmath block rules matched their regex against the rest of the source, so a block opened inside a blockquote could match a closing delimiter placed after it. The rule then pushed a token and returned True without advancing `state.line`, and the block tokenizer looped forever. The closing line is now searched within the current block only; otherwise the rule does not match, as in `amsmath` (#117). Fixes #156
1 parent cbac88c commit f117453

4 files changed

Lines changed: 93 additions & 19 deletions

File tree

‎mdit_py_plugins/texmath/index.py‎

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -128,25 +128,28 @@ def make_block_func(rule: RuleDictType) -> Callable[[StateBlock, int, int, bool]
128128
def _func(state: StateBlock, begLine: int, endLine: int, silent: bool) -> bool:
129129
begin = state.bMarks[begLine] + state.tShift[begLine]
130130
res = applyRule(rule, state.src, begin, state.parentType == "blockquote")
131-
if res:
132-
if not silent:
133-
token = state.push(rule["name"], "math", 0)
134-
token.block = True
135-
token.content = res[1]
136-
token.info = res[len(res.groups())]
137-
token.markup = rule["tag"]
138-
139-
line = begLine
140-
endpos = begin + res.end() - 1
141-
142-
while line < endLine:
143-
if endpos >= state.bMarks[line] and endpos <= state.eMarks[line]:
144-
# line for end of block math found ...
145-
state.line = line + 1
146-
break
147-
line += 1
148-
149-
return bool(res)
131+
if not res:
132+
return False
133+
134+
# find the line holding the end of the match, within this block only
135+
endpos = begin + res.end() - 1
136+
line = begLine
137+
while line < endLine and endpos > state.eMarks[line]:
138+
line += 1
139+
if line >= endLine or endpos < state.bMarks[line]:
140+
# the closing delimiter lies outside the current block,
141+
# e.g. math opened inside a blockquote and closed after it
142+
return False
143+
144+
if not silent:
145+
token = state.push(rule["name"], "math", 0)
146+
token.block = True
147+
token.content = res[1]
148+
token.info = res[len(res.groups())]
149+
token.markup = rule["tag"]
150+
151+
state.line = line + 1
152+
return True
150153

151154
return _func
152155

‎tests/fixtures/texmath_bracket.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,3 +410,15 @@ Indented by 4 spaces, DISABLE-CODEBLOCKS
410410
</eqn>
411411
</section>
412412
.
413+
414+
display equation opened in blockquote and closed outside it (valid=False)
415+
.
416+
> \[ a=1
417+
418+
\]
419+
.
420+
<blockquote>
421+
<p>[ a=1</p>
422+
</blockquote>
423+
<p>]</p>
424+
.

‎tests/fixtures/texmath_dollar.md‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,3 +454,45 @@ Indented by 4 spaces, DISABLE-CODEBLOCKS
454454
<eqn>a</eqn>
455455
</section>
456456
.
457+
458+
display equation opened in blockquote and closed outside it (valid=False)
459+
.
460+
> $$ a=1
461+
462+
$$
463+
.
464+
<blockquote>
465+
<p>$$ a=1</p>
466+
</blockquote>
467+
<p>$$</p>
468+
.
469+
470+
display equation opened in nested blockquote and closed outside it (valid=False)
471+
.
472+
> > $$ a=1
473+
474+
$$
475+
.
476+
<blockquote>
477+
<blockquote>
478+
<p>$$ a=1</p>
479+
</blockquote>
480+
</blockquote>
481+
<p>$$</p>
482+
.
483+
484+
display equation opened in blockquote in a list item and closed outside it (valid=False)
485+
.
486+
- > $$ a=1
487+
488+
$$ (1)
489+
.
490+
<ul>
491+
<li>
492+
<blockquote>
493+
<p>$$ a=1</p>
494+
</blockquote>
495+
</li>
496+
</ul>
497+
<p>$$ (1)</p>
498+
.

‎tests/test_texmath.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,23 @@ def test_plugin_parse(data_regression):
8080
data_regression.check([t.as_dict() for t in tokens])
8181

8282

83+
@pytest.mark.parametrize(
84+
"delimiters,opening,closing",
85+
[
86+
("brackets", "\\[", "\\]"),
87+
("dollars", "$$", "$$"),
88+
("kramdown", "$$", "$$"),
89+
("gitlab", "```math", "```"),
90+
("julia", "```math", "```"),
91+
],
92+
)
93+
def test_block_closed_outside_blockquote(delimiters, opening, closing):
94+
"""A math block opened inside a blockquote cannot close after it."""
95+
md = MarkdownIt("commonmark").use(texmath_plugin, delimiters=delimiters)
96+
tokens = md.parse(f"> {opening} a=1\n\n{closing}\n")
97+
assert not [t for t in tokens if t.type.startswith("math_block")]
98+
99+
83100
@pytest.mark.parametrize(
84101
"line,title,input,expected",
85102
read_fixture_file(FIXTURE_PATH.joinpath("texmath_dollar.md")),

0 commit comments

Comments
 (0)