This commit is contained in:
Paolo A
2024-08-13 13:44:16 +00:00
parent 1bbb23088d
commit e796d76612
4001 changed files with 30101 additions and 40075 deletions

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
/*
* This file is part of the league/commonmark package.
*
@@ -14,16 +16,17 @@
namespace League\CommonMark\Util;
use League\CommonMark\Cursor;
use League\CommonMark\Parser\Cursor;
/**
* @psalm-immutable
*/
final class LinkParserHelper
{
/**
* Attempt to parse link destination
*
* @param Cursor $cursor
*
* @return null|string The string, or null if no match
* @return string|null The string, or null if no match
*/
public static function parseLinkDestination(Cursor $cursor): ?string
{
@@ -34,7 +37,7 @@ final class LinkParserHelper
);
}
if ($cursor->getCharacter() === '<') {
if ($cursor->getCurrentCharacter() === '<') {
return null;
}
@@ -55,7 +58,7 @@ final class LinkParserHelper
return 0;
}
$length = \mb_strlen($match, 'utf-8');
$length = \mb_strlen($match, 'UTF-8');
if ($length > 1001) {
return 0;
@@ -64,12 +67,15 @@ final class LinkParserHelper
return $length;
}
public static function parsePartialLinkLabel(Cursor $cursor): ?string
{
return $cursor->match('/^(?:[^\\\\\[\]]+|\\\\.?)*/');
}
/**
* Attempt to parse link title (sans quotes)
*
* @param Cursor $cursor
*
* @return null|string The string, or null if no match
* @return string|null The string, or null if no match
*/
public static function parseLinkTitle(Cursor $cursor): ?string
{
@@ -81,14 +87,25 @@ final class LinkParserHelper
return null;
}
public static function parsePartialLinkTitle(Cursor $cursor, string $endDelimiter): ?string
{
$endDelimiter = \preg_quote($endDelimiter, '/');
$regex = \sprintf('/(%s|[^%s\x00])*(?:%s)?/', RegexHelper::PARTIAL_ESCAPED_CHAR, $endDelimiter, $endDelimiter);
if (($partialTitle = $cursor->match($regex)) === null) {
return null;
}
return RegexHelper::unescape($partialTitle);
}
private static function manuallyParseLinkDestination(Cursor $cursor): ?string
{
$oldPosition = $cursor->getPosition();
$oldState = $cursor->saveState();
$oldState = $cursor->saveState();
$openParens = 0;
while (($c = $cursor->getCharacter()) !== null) {
if ($c === '\\' && $cursor->peek() !== null && RegexHelper::isEscapable($cursor->peek())) {
while (($c = $cursor->getCurrentCharacter()) !== null) {
if ($c === '\\' && ($peek = $cursor->peek()) !== null && RegexHelper::isEscapable($peek)) {
$cursor->advanceBy(2);
} elseif ($c === '(') {
$cursor->advanceBy(1);
@@ -111,7 +128,7 @@ final class LinkParserHelper
return null;
}
if ($cursor->getPosition() === $oldPosition && $c !== ')') {
if ($cursor->getPosition() === $oldPosition && (! isset($c) || $c !== ')')) {
return null;
}