Completed
Push — main ( 167142...ff4e97 )
by Colin
22s queued 13s
created

CloseBracketParser::tryParseInlineLinkAndTitle()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 38
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 38
ccs 20
cts 20
cp 1
rs 9.2888
c 1
b 0
f 0
cc 5
nc 6
nop 1
crap 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 2132
    public function getMatchDefinition(): InlineParserMatch
42
    {
43 2132
        return InlineParserMatch::string(']');
44
    }
45
46 376
    public function parse(InlineParserContext $inlineContext): bool
47
    {
48
        // Look through stack of delimiters for a [ or !
49 376
        $opener = $inlineContext->getDelimiterStack()->searchByCharacter(['[', '!']);
50 376
        if ($opener === null) {
51 8
            return false;
52
        }
53
54 370
        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 370
        $cursor = $inlineContext->getCursor();
62
63 370
        $startPos      = $cursor->getPosition();
64 370
        $previousState = $cursor->saveState();
65
66 370
        $cursor->advanceBy(1);
67
68
        // Check to see if we have a link/image
69
70
        // Inline link?
71 370
        if ($result = $this->tryParseInlineLinkAndTitle($cursor)) {
72 164
            $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 298
        $isImage = $opener->getChar() === '!';
85
86 298
        $inline = $this->createInline($link['url'], $link['title'], $isImage, $reference ?? null);
87 298
        $opener->getInlineNode()->replaceWith($inline);
88 298
        while (($label = $inline->next()) !== null) {
89
            // Is there a Mention or Link contained within this link?
90
            // CommonMark does not allow nested links, so we'll restore the original text.
91 292
            if ($label instanceof Mention) {
92 2
                $label->replaceWith($replacement = new Text($label->getPrefix() . $label->getIdentifier()));
93 2
                $inline->appendChild($replacement);
94 292
            } elseif ($label instanceof Link) {
95 10
                foreach ($label->children() as $child) {
96 10
                    $label->insertBefore($child);
97
                }
98
99 10
                $label->detach();
100
            } else {
101 292
                $inline->appendChild($label);
102
            }
103
        }
104
105
        // Process delimiters such as emphasis inside link/image
106 298
        $delimiterStack = $inlineContext->getDelimiterStack();
107 298
        $stackBottom    = $opener->getPrevious();
108 298
        $delimiterStack->processDelimiters($stackBottom, $this->environment->getDelimiterProcessors());
109 298
        $delimiterStack->removeAll($stackBottom);
110
111
        // Merge any adjacent Text nodes together
112 298
        AdjacentTextMerger::mergeChildNodes($inline);
113
114
        // processEmphasis will remove this and later delimiters.
115
        // Now, for a link, we also remove earlier link openers (no links in links)
116 298
        if (! $isImage) {
117 260
            $inlineContext->getDelimiterStack()->removeEarlierMatches('[');
118
        }
119
120 298
        return true;
121
    }
122
123 2138
    public function setEnvironment(EnvironmentInterface $environment): void
124
    {
125 2138
        $this->environment = $environment;
126 2138
    }
127
128
    /**
129
     * @return array<string, string>|null
130
     */
131 370
    private function tryParseInlineLinkAndTitle(Cursor $cursor): ?array
132
    {
133 370
        if ($cursor->getCurrentCharacter() !== '(') {
134 182
            return null;
135
        }
136
137 196
        $previousState = $cursor->saveState();
138
139 196
        $cursor->advanceBy(1);
140 196
        $cursor->advanceToNextNonSpaceOrNewline();
141 196
        if (($dest = LinkParserHelper::parseLinkDestination($cursor)) === null) {
142 8
            $cursor->restoreState($previousState);
143
144 8
            return null;
145
        }
146
147 192
        $cursor->advanceToNextNonSpaceOrNewline();
148 192
        $previousCharacter = $cursor->peek(-1);
149
        // We know from previous lines that we've advanced at least one space so far, so this next call should never be null
150
        \assert(\is_string($previousCharacter));
151
152 192
        $title = '';
153
        // make sure there's a space before the title:
154 192
        if (\preg_match(RegexHelper::REGEX_WHITESPACE_CHAR, $previousCharacter)) {
155 46
            $title = LinkParserHelper::parseLinkTitle($cursor) ?? '';
156
        }
157
158 192
        $cursor->advanceToNextNonSpaceOrNewline();
159
160 192
        if ($cursor->getCurrentCharacter() !== ')') {
161 32
            $cursor->restoreState($previousState);
162
163 32
            return null;
164
        }
165
166 164
        $cursor->advanceBy(1);
167
168 164
        return ['url' => $dest, 'title' => $title];
169
    }
170
171 216
    private function tryParseReference(Cursor $cursor, ReferenceMapInterface $referenceMap, ?int $openerIndex, int $startPos): ?ReferenceInterface
172
    {
173 216
        if ($openerIndex === null) {
174
            return null;
175
        }
176
177 216
        $savePos     = $cursor->saveState();
178 216
        $beforeLabel = $cursor->getPosition();
179 216
        $n           = LinkParserHelper::parseLinkLabel($cursor);
180 216
        if ($n === 0 || $n === 2) {
181 184
            $start  = $openerIndex;
182 184
            $length = $startPos - $openerIndex;
183
        } else {
184 40
            $start  = $beforeLabel + 1;
185 40
            $length = $n - 2;
186
        }
187
188 216
        $referenceLabel = $cursor->getSubstring($start, $length);
189
190 216
        if ($n === 0) {
191
            // If shortcut reference link, rewind before spaces we skipped
192 168
            $cursor->restoreState($savePos);
193
        }
194
195 216
        return $referenceMap->get($referenceLabel);
196
    }
197
198 298
    private function createInline(string $url, string $title, bool $isImage, ?ReferenceInterface $reference = null): AbstractWebResource
199
    {
200 298
        if ($isImage) {
201 54
            $inline = new Image($url, null, $title);
202
        } else {
203 260
            $inline = new Link($url, null, $title);
204
        }
205
206 298
        if ($reference) {
207 138
            $inline->data->set('reference', $reference);
208
        }
209
210 298
        return $inline;
211
    }
212
}
213