Issues (33)

CommonMark/Parser/Block/ListBlockStartParser.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace League\CommonMark\Extension\CommonMark\Parser\Block;
15
16
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock;
17
use League\CommonMark\Extension\CommonMark\Node\Block\ListData;
18
use League\CommonMark\Parser\Block\BlockStart;
19
use League\CommonMark\Parser\Block\BlockStartParserInterface;
20
use League\CommonMark\Parser\Cursor;
21
use League\CommonMark\Parser\MarkdownParserStateInterface;
22
use League\CommonMark\Util\RegexHelper;
23
use League\Config\ConfigurationAwareInterface;
24
use League\Config\ConfigurationInterface;
25
26
final class ListBlockStartParser implements BlockStartParserInterface, ConfigurationAwareInterface
27
{
28
    /**
29
     * @var ConfigurationInterface|null
30
     *
31
     * @psalm-readonly-allow-private-mutation
32
     */
33
    private $config;
34
35
    /**
36
     * @var string|null
37
     *
38
     * @psalm-readonly-allow-private-mutation
39
     */
40
    private $listMarkerRegex;
41
42 3009
    public function setConfiguration(ConfigurationInterface $configuration): void
43
    {
44 3009
        $this->config = $configuration;
45 3009
    }
46
47 1800
    public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart
48
    {
49 1800
        if ($cursor->isIndented()) {
50 195
            return BlockStart::none();
51
        }
52
53 1680
        $listData = $this->parseList($cursor, $parserState->getParagraphContent() !== null);
54 1677
        if ($listData === null) {
55 1413
            return BlockStart::none();
56
        }
57
58 294
        $listItemParser = new ListItemParser($listData);
59
60
        // prepend the list block if needed
61 294
        $matched = $parserState->getLastMatchedBlockParser();
62 294
        if (! ($matched instanceof ListBlockParser) || ! $listData->equals($matched->getBlock()->getListData())) {
63 294
            $listBlockParser = new ListBlockParser($listData);
64
            // We start out with assuming a list is tight. If we find a blank line, we set it to loose later.
65 294
            $listBlockParser->getBlock()->setTight(true);
66
67 294
            return BlockStart::of($listBlockParser, $listItemParser)->at($cursor);
68
        }
69
70 105
        return BlockStart::of($listItemParser)->at($cursor);
71
    }
72
73 1680
    private function parseList(Cursor $cursor, bool $inParagraph): ?ListData
74
    {
75 1680
        $indent = $cursor->getIndent();
76
77 1680
        $tmpCursor = clone $cursor;
78 1680
        $tmpCursor->advanceToNextNonSpaceOrTab();
79 1680
        $rest = $tmpCursor->getRemainder();
80
81 1680
        if (\preg_match($this->listMarkerRegex ?? $this->generateListMarkerRegex(), $rest) === 1) {
82 507
            $data               = new ListData();
83 507
            $data->markerOffset = $indent;
84 507
            $data->type         = ListBlock::TYPE_BULLET;
85 507
            $data->delimiter    = null;
86 507
            $data->bulletChar   = $rest[0];
87 507
            $markerLength       = 1;
88 1242
        } elseif (($matches = RegexHelper::matchFirst('/^(\d{1,9})([.)])/', $rest)) && (! $inParagraph || $matches[1] === '1')) {
89 99
            $data               = new ListData();
90 99
            $data->markerOffset = $indent;
91 99
            $data->type         = ListBlock::TYPE_ORDERED;
92 99
            $data->start        = (int) $matches[1];
93 99
            $data->delimiter    = $matches[2];
94 99
            $data->bulletChar   = null;
95 99
            $markerLength       = \strlen($matches[0]);
96
        } else {
97 1149
            return null;
98
        }
99
100
        // Make sure we have spaces after
101 582
        $nextChar = $tmpCursor->peek($markerLength);
102 582
        if (! ($nextChar === null || $nextChar === "\t" || $nextChar === ' ')) {
103 291
            return null;
104
        }
105
106
        // If it interrupts paragraph, make sure first line isn't blank
107 300
        if ($inParagraph && ! RegexHelper::matchAt(RegexHelper::REGEX_NON_SPACE, $rest, $markerLength)) {
0 ignored issues
show
Bug Best Practice introduced by Colin O'Dell
The expression League\CommonMark\Util\R..., $rest, $markerLength) of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
108 6
            return null;
109
        }
110
111 294
        $cursor->advanceToNextNonSpaceOrTab(); // to start of marker
112 294
        $cursor->advanceBy($markerLength, true); // to end of marker
113 294
        $data->padding = self::calculateListMarkerPadding($cursor, $markerLength);
114
115 294
        return $data;
116
    }
117
118 294
    private static function calculateListMarkerPadding(Cursor $cursor, int $markerLength): int
119
    {
120 294
        $start          = $cursor->saveState();
121 294
        $spacesStartCol = $cursor->getColumn();
122
123 294
        while ($cursor->getColumn() - $spacesStartCol < 5) {
124 294
            if (! $cursor->advanceBySpaceOrTab()) {
125 285
                break;
126
            }
127
        }
128
129 294
        $blankItem         = $cursor->peek() === null;
130 294
        $spacesAfterMarker = $cursor->getColumn() - $spacesStartCol;
131
132 294
        if ($spacesAfterMarker >= 5 || $spacesAfterMarker < 1 || $blankItem) {
133 81
            $cursor->restoreState($start);
134 81
            $cursor->advanceBySpaceOrTab();
135
136 81
            return $markerLength + 1;
137
        }
138
139 231
        return $markerLength + $spacesAfterMarker;
140
    }
141
142 1680
    private function generateListMarkerRegex(): string
143
    {
144
        // No configuration given - use the defaults
145 1680
        if ($this->config === null) {
146
            return $this->listMarkerRegex = '/^[*+-]/';
147
        }
148
149 1680
        $markers = $this->config->get('commonmark/unordered_list_markers');
150
        \assert(\is_array($markers));
151
152 1677
        return $this->listMarkerRegex = '/^[' . \preg_quote(\implode('', $markers), '/') . ']/';
153
    }
154
}
155