Skip to content

Commit

Permalink
Fix invalid url parsing
Browse files Browse the repository at this point in the history
I did not found any specification where an URL can contain parenthesis
in the url, nor we do have a test that fails on this case. If we need
it anyway we can introduce it later on. But for now this fixes a bug
where the fetch of a url is way to gredy.
  • Loading branch information
jaapio authored and linawolf committed Aug 30, 2024
1 parent 534725b commit 075539c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected function getCatchablePatterns(): array
'|',
'\\*\\*',
'\\*',
'\b(?<!:)[a-z0-9\\.\-+]{2,}:\\/\\/[-a-zA-Z0-9()@:%_\\+.~#?&\\/=]*[-a-zA-Z0-9()@%_\\+~#&\\/=]', // standalone hyperlinks
'\b(?<!:)[a-z0-9\\.\-+]{2,}:\\/\\/[-a-zA-Z0-9@:%_\\+.~#?&\\/=]*[-a-zA-Z0-9@%_\\+~#&\\/=]', // standalone hyperlinks
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PHPUnit\Framework\TestCase;

use function PHPUnit\Framework\assertEquals;
use function trim;

final class InlineLexerTest extends TestCase
{
Expand Down Expand Up @@ -83,4 +84,37 @@ public static function inlineLexerProvider(): array
],
];
}

#[DataProvider('hyperlinkProvider')]
public function testHyperlinkEndsBeforeParenthesis(string $url): void
{
$input = '(text in parenthesis ' . $url . ').';
$lexer = new InlineLexer();

$lexer->setInput($input);
$lexer->moveNext();

for ($i = 0; $i < 21; $i++) {
$lexer->moveNext();
assertEquals(
trim($input[$i]) === '' ? InlineLexer::WHITESPACE : InlineLexer::WORD,
$lexer->token?->type,
);
assertEquals($input[$i], $lexer->token?->value);
}

$lexer->moveNext();
assertEquals(InlineLexer::HYPERLINK, $lexer->token?->type);
assertEquals($url, $lexer->token?->value);
}

/** @return array<string, array<string>> */
public static function hyperlinkProvider(): array
{
return [
'Url with parenthesis' => ['https://www.test.com'],
'Url with parenthesis and query' => ['https://www.test.com?query=1'],
'Url with parenthesis and query and fragment' => ['https://www.test.com?query=1#fragment'],
];
}
}

0 comments on commit 075539c

Please sign in to comment.