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\Delimiter\DelimiterInterface; |
20
|
|
|
use League\CommonMark\Environment\EnvironmentAwareInterface; |
21
|
|
|
use League\CommonMark\Environment\EnvironmentInterface; |
22
|
|
|
use League\CommonMark\Extension\CommonMark\Node\Inline\AbstractWebResource; |
23
|
|
|
use League\CommonMark\Extension\CommonMark\Node\Inline\Image; |
24
|
|
|
use League\CommonMark\Extension\CommonMark\Node\Inline\Link; |
25
|
|
|
use League\CommonMark\Node\Inline\AdjacentTextMerger; |
26
|
|
|
use League\CommonMark\Parser\Cursor; |
27
|
|
|
use League\CommonMark\Parser\Inline\InlineParserInterface; |
28
|
|
|
use League\CommonMark\Parser\InlineParserContext; |
29
|
|
|
use League\CommonMark\Reference\ReferenceInterface; |
30
|
|
|
use League\CommonMark\Reference\ReferenceMapInterface; |
31
|
|
|
use League\CommonMark\Util\LinkParserHelper; |
32
|
|
|
use League\CommonMark\Util\RegexHelper; |
33
|
|
|
|
34
|
|
|
final class CloseBracketParser implements InlineParserInterface, EnvironmentAwareInterface |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var EnvironmentInterface |
38
|
|
|
* |
39
|
|
|
* @psalm-readonly-allow-private-mutation |
40
|
|
|
*/ |
41
|
|
|
private $environment; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
2496 |
|
public function getCharacters(): array |
47
|
|
|
{ |
48
|
2496 |
|
return [']']; |
49
|
|
|
} |
50
|
|
|
|
51
|
453 |
|
public function parse(InlineParserContext $inlineContext): bool |
52
|
|
|
{ |
53
|
|
|
// Look through stack of delimiters for a [ or ! |
54
|
453 |
|
$opener = $inlineContext->getDelimiterStack()->searchByCharacter(['[', '!']); |
55
|
453 |
|
if ($opener === null) { |
56
|
12 |
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
444 |
|
if (! $opener->isActive()) { |
60
|
|
|
// no matched opener; remove from emphasis stack |
61
|
18 |
|
$inlineContext->getDelimiterStack()->removeDelimiter($opener); |
62
|
|
|
|
63
|
18 |
|
return false; |
64
|
|
|
} |
65
|
|
|
|
66
|
444 |
|
$cursor = $inlineContext->getCursor(); |
67
|
|
|
|
68
|
444 |
|
$startPos = $cursor->getPosition(); |
69
|
444 |
|
$previousState = $cursor->saveState(); |
70
|
|
|
|
71
|
444 |
|
$cursor->advanceBy(1); |
72
|
|
|
|
73
|
|
|
// Check to see if we have a link/image |
74
|
444 |
|
if (! ($link = $this->tryParseLink($cursor, $inlineContext->getReferenceMap(), $opener, $startPos))) { |
75
|
|
|
// No match |
76
|
114 |
|
$inlineContext->getDelimiterStack()->removeDelimiter($opener); // Remove this opener from stack |
77
|
114 |
|
$cursor->restoreState($previousState); |
|
|
|
|
78
|
|
|
|
79
|
114 |
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
360 |
|
$isImage = $opener->getChar() === '!'; |
83
|
|
|
|
84
|
360 |
|
$inline = $this->createInline($link['url'], $link['title'], $isImage); |
85
|
360 |
|
$opener->getInlineNode()->replaceWith($inline); |
86
|
360 |
|
while (($label = $inline->next()) !== null) { |
87
|
357 |
|
$inline->appendChild($label); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// Process delimiters such as emphasis inside link/image |
91
|
360 |
|
$delimiterStack = $inlineContext->getDelimiterStack(); |
92
|
360 |
|
$stackBottom = $opener->getPrevious(); |
93
|
360 |
|
$delimiterStack->processDelimiters($stackBottom, $this->environment->getDelimiterProcessors()); |
94
|
360 |
|
$delimiterStack->removeAll($stackBottom); |
95
|
|
|
|
96
|
|
|
// Merge any adjacent Text nodes together |
97
|
360 |
|
AdjacentTextMerger::mergeChildNodes($inline); |
98
|
|
|
|
99
|
|
|
// processEmphasis will remove this and later delimiters. |
100
|
|
|
// Now, for a link, we also remove earlier link openers (no links in links) |
101
|
360 |
|
if (! $isImage) { |
102
|
306 |
|
$inlineContext->getDelimiterStack()->removeEarlierMatches('['); |
103
|
|
|
} |
104
|
|
|
|
105
|
360 |
|
return true; |
106
|
|
|
} |
107
|
|
|
|
108
|
2496 |
|
public function setEnvironment(EnvironmentInterface $environment): void |
109
|
|
|
{ |
110
|
2496 |
|
$this->environment = $environment; |
111
|
2496 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return array<string, string>|false |
|
|
|
|
115
|
|
|
*/ |
116
|
444 |
|
private function tryParseLink(Cursor $cursor, ReferenceMapInterface $referenceMap, DelimiterInterface $opener, int $startPos) |
117
|
|
|
{ |
118
|
|
|
// Check to see if we have a link/image |
119
|
|
|
// Inline link? |
120
|
444 |
|
if ($result = $this->tryParseInlineLinkAndTitle($cursor)) { |
121
|
165 |
|
return $result; |
122
|
|
|
} |
123
|
|
|
|
124
|
294 |
|
if ($link = $this->tryParseReference($cursor, $referenceMap, $opener->getIndex(), $startPos)) { |
125
|
201 |
|
return ['url' => $link->getDestination(), 'title' => $link->getTitle()]; |
126
|
|
|
} |
127
|
|
|
|
128
|
114 |
|
return false; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return array<string, string>|false |
|
|
|
|
133
|
|
|
*/ |
134
|
444 |
|
private function tryParseInlineLinkAndTitle(Cursor $cursor) |
135
|
|
|
{ |
136
|
444 |
|
if ($cursor->getCharacter() !== '(') { |
137
|
258 |
|
return false; |
138
|
|
|
} |
139
|
|
|
|
140
|
195 |
|
$previousState = $cursor->saveState(); |
141
|
|
|
|
142
|
195 |
|
$cursor->advanceBy(1); |
143
|
195 |
|
$cursor->advanceToNextNonSpaceOrNewline(); |
144
|
195 |
|
if (($dest = LinkParserHelper::parseLinkDestination($cursor)) === null) { |
145
|
9 |
|
$cursor->restoreState($previousState); |
|
|
|
|
146
|
|
|
|
147
|
9 |
|
return false; |
148
|
|
|
} |
149
|
|
|
|
150
|
189 |
|
$cursor->advanceToNextNonSpaceOrNewline(); |
151
|
189 |
|
$previousCharacter = $cursor->peek(-1); |
152
|
|
|
// We know from previous lines that we've advanced at least one space so far, so this next call should never be null |
153
|
189 |
|
\assert(\is_string($previousCharacter)); |
154
|
|
|
|
155
|
189 |
|
$title = ''; |
156
|
|
|
// make sure there's a space before the title: |
157
|
189 |
|
if (\preg_match(RegexHelper::REGEX_WHITESPACE_CHAR, $previousCharacter)) { |
158
|
54 |
|
$title = LinkParserHelper::parseLinkTitle($cursor) ?? ''; |
159
|
|
|
} |
160
|
|
|
|
161
|
189 |
|
$cursor->advanceToNextNonSpaceOrNewline(); |
162
|
|
|
|
163
|
189 |
|
if ($cursor->getCharacter() !== ')') { |
164
|
30 |
|
$cursor->restoreState($previousState); |
|
|
|
|
165
|
|
|
|
166
|
30 |
|
return false; |
167
|
|
|
} |
168
|
|
|
|
169
|
165 |
|
$cursor->advanceBy(1); |
170
|
|
|
|
171
|
165 |
|
return ['url' => $dest, 'title' => $title]; |
172
|
|
|
} |
173
|
|
|
|
174
|
294 |
|
private function tryParseReference(Cursor $cursor, ReferenceMapInterface $referenceMap, ?int $openerIndex, int $startPos): ?ReferenceInterface |
175
|
|
|
{ |
176
|
294 |
|
if ($openerIndex === null) { |
177
|
|
|
return null; |
178
|
|
|
} |
179
|
|
|
|
180
|
294 |
|
$savePos = $cursor->saveState(); |
181
|
294 |
|
$beforeLabel = $cursor->getPosition(); |
182
|
294 |
|
$n = LinkParserHelper::parseLinkLabel($cursor); |
183
|
294 |
|
if ($n === 0 || $n === 2) { |
184
|
243 |
|
$start = $openerIndex; |
185
|
243 |
|
$length = $startPos - $openerIndex; |
186
|
|
|
} else { |
187
|
63 |
|
$start = $beforeLabel + 1; |
188
|
63 |
|
$length = $n - 2; |
189
|
|
|
} |
190
|
|
|
|
191
|
294 |
|
$referenceLabel = $cursor->getSubstring($start, $length); |
192
|
|
|
|
193
|
294 |
|
if ($n === 0) { |
194
|
|
|
// If shortcut reference link, rewind before spaces we skipped |
195
|
219 |
|
$cursor->restoreState($savePos); |
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
198
|
294 |
|
return $referenceMap->get($referenceLabel); |
199
|
|
|
} |
200
|
|
|
|
201
|
360 |
|
private function createInline(string $url, string $title, bool $isImage): AbstractWebResource |
202
|
|
|
{ |
203
|
360 |
|
if ($isImage) { |
204
|
72 |
|
return new Image($url, null, $title); |
205
|
|
|
} |
206
|
|
|
|
207
|
306 |
|
return new Link($url, null, $title); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: