Commit 3254b7cb0060b2c7bfe5be96bb3ff4649544a221

Martin Mitas 2020-11-13T12:02:39

md_process_table_block_contents: Suppress empty TBODY block generation. When the table has no body rows, do not call the callback with MD_BLOCK_TBODY events. Fixes #138.

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4ab5de4..3b465d4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -19,6 +19,10 @@ Fixes:
    Handle unmatched parenthesis pairs inside a permissive URL and WWW auto-links
    in a way more compatible with the GFM.
 
+ * [#138](https://github.com/mity/md4c/issues/138):
+   The tag `<tbody></tbody>` is suppressed whenever the table has zero body
+   rows.
+
 
 ## Version 0.4.6
 
diff --git a/src/md4c.c b/src/md4c.c
index 0cc905d..314000e 100644
--- a/src/md4c.c
+++ b/src/md4c.c
@@ -4507,12 +4507,14 @@ md_process_table_block_contents(MD_CTX* ctx, int col_count, const MD_LINE* lines
                         lines[0].beg, lines[0].end, align, col_count));
     MD_LEAVE_BLOCK(MD_BLOCK_THEAD, NULL);
 
-    MD_ENTER_BLOCK(MD_BLOCK_TBODY, NULL);
-    for(i = 2; i < n_lines; i++) {
-        MD_CHECK(md_process_table_row(ctx, MD_BLOCK_TD,
-                 lines[i].beg, lines[i].end, align, col_count));
+    if(n_lines > 2) {
+        MD_ENTER_BLOCK(MD_BLOCK_TBODY, NULL);
+        for(i = 2; i < n_lines; i++) {
+            MD_CHECK(md_process_table_row(ctx, MD_BLOCK_TD,
+                     lines[i].beg, lines[i].end, align, col_count));
+        }
+        MD_LEAVE_BLOCK(MD_BLOCK_TBODY, NULL);
     }
-    MD_LEAVE_BLOCK(MD_BLOCK_TBODY, NULL);
 
 abort:
     free(align);
diff --git a/test/tables.txt b/test/tables.txt
index 80147ab..bef5799 100644
--- a/test/tables.txt
+++ b/test/tables.txt
@@ -301,8 +301,6 @@ x|x
 <th>x</th>
 </tr>
 </thead>
-<tbody>
-</tbody>
 </table>
 </li>
 </ul>
@@ -361,3 +359,20 @@ A | B
 </tbody>
 </table>
 ````````````````````````````````
+
+
+### [Issue 138](https://github.com/mity/md4c/issues/138)
+
+```````````````````````````````` example
+| abc | def |
+| --- | --- |
+.
+<table>
+<thead>
+<tr>
+<th>abc</th>
+<th>def</th>
+</tr>
+</thead>
+</table>
+````````````````````````````````