diff --git a/Parsedown.php b/Parsedown.php
index 5e4a6e2..0b00886 100644
--- a/Parsedown.php
+++ b/Parsedown.php
@@ -836,104 +836,119 @@ class Parsedown
return;
}
- if (strpos($Block['element']['text'], '|') !== false and chop($Line['text'], ' -:|') === '')
- {
- $alignments = array();
-
- $divider = $Line['text'];
-
- $divider = trim($divider);
- $divider = trim($divider, '|');
-
- $dividerCells = explode('|', $divider);
-
- foreach ($dividerCells as $dividerCell)
- {
- $dividerCell = trim($dividerCell);
-
- if ($dividerCell === '')
- {
- continue;
- }
-
- $alignment = null;
-
- if ($dividerCell[0] === ':')
- {
- $alignment = 'left';
- }
-
- if (substr($dividerCell, - 1) === ':')
- {
- $alignment = $alignment === 'left' ? 'center' : 'right';
- }
-
- $alignments []= $alignment;
- }
-
- # ~
-
- $HeaderElements = array();
-
- $header = $Block['element']['text'];
-
- $header = trim($header);
- $header = trim($header, '|');
-
- $headerCells = explode('|', $header);
-
- foreach ($headerCells as $index => $headerCell)
- {
- $headerCell = trim($headerCell);
-
- $HeaderElement = array(
- 'name' => 'th',
- 'text' => $headerCell,
- 'handler' => 'line',
- );
-
- if (isset($alignments[$index]))
- {
- $alignment = $alignments[$index];
-
- $HeaderElement['attributes'] = array(
- 'style' => 'text-align: '.$alignment.';',
- );
- }
-
- $HeaderElements []= $HeaderElement;
- }
-
- # ~
-
- $Block = array(
- 'alignments' => $alignments,
- 'identified' => true,
- 'element' => array(
- 'name' => 'table',
- 'handler' => 'elements',
- ),
- );
-
- $Block['element']['text'] []= array(
- 'name' => 'thead',
- 'handler' => 'elements',
- );
-
- $Block['element']['text'] []= array(
- 'name' => 'tbody',
- 'handler' => 'elements',
- 'text' => array(),
- );
-
- $Block['element']['text'][0]['text'] []= array(
- 'name' => 'tr',
- 'handler' => 'elements',
- 'text' => $HeaderElements,
- );
-
- return $Block;
+ if (
+ strpos($Block['element']['text'], '|') === false
+ and strpos($Line['text'], '|') === false
+ and strpos($Line['text'], ':') === false
+ ) {
+ return;
}
+
+ if (chop($Line['text'], ' -:|') !== '')
+ {
+ return;
+ }
+
+ $alignments = array();
+
+ $divider = $Line['text'];
+
+ $divider = trim($divider);
+ $divider = trim($divider, '|');
+
+ $dividerCells = explode('|', $divider);
+
+ foreach ($dividerCells as $dividerCell)
+ {
+ $dividerCell = trim($dividerCell);
+
+ if ($dividerCell === '')
+ {
+ return;
+ }
+
+ $alignment = null;
+
+ if ($dividerCell[0] === ':')
+ {
+ $alignment = 'left';
+ }
+
+ if (substr($dividerCell, - 1) === ':')
+ {
+ $alignment = $alignment === 'left' ? 'center' : 'right';
+ }
+
+ $alignments []= $alignment;
+ }
+
+ # ~
+
+ $HeaderElements = array();
+
+ $header = $Block['element']['text'];
+
+ $header = trim($header);
+ $header = trim($header, '|');
+
+ $headerCells = explode('|', $header);
+
+ if (count($headerCells) !== count($alignments))
+ {
+ return;
+ }
+
+ foreach ($headerCells as $index => $headerCell)
+ {
+ $headerCell = trim($headerCell);
+
+ $HeaderElement = array(
+ 'name' => 'th',
+ 'text' => $headerCell,
+ 'handler' => 'line',
+ );
+
+ if (isset($alignments[$index]))
+ {
+ $alignment = $alignments[$index];
+
+ $HeaderElement['attributes'] = array(
+ 'style' => 'text-align: '.$alignment.';',
+ );
+ }
+
+ $HeaderElements []= $HeaderElement;
+ }
+
+ # ~
+
+ $Block = array(
+ 'alignments' => $alignments,
+ 'identified' => true,
+ 'element' => array(
+ 'name' => 'table',
+ 'handler' => 'elements',
+ ),
+ );
+
+ $Block['element']['text'] []= array(
+ 'name' => 'thead',
+ 'handler' => 'elements',
+ );
+
+ $Block['element']['text'] []= array(
+ 'name' => 'tbody',
+ 'handler' => 'elements',
+ 'text' => array(),
+ );
+
+ $Block['element']['text'][0]['text'] []= array(
+ 'name' => 'tr',
+ 'handler' => 'elements',
+ 'text' => $HeaderElements,
+ );
+
+ return $Block;
}
protected function blockTableContinue($Line, array $Block)
@@ -943,7 +958,7 @@ class Parsedown
return;
}
- if ($Line['text'][0] === '|' or strpos($Line['text'], '|'))
+ if (count($Block['alignments']) === 1 or $Line['text'][0] === '|' or strpos($Line['text'], '|'))
{
$Elements = array();
@@ -954,7 +969,9 @@ class Parsedown
preg_match_all('/(?:(\\\\[|])|[^|`]|`[^`]+`|`)+/', $row, $matches);
- foreach ($matches[0] as $index => $cell)
+ $cells = array_slice($matches[0], 0, count($Block['alignments']));
+
+ foreach ($cells as $index => $cell)
{
$cell = trim($cell);
diff --git a/test/data/simple_table.html b/test/data/simple_table.html
index 237d7ef..f6f7aab 100644
--- a/test/data/simple_table.html
+++ b/test/data/simple_table.html
@@ -34,4 +34,36 @@
cell 2.2 |
+
+
+
+
+
+header 1 |
+
+
+
+
+cell 1.1 |
+
+
+cell 2.1 |
+
+
+
+
+
+
+
+header 1 |
+
+
+
+
+cell 1.1 |
+
+
+cell 2.1 |
+
+
\ No newline at end of file
diff --git a/test/data/simple_table.md b/test/data/simple_table.md
index 466d140..f767257 100644
--- a/test/data/simple_table.md
+++ b/test/data/simple_table.md
@@ -8,4 +8,18 @@ cell 2.1 | cell 2.2
header 1 | header 2
:------- | --------
cell 1.1 | cell 1.2
-cell 2.1 | cell 2.2
\ No newline at end of file
+cell 2.1 | cell 2.2
+
+---
+
+header 1
+:-------
+cell 1.1
+cell 2.1
+
+---
+
+header 1
+-------|
+cell 1.1
+cell 2.1
\ No newline at end of file