Skip to content
Merged
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
14 changes: 14 additions & 0 deletions markdown_it/rules_block/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
headerLineRe = re.compile(r"^:?-+:?$")
enclosingPipesRe = re.compile(r"^\||\|$")

# Limit the amount of empty autocompleted cells in a table,
# see https://github.com/markdown-it/markdown-it/issues/1000,
# Both pulldown-cmark and commonmark-hs limit the number of cells this way to ~200k.
# We set it to 65k, which can expand user input by a factor of x370
# (256x256 square is 1.8kB expanded into 650kB).
MAX_AUTOCOMPLETED_CELLS = 0x10000


def getLine(state: StateBlock, line: int) -> str:
pos = state.bMarks[line] + state.tShift[line]
Expand Down Expand Up @@ -172,6 +179,7 @@ def table(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bool
token = state.push("tr_close", "tr", -1)
token = state.push("thead_close", "thead", -1)

autocompleted_cells = 0
nextLine = startLine + 2
while nextLine < endLine:
if state.sCount[nextLine] < state.blkIndent:
Expand All @@ -196,6 +204,12 @@ def table(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bool
if columns and columns[-1] == "":
columns.pop()

# note: autocomplete count can be negative if user specifies more columns than header,
# but that does not affect intended use (which is limiting expansion)
autocompleted_cells += columnCount - len(columns)
if autocompleted_cells > MAX_AUTOCOMPLETED_CELLS:
break

if nextLine == startLine + 2:
token = state.push("tbody_open", "tbody", 1)
token.map = tbodyLines = [startLine + 2, 0]
Expand Down