Skip to content

Commit

Permalink
Implement new max_nesting_level setting (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
colinodell committed Dec 30, 2017
1 parent 68b1737 commit 57107ed
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This release contains several breaking changes and a minimum PHP version bump -

### Added

- Added new `max_nesting_level` setting (#243)
- Added minor performance optimizations to `Cursor`

### Changed
Expand Down
8 changes: 7 additions & 1 deletion src/DocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,19 @@ class DocParser
*/
private $inlineParserEngine;

/**
* @var int|double
*/
private $maxNestingLevel;

/**
* @param Environment $environment
*/
public function __construct(Environment $environment)
{
$this->environment = $environment;
$this->inlineParserEngine = new InlineParserEngine($environment);
$this->maxNestingLevel = $environment->getConfig('max_nesting_level', INF);
}

/**
Expand Down Expand Up @@ -197,7 +203,7 @@ private function parseBlocks(ContextInterface $context, Cursor $cursor)
}
}

if (!$parsed || $context->getContainer()->acceptsLines()) {
if (!$parsed || $context->getContainer()->acceptsLines() || $context->getTip()->getDepth() >= $this->maxNestingLevel) {
$context->setBlocksParsed(true);
break;
}
Expand Down
1 change: 1 addition & 0 deletions src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ public static function createCommonMarkEnvironment()
'safe' => false, // deprecated option
'html_input' => self::HTML_INPUT_ALLOW,
'allow_unsafe_links' => true,
'max_nesting_level' => INF,
]);

return $environment;
Expand Down
15 changes: 15 additions & 0 deletions src/Node/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

abstract class Node
{
/**
* @var int
*/
protected $depth = 0;

/**
* @var Node|null
*/
Expand Down Expand Up @@ -61,6 +66,7 @@ public function parent()
protected function setParent(Node $node = null)
{
$this->parent = $node;
$this->depth = ($node === null) ? 0 : $node->depth + 1;
}

/**
Expand Down Expand Up @@ -129,6 +135,7 @@ public function detach()
$this->parent = null;
$this->next = null;
$this->previous = null;
$this->depth = 0;
}

/**
Expand Down Expand Up @@ -225,6 +232,14 @@ public function replaceChildren(array $children)
return $this;
}

/**
* @return int
*/
public function getDepth()
{
return $this->depth;
}

/**
* @return NodeWalker
*/
Expand Down
38 changes: 38 additions & 0 deletions tests/functional/MaximumNestingLevelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace League\CommonMark\Tests\functional;

use League\CommonMark\CommonMarkConverter;

class MaximumNestingLevelTest extends \PHPUnit_Framework_TestCase
{
public function testThatWeCanHitTheLimit()
{
$converter = new CommonMarkConverter(['max_nesting_level' => 2]);

$markdown = '> > Foo';
$expected = '<blockquote>
<blockquote>
<p>Foo</p>
</blockquote>
</blockquote>
';

$this->assertEquals($expected, $converter->convertToHtml($markdown));
}

public function testThatWeCannotExceedTheLimit()
{
$converter = new CommonMarkConverter(['max_nesting_level' => 2]);

$markdown = '> > > > > > Foo';
$expected = '<blockquote>
<blockquote>
<p>&gt; &gt; &gt; &gt; Foo</p>
</blockquote>
</blockquote>
';

$this->assertEquals($expected, $converter->convertToHtml($markdown));
}
}

0 comments on commit 57107ed

Please sign in to comment.