Completed
Push — master ( 0b0b24...54ff37 )
by Colin
02:37
created

HtmlBlockParser::parse()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 19
cts 19
cp 1
rs 8.4426
c 0
b 0
f 0
cc 7
nc 5
nop 2
crap 7
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
9
 *  - (c) John MacFarlane
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace League\CommonMark\Extension\CommonMark\Parser\Block;
16
17
use League\CommonMark\Extension\CommonMark\Node\Block\HtmlBlock;
18
use League\CommonMark\Node\Block\Paragraph;
19
use League\CommonMark\Parser\Block\BlockParserInterface;
20
use League\CommonMark\Parser\ContextInterface;
21
use League\CommonMark\Parser\Cursor;
22
use League\CommonMark\Util\RegexHelper;
23
24
final class HtmlBlockParser implements BlockParserInterface
25
{
26 2337
    public function parse(ContextInterface $context, Cursor $cursor): bool
27
    {
28 2337
        if ($cursor->isIndented()) {
29 183
            return false;
30
        }
31
32 2280
        if ($cursor->getNextNonSpaceCharacter() !== '<') {
33 2112
            return false;
34
        }
35
36 300
        $savedState = $cursor->saveState();
37
38 300
        $cursor->advanceToNextNonSpaceOrTab();
39 300
        $line = $cursor->getRemainder();
40
41 300
        for ($blockType = 1; $blockType <= 7; $blockType++) {
42 300
            $match = RegexHelper::matchAt(
43 300
                RegexHelper::getHtmlBlockOpenRegex($blockType),
44 100
                $line
45
            );
46
47 300
            if ($match !== null && ($blockType < 7 || !($context->getContainer() instanceof Paragraph))) {
48 168
                $cursor->restoreState($savedState);
49 168
                $context->addBlock(new HtmlBlock($blockType));
50 168
                $context->setBlocksParsed(true);
51
52 168
                return true;
53
            }
54
        }
55
56 144
        $cursor->restoreState($savedState);
57
58 144
        return false;
59
    }
60
}
61