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
2 changes: 2 additions & 0 deletions markdown_it/parser_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def tokenize(self, state: StateBlock, startLine: int, endLine: int) -> None:
# - return True
for rule in rules:
if rule(state, line, endLine, False):
if state.line <= line:
raise RuntimeError("block rule didn't increment state.line")
break

# set state.tight if we had an empty line before current tag
Expand Down
5 changes: 5 additions & 0 deletions markdown_it/parser_inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ def skipToken(self, state: StateInline) -> None:
ok = rule(state, True)
state.level -= 1
if ok:
if state.pos <= pos:
raise RuntimeError("inline rule didn't increment state.pos")
break
else:
# Too much nesting, just skip until the end of the paragraph.
Expand Down Expand Up @@ -179,6 +181,7 @@ def tokenize(self, state: StateInline) -> None:
maxNesting = state.md.options["maxNesting"]

while state.pos < end:
pos = state.pos
# Try all possible rules.
# On success, rule should:
#
Expand All @@ -190,6 +193,8 @@ def tokenize(self, state: StateInline) -> None:
for rule in rules:
ok = rule(state, False)
if ok:
if state.pos <= pos:
raise RuntimeError("inline rule didn't increment state.pos")
break

if ok:
Expand Down
31 changes: 31 additions & 0 deletions tests/test_parser_progress.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Regression tests for custom rules that claim a match without consuming input."""

import pytest

from markdown_it import MarkdownIt
from markdown_it.rules_inline.state_inline import StateInline


def test_block_rule_must_advance_line() -> None:
md = MarkdownIt("commonmark")
md.block.ruler.before("paragraph", "stuck", lambda state, start, end, silent: True)

with pytest.raises(RuntimeError, match=r"block rule didn't increment state\.line"):
md.parse("text\n")


def test_inline_rule_must_advance_position() -> None:
md = MarkdownIt("commonmark")
md.inline.ruler.before("text", "stuck", lambda state, silent: True)

with pytest.raises(RuntimeError, match=r"inline rule didn't increment state\.pos"):
md.parse("text")


def test_inline_validation_rule_must_advance_position() -> None:
md = MarkdownIt("commonmark")
md.inline.ruler.before("text", "stuck", lambda state, silent: True)
state = StateInline("text", md, {}, [])

with pytest.raises(RuntimeError, match=r"inline rule didn't increment state\.pos"):
md.inline.skipToken(state)