Skip to content

Commit f681fee

Browse files
tanakahisaterucebe
authored andcommitted
Fix PHP fatal error when maximumNestingLevel was reached.
close #98
1 parent 9d6c36d commit f681fee

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

Parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ protected function parseInline($text)
319319
{
320320
if ($this->_depth >= $this->maximumNestingLevel) {
321321
// maximum depth is reached, do not parse input
322-
return ['text', $text];
322+
return [['text', $text]];
323323
}
324324
$this->_depth++;
325325

tests/ParserTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ public function testMarkerOrder()
4141
$this->assertEquals('Result is A', $parser->parseParagraph('Result is [abc]'));
4242
$this->assertEquals('Result is B', $parser->parseParagraph('Result is [[abc]]'));
4343
}
44+
45+
public function testMaxNestingLevel()
46+
{
47+
$parser = new TestParser();
48+
$parser->markers = [
49+
'[' => 'parseMarkerC',
50+
];
51+
52+
$parser->maximumNestingLevel = 3;
53+
$this->assertEquals("(C-a(C-b(C-c)))", $parser->parseParagraph('[a[b[c]]]'));
54+
$parser->maximumNestingLevel = 2;
55+
$this->assertEquals("(C-a(C-b[c]))", $parser->parseParagraph('[a[b[c]]]'));
56+
$parser->maximumNestingLevel = 1;
57+
$this->assertEquals("(C-a[b[c]])", $parser->parseParagraph('[a[b[c]]]'));
58+
}
4459
}
4560

4661
class TestParser extends Parser
@@ -61,4 +76,11 @@ protected function parseMarkerB($text)
6176
{
6277
return [['text', 'B'], strrpos($text, ']') + 1];
6378
}
79+
80+
protected function parseMarkerC($text)
81+
{
82+
$terminatingMarkerPos = strrpos($text, ']');
83+
$inside = $this->parseInline(substr($text, 1, $terminatingMarkerPos - 1));
84+
return [['text', '(C-' . $this->renderAbsy($inside) . ')'], $terminatingMarkerPos + 1];
85+
}
6486
}

0 commit comments

Comments
 (0)