Repository: thephpleague/commonmark (php)

Issue:
Tables in tight list items don't render
**Version(s) affected**: 1.5.7

**Description**

table syntax included after plain text in a list item is rendered as plain text instead of a table. this differs from cmark-gfm, where the text and table are rendered as children of the list item. this only happens when there are no empty lines between the text and table (if there are, it renders as a paragraph and table correctly)

it looks like an issue specific to tables because other block-level elements are working fine here, I tested blockquotes and code blocks

**How to reproduce**

[compare on babelmark](https://babelmark.github.io/?text=-+list+item%0A++%7C+head+%7C%0A++%7C+%3A--+%7C%0A++%7C+body+%7C) between league/commonmark GFM and GitHub Flavored Markdown

Pull request description:
Fix parsing of tables within list items
Fixes #590

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 23fe7c04..887fae08 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,7 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi
 ### Fixed
 
  - Fixed Table of Contents not rendering heading inlines properly (#587, #588)
+ - Fixed parsing of tables within list items (#590)
 
 ## [1.5.7] - 2020-10-31
 
diff --git a/src/Extension/Table/TableParser.php b/src/Extension/Table/TableParser.php
index ce44332b..4136503c 100644
--- a/src/Extension/Table/TableParser.php
+++ b/src/Extension/Table/TableParser.php
@@ -39,11 +39,12 @@ final class TableParser implements BlockParserInterface, EnvironmentAwareInterfa
         }
 
         $lines = $container->getStrings();
-        if (count($lines) !== 1) {
+        if (count($lines) === 0) {
             return false;
         }
 
-        if (\strpos($lines[0], '|') === false) {
+        $lastLine = \array_pop($lines);
+        if (\strpos($lastLine, '|') === false) {
             return false;
         }
 
@@ -57,7 +58,7 @@ final class TableParser implements BlockParserInterface, EnvironmentAwareInterfa
             return false;
         }
 
-        $head = $this->parseRow(trim((string) array_pop($lines)), $columns, TableCell::TYPE_HEAD);
+        $head = $this->parseRow(trim((string) $lastLine), $columns, TableCell::TYPE_HEAD);
         if (null === $head) {
             $cursor->restoreState($oldState);
 


Test patch:
diff --git a/tests/functional/Extension/Table/data/single_column_table_and_block_elements.html b/tests/functional/Extension/Table/data/single_column_table_and_block_elements.html
index 8163c939..849be8a1 100644
--- a/tests/functional/Extension/Table/data/single_column_table_and_block_elements.html
+++ b/tests/functional/Extension/Table/data/single_column_table_and_block_elements.html
@@ -15,7 +15,16 @@
 </tbody>
 </table>
 <p>Paragraph</p>
-<p>Paragraph and not a table
-| header 1 |
-| -------- |
-| cell 1.1 |</p>
+<p>Paragraph followed by a table</p>
+<table>
+<thead>
+<tr>
+<th>header 1</th>
+</tr>
+</thead>
+<tbody>
+<tr>
+<td>cell 1.1</td>
+</tr>
+</tbody>
+</table>
diff --git a/tests/functional/Extension/Table/data/single_column_table_and_block_elements.md b/tests/functional/Extension/Table/data/single_column_table_and_block_elements.md
index c348fa80..a510abd9 100644
--- a/tests/functional/Extension/Table/data/single_column_table_and_block_elements.md
+++ b/tests/functional/Extension/Table/data/single_column_table_and_block_elements.md
@@ -7,7 +7,7 @@ Not a paragraph
 
 Paragraph
 
-Paragraph and not a table
+Paragraph followed by a table
 | header 1 |
 | -------- |
 | cell 1.1 |
diff --git a/tests/functional/Extension/Table/data/table-and-block-elements.html b/tests/functional/Extension/Table/data/table-and-block-elements.html
index f9e1ff04..b04e3d1b 100644
--- a/tests/functional/Extension/Table/data/table-and-block-elements.html
+++ b/tests/functional/Extension/Table/data/table-and-block-elements.html
@@ -18,10 +18,40 @@
 <li>list</li>
 <li>list</li>
 </ul>
-<p>Paragraph
-th | th
----|---
-td | td</p>
+<p>Paragraph</p>
+<table>
+<thead>
+<tr>
+<th>th</th>
+<th>th</th>
+</tr>
+</thead>
+<tbody>
+<tr>
+<td>td</td>
+<td>td</td>
+</tr>
+</tbody>
+</table>
+<table>
+<thead>
+<tr>
+<th>th</th>
+<th>th</th>
+</tr>
+</thead>
+<tbody>
+<tr>
+<td>td</td>
+<td>td</td>
+</tr>
+<tr>
+<td>Cell</td>
+<td></td>
+</tr>
+</tbody>
+</table>
+<p>Paragraph</p>
 <table>
 <thead>
 <tr>
@@ -35,13 +65,8 @@ td | td</p>
 <td>td</td>
 </tr>
 <tr>
-<td>Paragraph</td>
+<td>Cell</td>
 <td></td>
 </tr>
 </tbody>
 </table>
-<p>Paragraph
-th | th
----|---
-td | td
-Paragraph</p>
diff --git a/tests/functional/Extension/Table/data/table-and-block-elements.md b/tests/functional/Extension/Table/data/table-and-block-elements.md
index 44597311..fcb2ec65 100644
--- a/tests/functional/Extension/Table/data/table-and-block-elements.md
+++ b/tests/functional/Extension/Table/data/table-and-block-elements.md
@@ -19,10 +19,10 @@ td | td
 th | th
 ---|---
 td | td
-Paragraph
+Cell
 
 Paragraph
 th | th
 ---|---
 td | td
-Paragraph
+Cell
diff --git a/tests/functional/Extension/Table/data/table-in-list.html b/tests/functional/Extension/Table/data/table-in-list.html
new file mode 100644
index 00000000..62861003
--- /dev/null
+++ b/tests/functional/Extension/Table/data/table-in-list.html
@@ -0,0 +1,16 @@
+<ul>
+<li>list item
+<table>
+<thead>
+<tr>
+<th align="left">head</th>
+</tr>
+</thead>
+<tbody>
+<tr>
+<td align="left">body</td>
+</tr>
+</tbody>
+</table>
+</li>
+</ul>
diff --git a/tests/functional/Extension/Table/data/table-in-list.md b/tests/functional/Extension/Table/data/table-in-list.md
new file mode 100644
index 00000000..73370a5d
--- /dev/null
+++ b/tests/functional/Extension/Table/data/table-in-list.md
@@ -0,0 +1,4 @@
+- list item
+  | head |
+  | :-- |
+  | body |