HtmlBlockParser::parse()   B
last analyzed

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\Block\Parser;
16
17
use League\CommonMark\Block\Element\HtmlBlock;
18
use League\CommonMark\Block\Element\Paragraph;
19
use League\CommonMark\ContextInterface;
20
use League\CommonMark\Cursor;
21
use League\CommonMark\Util\RegexHelper;
22
23
final class HtmlBlockParser implements BlockParserInterface
24
{
25
    /**
26
     * @param ContextInterface $context
27
     * @param Cursor           $cursor
28
     *
29
     * @return bool
30
     */
31 1938
    public function parse(ContextInterface $context, Cursor $cursor): bool
32
    {
33 1938
        if ($cursor->isIndented()) {
34 180
            return false;
35
        }
36
37 1881
        if ($cursor->getNextNonSpaceCharacter() !== '<') {
38 1722
            return false;
39
        }
40
41 279
        $savedState = $cursor->saveState();
42
43 279
        $cursor->advanceToNextNonSpaceOrTab();
44 279
        $line = $cursor->getRemainder();
45
46 279
        for ($blockType = 1; $blockType <= 7; $blockType++) {
47 279
            $match = RegexHelper::matchAt(
48 279
                RegexHelper::getHtmlBlockOpenRegex($blockType),
49 186
                $line
50
            );
51
52 279
            if ($match !== null && ($blockType < 7 || !($context->getContainer() instanceof Paragraph))) {
53 156
                $cursor->restoreState($savedState);
54 156
                $context->addBlock(new HtmlBlock($blockType));
55 156
                $context->setBlocksParsed(true);
56
57 156
                return true;
58
            }
59
        }
60
61 126
        $cursor->restoreState($savedState);
62
63 126
        return false;
64
    }
65
}
66