Passed
Branch main (61f4ef)
by Colin
02:24
created

CloseBracketParser   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Test Coverage

Coverage 98.78%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 23
eloc 81
c 1
b 0
f 0
dl 0
loc 169
ccs 81
cts 82
cp 0.9878
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createInline() 0 13 3
A setEnvironment() 0 3 1
A getMatchDefinition() 0 3 1
B parse() 0 69 8
A tryParseInlineLinkAndTitle() 0 38 5
A tryParseReference() 0 25 5
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
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
11
 *  - (c) John MacFarlane
12
 *
13
 * For the full copyright and license information, please view the LICENSE
14
 * file that was distributed with this source code.
15
 */
16
17
namespace League\CommonMark\Extension\CommonMark\Parser\Inline;
18
19
use League\CommonMark\Environment\EnvironmentAwareInterface;
20
use League\CommonMark\Environment\EnvironmentInterface;
21
use League\CommonMark\Extension\CommonMark\Node\Inline\AbstractWebResource;
22
use League\CommonMark\Extension\CommonMark\Node\Inline\Image;
23
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
24
use League\CommonMark\Extension\Mention\Mention;
25
use League\CommonMark\Node\Inline\AdjacentTextMerger;
26
use League\CommonMark\Node\Inline\Text;
27
use League\CommonMark\Parser\Cursor;
28
use League\CommonMark\Parser\Inline\InlineParserInterface;
29
use League\CommonMark\Parser\Inline\InlineParserMatch;
30
use League\CommonMark\Parser\InlineParserContext;
31
use League\CommonMark\Reference\ReferenceInterface;
32
use League\CommonMark\Reference\ReferenceMapInterface;
33
use League\CommonMark\Util\LinkParserHelper;
34
use League\CommonMark\Util\RegexHelper;
35
36
final class CloseBracketParser implements InlineParserInterface, EnvironmentAwareInterface
37
{
38
    /** @psalm-readonly-allow-private-mutation */
39
    private EnvironmentInterface $environment;
40
41 2124
    public function getMatchDefinition(): InlineParserMatch
42
    {
43 2124
        return InlineParserMatch::string(']');
44
    }
45
46 368
    public function parse(InlineParserContext $inlineContext): bool
47
    {
48
        // Look through stack of delimiters for a [ or !
49 368
        $opener = $inlineContext->getDelimiterStack()->searchByCharacter(['[', '!']);
50 368
        if ($opener === null) {
51 8
            return false;
52
        }
53
54 362
        if (! $opener->isActive()) {
55
            // no matched opener; remove from emphasis stack
56 12
            $inlineContext->getDelimiterStack()->removeDelimiter($opener);
57
58 12
            return false;
59
        }
60
61 362
        $cursor = $inlineContext->getCursor();
62
63 362
        $startPos      = $cursor->getPosition();
64 362
        $previousState = $cursor->saveState();
65
66 362
        $cursor->advanceBy(1);
67
68
        // Check to see if we have a link/image
69
70
        // Inline link?
71 362
        if ($result = $this->tryParseInlineLinkAndTitle($cursor)) {
72 156
            $link = $result;
73 216
        } elseif ($link = $this->tryParseReference($cursor, $inlineContext->getReferenceMap(), $opener->getIndex(), $startPos)) {
74 138
            $reference = $link;
75 138
            $link      = ['url' => $link->getDestination(), 'title' => $link->getTitle()];
76
        } else {
77
            // No match
78 92
            $inlineContext->getDelimiterStack()->removeDelimiter($opener); // Remove this opener from stack
79 92
            $cursor->restoreState($previousState);
80
81 92
            return false;
82
        }
83
84 290
        $isImage = $opener->getChar() === '!';
85
86 290
        $inline = $this->createInline($link['url'], $link['title'], $isImage, $reference ?? null);
87 290
        $opener->getInlineNode()->replaceWith($inline);
88 290
        while (($label = $inline->next()) !== null) {
89
            // Is there a Mention contained within this link?
90
            // CommonMark does not allow nested links, so we'll restore the original text.
91 284
            if ($label instanceof Mention) {
92 2
                $label->replaceWith($replacement = new Text($label->getPrefix() . $label->getIdentifier()));
93 2
                $label = $replacement;
94
            }
95
96 284
            $inline->appendChild($label);
97
        }
98
99
        // Process delimiters such as emphasis inside link/image
100 290
        $delimiterStack = $inlineContext->getDelimiterStack();
101 290
        $stackBottom    = $opener->getPrevious();
102 290
        $delimiterStack->processDelimiters($stackBottom, $this->environment->getDelimiterProcessors());
103 290
        $delimiterStack->removeAll($stackBottom);
104
105
        // Merge any adjacent Text nodes together
106 290
        AdjacentTextMerger::mergeChildNodes($inline);
107
108
        // processEmphasis will remove this and later delimiters.
109
        // Now, for a link, we also remove earlier link openers (no links in links)
110 290
        if (! $isImage) {
111 252
            $inlineContext->getDelimiterStack()->removeEarlierMatches('[');
112
        }
113
114 290
        return true;
115
    }
116
117 2130
    public function setEnvironment(EnvironmentInterface $environment): void
118
    {
119 2130
        $this->environment = $environment;
120 2130
    }
121
122
    /**
123
     * @return array<string, string>|null
124
     */
125 362
    private function tryParseInlineLinkAndTitle(Cursor $cursor): ?array
126
    {
127 362
        if ($cursor->getCurrentCharacter() !== '(') {
128 182
            return null;
129
        }
130
131 188
        $previousState = $cursor->saveState();
132
133 188
        $cursor->advanceBy(1);
134 188
        $cursor->advanceToNextNonSpaceOrNewline();
135 188
        if (($dest = LinkParserHelper::parseLinkDestination($cursor)) === null) {
136 8
            $cursor->restoreState($previousState);
137
138 8
            return null;
139
        }
140
141 184
        $cursor->advanceToNextNonSpaceOrNewline();
142 184
        $previousCharacter = $cursor->peek(-1);
143
        // We know from previous lines that we've advanced at least one space so far, so this next call should never be null
144
        \assert(\is_string($previousCharacter));
145
146 184
        $title = '';
147
        // make sure there's a space before the title:
148 184
        if (\preg_match(RegexHelper::REGEX_WHITESPACE_CHAR, $previousCharacter)) {
149 46
            $title = LinkParserHelper::parseLinkTitle($cursor) ?? '';
150
        }
151
152 184
        $cursor->advanceToNextNonSpaceOrNewline();
153
154 184
        if ($cursor->getCurrentCharacter() !== ')') {
155 32
            $cursor->restoreState($previousState);
156
157 32
            return null;
158
        }
159
160 156
        $cursor->advanceBy(1);
161
162 156
        return ['url' => $dest, 'title' => $title];
163
    }
164
165 216
    private function tryParseReference(Cursor $cursor, ReferenceMapInterface $referenceMap, ?int $openerIndex, int $startPos): ?ReferenceInterface
166
    {
167 216
        if ($openerIndex === null) {
168
            return null;
169
        }
170
171 216
        $savePos     = $cursor->saveState();
172 216
        $beforeLabel = $cursor->getPosition();
173 216
        $n           = LinkParserHelper::parseLinkLabel($cursor);
174 216
        if ($n === 0 || $n === 2) {
175 184
            $start  = $openerIndex;
176 184
            $length = $startPos - $openerIndex;
177
        } else {
178 40
            $start  = $beforeLabel + 1;
179 40
            $length = $n - 2;
180
        }
181
182 216
        $referenceLabel = $cursor->getSubstring($start, $length);
183
184 216
        if ($n === 0) {
185
            // If shortcut reference link, rewind before spaces we skipped
186 168
            $cursor->restoreState($savePos);
187
        }
188
189 216
        return $referenceMap->get($referenceLabel);
190
    }
191
192 290
    private function createInline(string $url, string $title, bool $isImage, ?ReferenceInterface $reference = null): AbstractWebResource
193
    {
194 290
        if ($isImage) {
195 54
            $inline = new Image($url, null, $title);
196
        } else {
197 252
            $inline = new Link($url, null, $title);
198
        }
199
200 290
        if ($reference) {
201 138
            $inline->data->set('reference', $reference);
202
        }
203
204 290
        return $inline;
205
    }
206
}
207