Issues (85)

src/Util/LinkParserHelper.php (1 issue)

Severity
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\Util;
18
19
use League\CommonMark\Parser\Cursor;
20
21
/**
22
 * @psalm-immutable
23
 */
24
final class LinkParserHelper
25
{
26
    /**
27
     * Attempt to parse link destination
28
     *
29
     * @return string|null The string, or null if no match
30
     */
31
    public static function parseLinkDestination(Cursor $cursor): ?string
32
    {
33
        if ($cursor->getCurrentCharacter() === '<') {
34
            return self::parseDestinationBraces($cursor);
35
        }
36
37
        $destination = self::manuallyParseLinkDestination($cursor);
38
        if ($destination === null) {
39
            return null;
40
        }
41
42
        return UrlEncoder::unescapeAndEncode(
43
            RegexHelper::unescape($destination)
44
        );
45
    }
46
47
    public static function parseLinkLabel(Cursor $cursor): int
48
    {
49
        $match = $cursor->match('/^\[(?:[^\\\\\[\]]|\\\\.){0,1000}\]/');
50
        if ($match === null) {
51
            return 0;
52
        }
53
54
        $length = \mb_strlen($match, 'UTF-8');
55
56
        if ($length > 1001) {
57
            return 0;
58
        }
59
60
        return $length;
61
    }
62
63
    public static function parsePartialLinkLabel(Cursor $cursor): ?string
64
    {
65
        return $cursor->match('/^(?:[^\\\\\[\]]++|\\\\.?)*+/');
66
    }
67
68
    /**
69
     * Attempt to parse link title (sans quotes)
70
     *
71
     * @return string|null The string, or null if no match
72
     */
73
    public static function parseLinkTitle(Cursor $cursor): ?string
74
    {
75
        if ($title = $cursor->match('/' . RegexHelper::PARTIAL_LINK_TITLE . '/')) {
76
            // Chop off quotes from title and unescape
77
            return RegexHelper::unescape(\substr($title, 1, -1));
78
        }
79
80
        return null;
81
    }
82
83
    public static function parsePartialLinkTitle(Cursor $cursor, string $endDelimiter): ?string
84
    {
85
        $endDelimiter = \preg_quote($endDelimiter, '/');
86
        $regex        = \sprintf('/(%s|[^%s\x00])*(?:%s)?/', RegexHelper::PARTIAL_ESCAPED_CHAR, $endDelimiter, $endDelimiter);
87
        if (($partialTitle = $cursor->match($regex)) === null) {
88
            return null;
89
        }
90
91
        return RegexHelper::unescape($partialTitle);
92
    }
93
94
    private static function manuallyParseLinkDestination(Cursor $cursor): ?string
95
    {
96
        $remainder  = $cursor->getRemainder();
97
        $openParens = 0;
98
        $len        = \strlen($remainder);
99
        for ($i = 0; $i < $len; $i++) {
100
            $c = $remainder[$i];
101
            if ($c === '\\' && $i + 1 < $len && RegexHelper::isEscapable($remainder[$i + 1])) {
102
                $i++;
103
            } elseif ($c === '(') {
104
                $openParens++;
105
                // Limit to 32 nested parens for pathological cases
106
                if ($openParens > 32) {
107
                    return null;
108
                }
109
            } elseif ($c === ')') {
110
                if ($openParens < 1) {
111
                    break;
112
                }
113
114
                $openParens--;
115
            } elseif (\ord($c) <= 32 && RegexHelper::isWhitespace($c)) {
116
                break;
117
            }
118
        }
119
120
        if ($openParens !== 0) {
0 ignored issues
show
The condition $openParens !== 0 is always false.
Loading history...
121
            return null;
122
        }
123
124
        if ($i === 0 && (! isset($c) || $c !== ')')) {
125
            return null;
126
        }
127
128
        $destination = \substr($remainder, 0, $i);
129
        $cursor->advanceBy(\mb_strlen($destination, 'UTF-8'));
130
131
        return $destination;
132
    }
133
134
    /** @var \WeakReference<Cursor>|null */
135
    private static ?\WeakReference $lastCursor       = null;
136
    private static bool $lastCursorLacksClosingBrace = false;
137
138
    private static function parseDestinationBraces(Cursor $cursor): ?string
139
    {
140
        // Optimization: If we've previously parsed this cursor and returned `null`, we know
141
        // that no closing brace exists, so we can skip the regex entirely. This helps avoid
142
        // certain pathological cases where the regex engine can take a very long time to
143
        // determine that no match exists.
144
        if (self::$lastCursor !== null && self::$lastCursor->get() === $cursor) {
145
            if (self::$lastCursorLacksClosingBrace) {
146
                return null;
147
            }
148
        } else {
149
            self::$lastCursor = \WeakReference::create($cursor);
150
        }
151
152
        if ($res = $cursor->match(RegexHelper::REGEX_LINK_DESTINATION_BRACES)) {
153
            self::$lastCursorLacksClosingBrace = false;
154
155
            // Chop off surrounding <..>:
156
            return UrlEncoder::unescapeAndEncode(
157
                RegexHelper::unescape(\substr($res, 1, -1))
158
            );
159
        }
160
161
        self::$lastCursorLacksClosingBrace = true;
162
163
        return null;
164
    }
165
}
166