Skip to content

[BUGFIX] Prevent false positives for named references #545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ protected function getType(string &$value)
return self::ANONYMOUSE_REFERENCE;
}

if (preg_match('/[a-z0-9-]+_{1}/i', $value)) {
if (preg_match('/[a-z0-9-]+_{1}(?=\s|$)/i', $value)) {
return self::NAMED_REFERENCE;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace phpDocumentor\Guides\RestructuredText\Parser;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

use function PHPUnit\Framework\assertEquals;

class InlineLexerTest extends TestCase
{
/** @param int[] $result */
#[DataProvider('inlineLexerProvider')]
public function testLexer(string $input, array $result): void
{
$lexer = new InlineLexer();
$lexer->setInput($input);
$lexer->moveNext();
$lexer->moveNext();
foreach ($result as $tokenType) {
assertEquals($tokenType, $lexer->token?->type);
}
}

/** @return array<string, array<string | int[]>> */
public static function inlineLexerProvider(): array
{
return [
'Backtick' => [
'`',
[InlineLexer::BACKTICK],
],
'Normal Url' => [
'http://www.test.com',
[InlineLexer::HYPERLINK],
],
'HTTPS Url' => [
'https://www.test.com',
[InlineLexer::HYPERLINK],
],
'String with underscore' => [
'EXT:css_styled_content/static/v6.2',
[InlineLexer::WORD],
],
'Named Reference' => [
'css_',
[InlineLexer::NAMED_REFERENCE],
],
'Named Reference in sentence' => [
'css_ and something',
[InlineLexer::NAMED_REFERENCE],
],
];
}
}