Skip to content

Commit

Permalink
Merge pull request #545 from phpDocumentor/bugfix/reference-errors
Browse files Browse the repository at this point in the history
[BUGFIX] Prevent false positives for named references
  • Loading branch information
jaapio authored Aug 19, 2023
2 parents fedf6a5 + 10d3529 commit f26fd3f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
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],
],
];
}
}

0 comments on commit f26fd3f

Please sign in to comment.