Skip to content

Commit

Permalink
triggers syntax error when number of table cells in header/body are n…
Browse files Browse the repository at this point in the history
…ot the same (on strict mode) #4
  • Loading branch information
kzykhys committed Aug 7, 2013
1 parent cc5eb10 commit f4646a2
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 29 deletions.
108 changes: 79 additions & 29 deletions src/Ciconia/Extension/Gfm/TableExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Ciconia\Common\Collection;
use Ciconia\Common\Tag;
use Ciconia\Common\Text;
use Ciconia\Exception\SyntaxError;
use Ciconia\Extension\ExtensionInterface;
use Ciconia\Markdown;
use Ciconia\Renderer\HtmlRenderer;
Expand Down Expand Up @@ -83,38 +84,19 @@ public function processTable(Text $text, array $options = array())
$this->escapePipes($rule);
$this->escapePipes($body);

$baseTags = $this->createBaseTags($rule->split('/\|/'));
try {
$baseTags = $this->createBaseTags($rule->split('/\|/'));
$headerCells = $this->parseHeader($header, $baseTags);
$bodyRows = $this->parseBody($body, $baseTags);
} catch (SyntaxError $e) {
if ($options['strict']) {
throw $e;
}

$headerCells = new Collection();
$bodyRows = new Collection();

$header->split('/\|/')->each(function (Text $cell, $index) use ($baseTags, &$headerCells) {
/* @var Tag $tag */
$tag = clone $baseTags->get($index);
$tag->setName('th');
$this->markdown->emit('inline', array($cell));
$tag->setText($cell->trim());

$headerCells->add($tag);
});

$body->split('/\n/')->each(function (Text $row) use ($baseTags, &$bodyRows) {
$row->trim()->trim('|');
$cells = new Collection();
$row->split('/\|/')->each(function (Text $cell, $index) use (&$baseTags, &$cells) {
/* @var Tag $tag */
$tag = clone $baseTags->get($index);
$this->markdown->emit('inline', array($cell));
$tag->setText($cell->trim());

$cells->add($tag);
});

$bodyRows->add($cells);
});
return $w;
}

$html = $this->createView($headerCells, $bodyRows);

$this->unescapePipes($html);

return $html . "\n\n";
Expand Down Expand Up @@ -178,6 +160,74 @@ protected function createBaseTags(Collection $rules)
return $baseTags;
}

/**
* @param Text $header
* @param Collection $baseTags
*
* @throws \Ciconia\Exception\SyntaxError
*
* @return Collection
*/
protected function parseHeader(Text $header, Collection $baseTags)
{
$cells = new Collection();

$header->split('/\|/')->each(function (Text $cell, $index) use ($baseTags, &$cells) {
/* @var Tag $tag */
$tag = clone $baseTags->get($index);
$tag->setName('th');
$this->markdown->emit('inline', array($cell));
$tag->setText($cell->trim());

$cells->add($tag);
});

if ($baseTags->count() != $cells->count()) {
throw new SyntaxError(
'Unexpected number of table cells in header.',
$this, $header, $this->markdown
);
}

return $cells;
}

/**
* @param Text $body
* @param Collection $baseTags
*
* @return Collection
*/
protected function parseBody(Text $body, Collection $baseTags)
{
$rows = new Collection();

$body->split('/\n/')->each(function (Text $row) use ($baseTags, &$rows) {
$row->trim()->trim('|');

$cells = new Collection();
$row->split('/\|/')->each(function (Text $cell, $index) use (&$baseTags, &$cells) {
/* @var Tag $tag */
$tag = clone $baseTags->get($index);
$this->markdown->emit('inline', array($cell));
$tag->setText($cell->trim());

$cells->add($tag);
});

if ($baseTags->count() != $cells->count()) {
throw new SyntaxError(
'Unexpected number of table cells in body.',
$this, $row, $this->markdown
);
}

$rows->add($cells);
});

return $rows;
}

/**
* @param Text $text
*/
Expand Down
2 changes: 2 additions & 0 deletions test/Ciconia/CiconiaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ public function strictModeProvider()
public function testStrictModeDisabled($name, $markdown, $expected)
{
$md = new \Ciconia\Ciconia();
$md->addExtension(new \Ciconia\Extension\Gfm\TableExtension());

$expected = str_replace("\r\n", "\n", $expected);
$expected = str_replace("\r", "\n", $expected);
Expand All @@ -228,6 +229,7 @@ public function testStrictModeDisabled($name, $markdown, $expected)
public function testReferenceLinkHasInvalidIdOnStrictMode($name, $markdown, $expected)
{
$md = new \Ciconia\Ciconia();
$md->addExtension(new \Ciconia\Extension\Gfm\TableExtension());

$md->render($markdown, array('strict' => true));
}
Expand Down
3 changes: 3 additions & 0 deletions test/Ciconia/Resources/core/strict/table-invalid-body.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
th | th | th
---|----|----
td | td
3 changes: 3 additions & 0 deletions test/Ciconia/Resources/core/strict/table-invalid-body.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>th | th | th
---|----|----
td | td</p>
3 changes: 3 additions & 0 deletions test/Ciconia/Resources/core/strict/table-invalid-header.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
th | th
---|----|----
td | td | td
3 changes: 3 additions & 0 deletions test/Ciconia/Resources/core/strict/table-invalid-header.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>th | th
---|----|----
td | td | td</p>

0 comments on commit f4646a2

Please sign in to comment.