From 2297a4e20beafadd3338ce4e08d166937dbed21d Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Sat, 16 May 2015 00:51:50 +0800 Subject: [PATCH] Fix rendering in table with empty cells `find_emph_char` returns 0 if the char specified is not found in the current line, but this is also what happens when there's an empty cell. This patch adds logic to work around this problem. See uranusjr/macdown#321 --- src/document.c | 10 ++++++- test/Tests/Table.html | 66 +++++++++++++++++++++++++++++++++++++++++++ test/Tests/Table.text | 21 ++++++++++++++ test/config.json | 5 ++++ 4 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 test/Tests/Table.html create mode 100644 test/Tests/Table.text diff --git a/src/document.c b/src/document.c index 27c17b23..99596e95 100644 --- a/src/document.c +++ b/src/document.c @@ -2225,7 +2225,15 @@ parse_table_row( cell_start = i; len = find_emph_char(data + i, size - i, '|'); - i += len ? len : size - i; + + /* Two possibilities for len == 0: + 1) No more pipe char found in the current line. + 2) The next pipe is right after the current one, i.e. empty cell. + For case 1, we skip to the end of line; for case 2 we just continue. + */ + if (len == 0 && data[i] != '|') + len = size - i; + i += len; cell_end = i - 1; diff --git a/test/Tests/Table.html b/test/Tests/Table.html new file mode 100644 index 00000000..f4345980 --- /dev/null +++ b/test/Tests/Table.html @@ -0,0 +1,66 @@ +

Standard table

+ + + + + + + + + + + + + + +
headline1headline2
123
+ + +

Cell alignment

+ + + + + + + + + + + + + + + + +
headline1headline2headline3
123
+ + +

Malformed table: missing cell at row in body

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
headline1headline2headline3
12
34
56
diff --git a/test/Tests/Table.text b/test/Tests/Table.text new file mode 100644 index 00000000..37df5397 --- /dev/null +++ b/test/Tests/Table.text @@ -0,0 +1,21 @@ +# Standard table + +|headline1|headline2| +|---------|---------| +|123 | | + + +# Cell alignment + +|headline1|headline2|headline3| +|:-------|:------:|------:| +|123||| + + +# Malformed table: missing cell at row in body + +|headline1|headline2|headline3| +|-------|-------|-------| +|12 +|34|| +|56| diff --git a/test/config.json b/test/config.json index d3e170e8..365c7bc9 100644 --- a/test/config.json +++ b/test/config.json @@ -106,6 +106,11 @@ "input": "Tests/Underline.text", "output": "Tests/Underline.html", "flags": ["--underline"] + }, + { + "input": "Tests/Table.text", + "output": "Tests/Table.html", + "flags": ["--tables"] } ] }