From cb692b91658248fd09a6289206d79c76405ed584 Mon Sep 17 00:00:00 2001 From: Brent Plump Date: Sat, 4 Sep 2021 15:47:26 +1000 Subject: [PATCH] Fix tables with leading/trailing header pipes and trailing spaces --- .../gfm/tables/internal/TableBlockParser.java | 14 ++++-- .../commonmark/ext/gfm/tables/TablesTest.java | 43 +++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) 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 a8eedb7a..a203164d 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 @@ -122,14 +122,22 @@ private TableCell parseCell(SourceLine cell, int column, InlineParser inlinePars private static List split(SourceLine line) { CharSequence row = line.getContent(); int nonSpace = Parsing.skipSpaceTab(row, 0, row.length()); - int cellStart = row.charAt(nonSpace) == '|' ? nonSpace + 1 : nonSpace; + int cellStart = nonSpace; + int cellEnd = row.length(); + if (row.charAt(nonSpace) == '|') { + // This row has leading/trailing pipes - skip the leading pipe + cellStart = nonSpace + 1; + // Strip whitespace from the end but not the pipe or we could miss an empty ("||") cell + int nonSpaceEnd = Parsing.skipSpaceTabBackwards(row, row.length() - 1, cellStart + 1); + cellEnd = nonSpaceEnd + 1; + } List cells = new ArrayList<>(); StringBuilder sb = new StringBuilder(); - for (int i = cellStart; i < row.length(); i++) { + for (int i = cellStart; i < cellEnd; i++) { char c = row.charAt(i); switch (c) { case '\\': - if (i + 1 < row.length() && row.charAt(i + 1) == '|') { + if (i + 1 < cellEnd && row.charAt(i + 1) == '|') { // Pipe is special for table parsing. An escaped pipe doesn't result in a new cell, but is // passed down to inline parsing as an unescaped pipe. Note that that applies even for the `\|` // in an input like `\\|` - in other words, table parsing doesn't support escaping backslashes. 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 2ec36cf1..4d515321 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 @@ -235,6 +235,49 @@ public void pipesOnOutside() { "\n"); } + @Test + public void pipesOnOutsideWhitespaceAfterHeader() { + assertRendering("|Abc|Def| \n|---|---|\n|1|2|", "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "
AbcDef
12
\n"); + } + + @Test + public void pipesOnOutsideZeroLengthHeaders() { + // This is literally what someone has done IRL - it helped to expose + // an issue with parsing the last header cell correctly + assertRendering("||center header||\n" + + "-|-------------|-\n" + + "1| 2 |3", + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "
center header
123
\n"); + } + @Test public void inlineElements() { assertRendering("*Abc*|Def\n---|---\n1|2", "\n" +