|
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\Util; |
|
16
|
|
|
|
|
17
|
|
|
use League\CommonMark\Cursor; |
|
18
|
|
|
|
|
19
|
|
|
final class LinkParserHelper |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Attempt to parse link destination |
|
23
|
|
|
* |
|
24
|
|
|
* @param Cursor $cursor |
|
25
|
|
|
* |
|
26
|
|
|
* @return null|string The string, or null if no match |
|
27
|
|
|
*/ |
|
28
|
423 |
|
public static function parseLinkDestination(Cursor $cursor): ?string |
|
29
|
|
|
{ |
|
30
|
423 |
|
if ($res = $cursor->match(RegexHelper::REGEX_LINK_DESTINATION_BRACES)) { |
|
31
|
|
|
// Chop off surrounding <..>: |
|
32
|
30 |
|
return UrlEncoder::unescapeAndEncode( |
|
33
|
30 |
|
RegexHelper::unescape(\substr($res, 1, -1)) |
|
34
|
|
|
); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
396 |
|
if ($cursor->getCharacter() === '<') { |
|
38
|
9 |
|
return null; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
387 |
|
$destination = self::manuallyParseLinkDestination($cursor); |
|
42
|
387 |
|
if ($destination === null) { |
|
43
|
3 |
|
return null; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
384 |
|
return UrlEncoder::unescapeAndEncode( |
|
47
|
384 |
|
RegexHelper::unescape($destination) |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param Cursor $cursor |
|
53
|
|
|
* |
|
54
|
|
|
* @return int |
|
55
|
|
|
*/ |
|
56
|
429 |
|
public static function parseLinkLabel(Cursor $cursor): int |
|
57
|
|
|
{ |
|
58
|
429 |
|
$match = $cursor->match('/^\[(?:[^\\\\\[\]]|\\\\.){0,1000}\]/'); |
|
59
|
429 |
|
if ($match === null) { |
|
60
|
228 |
|
return 0; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
402 |
|
$length = \mb_strlen($match, 'utf-8'); |
|
64
|
|
|
|
|
65
|
402 |
|
if ($length > 1001) { |
|
66
|
|
|
return 0; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
402 |
|
return $length; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Attempt to parse link title (sans quotes) |
|
74
|
|
|
* |
|
75
|
|
|
* @param Cursor $cursor |
|
76
|
|
|
* |
|
77
|
|
|
* @return null|string The string, or null if no match |
|
78
|
|
|
*/ |
|
79
|
183 |
|
public static function parseLinkTitle(Cursor $cursor): ?string |
|
80
|
|
|
{ |
|
81
|
183 |
|
if ($title = $cursor->match('/' . RegexHelper::PARTIAL_LINK_TITLE . '/')) { |
|
82
|
|
|
// Chop off quotes from title and unescape |
|
83
|
138 |
|
return RegexHelper::unescape(\substr($title, 1, -1)); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
45 |
|
return null; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param Cursor $cursor |
|
91
|
|
|
* |
|
92
|
|
|
* @return string|null |
|
93
|
|
|
*/ |
|
94
|
387 |
|
private static function manuallyParseLinkDestination(Cursor $cursor): ?string |
|
95
|
|
|
{ |
|
96
|
387 |
|
$oldPosition = $cursor->getPosition(); |
|
97
|
387 |
|
$oldState = $cursor->saveState(); |
|
98
|
|
|
|
|
99
|
387 |
|
$openParens = 0; |
|
100
|
387 |
|
while (($c = $cursor->getCharacter()) !== null) { |
|
101
|
384 |
|
if ($c === '\\' && RegexHelper::isEscapable($cursor->peek())) { |
|
102
|
18 |
|
$cursor->advanceBy(2); |
|
103
|
384 |
|
} elseif ($c === '(') { |
|
104
|
18 |
|
$cursor->advance(); |
|
105
|
18 |
|
$openParens++; |
|
106
|
384 |
|
} elseif ($c === ')') { |
|
107
|
120 |
|
if ($openParens < 1) { |
|
108
|
117 |
|
break; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
18 |
|
$cursor->advance(); |
|
112
|
18 |
|
$openParens--; |
|
113
|
381 |
|
} elseif (\preg_match(RegexHelper::REGEX_WHITESPACE_CHAR, $c)) { |
|
114
|
177 |
|
break; |
|
115
|
|
|
} else { |
|
116
|
381 |
|
$cursor->advance(); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
387 |
|
if ($cursor->getPosition() === $oldPosition && $c !== ')') { |
|
121
|
3 |
|
return null; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
384 |
|
$newPos = $cursor->getPosition(); |
|
125
|
384 |
|
$cursor->restoreState($oldState); |
|
126
|
|
|
|
|
127
|
384 |
|
$cursor->advanceBy($newPos - $cursor->getPosition()); |
|
128
|
|
|
|
|
129
|
384 |
|
return $cursor->getPreviousText(); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|