Skip to content

Commit 075539c

Browse files
jaapiolinawolf
authored andcommitted
Fix invalid url parsing
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.
1 parent 534725b commit 075539c

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

packages/guides-restructured-text/src/RestructuredText/Parser/InlineLexer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected function getCatchablePatterns(): array
7474
'|',
7575
'\\*\\*',
7676
'\\*',
77-
'\b(?<!:)[a-z0-9\\.\-+]{2,}:\\/\\/[-a-zA-Z0-9()@:%_\\+.~#?&\\/=]*[-a-zA-Z0-9()@%_\\+~#&\\/=]', // standalone hyperlinks
77+
'\b(?<!:)[a-z0-9\\.\-+]{2,}:\\/\\/[-a-zA-Z0-9@:%_\\+.~#?&\\/=]*[-a-zA-Z0-9@%_\\+~#&\\/=]', // standalone hyperlinks
7878
];
7979
}
8080

packages/guides-restructured-text/tests/unit/Parser/InlineLexerTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PHPUnit\Framework\TestCase;
1818

1919
use function PHPUnit\Framework\assertEquals;
20+
use function trim;
2021

2122
final class InlineLexerTest extends TestCase
2223
{
@@ -83,4 +84,37 @@ public static function inlineLexerProvider(): array
8384
],
8485
];
8586
}
87+
88+
#[DataProvider('hyperlinkProvider')]
89+
public function testHyperlinkEndsBeforeParenthesis(string $url): void
90+
{
91+
$input = '(text in parenthesis ' . $url . ').';
92+
$lexer = new InlineLexer();
93+
94+
$lexer->setInput($input);
95+
$lexer->moveNext();
96+
97+
for ($i = 0; $i < 21; $i++) {
98+
$lexer->moveNext();
99+
assertEquals(
100+
trim($input[$i]) === '' ? InlineLexer::WHITESPACE : InlineLexer::WORD,
101+
$lexer->token?->type,
102+
);
103+
assertEquals($input[$i], $lexer->token?->value);
104+
}
105+
106+
$lexer->moveNext();
107+
assertEquals(InlineLexer::HYPERLINK, $lexer->token?->type);
108+
assertEquals($url, $lexer->token?->value);
109+
}
110+
111+
/** @return array<string, array<string>> */
112+
public static function hyperlinkProvider(): array
113+
{
114+
return [
115+
'Url with parenthesis' => ['https://www.test.com'],
116+
'Url with parenthesis and query' => ['https://www.test.com?query=1'],
117+
'Url with parenthesis and query and fragment' => ['https://www.test.com?query=1#fragment'],
118+
];
119+
}
86120
}

0 commit comments

Comments
 (0)