From 2c7ce70270d81fe69cefa89c802c597360e3e873 Mon Sep 17 00:00:00 2001 From: simpleqt <2015900818@qq.com> Date: Sun, 22 Nov 2026 17:39:54 +0800 Subject: [PATCH] Reject GFM tables where the delimiter row has more cells than the header TableBlockParser accepted a table when the delimiter row had at least as many cells as the header row. The GFM spec requires the counts to match: "The header row must match the delimiter row in the number of cells. If not, a table will not be recognized", and cmark-gfm enforces strict equality (header_row->n_columns != delimiter_row->n_columns rejects the table). For example, the following was parsed as a table and now stays a paragraph: Abc|Def ---|-|- 1|2 Signed-off-by: simpleqt <2015900818@qq.com> --- .../gfm/tables/internal/TableBlockParser.java | 4 ++- .../commonmark/ext/gfm/tables/TablesTest.java | 26 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/commonmark-ext-gfm-tables/src/main/java/org/commonmark/ext/gfm/tables/internal/TableBlockParser.java b/commonmark-ext-gfm-tables/src/main/java/org/commonmark/ext/gfm/tables/internal/TableBlockParser.java index 38cef5ff..195f5e0f 100644 --- a/commonmark-ext-gfm-tables/src/main/java/org/commonmark/ext/gfm/tables/internal/TableBlockParser.java +++ b/commonmark-ext-gfm-tables/src/main/java/org/commonmark/ext/gfm/tables/internal/TableBlockParser.java @@ -312,7 +312,9 @@ public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockPar if (columns != null && !columns.isEmpty()) { SourceLine paragraph = paragraphLines.get(paragraphLines.size() - 1); List headerCells = split(paragraph); - if (columns.size() >= headerCells.size()) { + // spec: The header row must match the delimiter row in the number of cells. + // If not, a table will not be recognized + if (columns.size() == headerCells.size()) { var parser = new TableBlockParser(columns, paragraph, maxCells); return BlockStart.of(parser) .atIndex(state.getIndex()) diff --git a/commonmark-ext-gfm-tables/src/test/java/org/commonmark/ext/gfm/tables/TablesTest.java b/commonmark-ext-gfm-tables/src/test/java/org/commonmark/ext/gfm/tables/TablesTest.java index 7621660f..31df43b7 100644 --- a/commonmark-ext-gfm-tables/src/test/java/org/commonmark/ext/gfm/tables/TablesTest.java +++ b/commonmark-ext-gfm-tables/src/test/java/org/commonmark/ext/gfm/tables/TablesTest.java @@ -327,6 +327,32 @@ public void separatorMustNotHaveLessPartsThanHead() { """); } + @Test + public void separatorMustNotHaveMorePartsThanHead() { + assertRendering( + """ + Abc|Def + ---|-|- + 1|2 + """, + """ +

Abc|Def + ---|-|- + 1|2

+ """); + assertRendering( + """ + a|b|c + -|-|-|- + d|e|f + """, + """ +

a|b|c + -|-|-|- + d|e|f

+ """); + } + @Test public void padding() { assertRendering(