Skip to content

Commit a233106

Browse files
authored
Merge pull request #1333 from lacatoire/fix/textrole-null-backslash
Guard against null token after trailing backslash in text role
2 parents b324863 + 9642f64 commit a233106

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineRules/TextRoleRule.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ public function apply(BlockContext $blockContext, InlineLexer $lexer): InlineNod
7676

7777
break;
7878
case InlineLexer::BACKSLASH:
79-
$rawPart .= $token->value;
8079
$lexer->moveNext();
80+
if ($lexer->token === null) {
81+
break 2;
82+
}
83+
84+
$rawPart .= $token->value;
8185
$part .= $lexer->token->value;
8286
$rawPart .= $lexer->token->value;
8387

packages/guides-restructured-text/tests/unit/Parser/Productions/InlineRules/TextRoleRuleTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,31 @@ public function __construct(
148148
*/
149149
self::assertSame($expectedRawContent, $node->rawContent);
150150
}
151+
152+
public function testApplyReturnsNullOnTrailingBackslashWithoutRaisingWarning(): void
153+
{
154+
$input = ':role:`text\\';
155+
156+
$textRoleFactory = $this->createMock(TextRoleFactory::class);
157+
$textRoleFactory->expects(self::never())->method('getTextRole');
158+
159+
$lexer = new InlineLexer();
160+
$lexer->setInput($input);
161+
$lexer->moveNext();
162+
$lexer->moveNext();
163+
164+
$textRoleRule = new TextRoleRule();
165+
self::assertTrue($textRoleRule->applies($lexer));
166+
167+
$documentParserContext = new DocumentParserContext(
168+
self::createStub(ParserContext::class),
169+
$textRoleFactory,
170+
self::createStub(MarkupLanguageParser::class),
171+
);
172+
173+
self::assertNull($textRoleRule->apply(
174+
new BlockContext($documentParserContext, ''),
175+
$lexer,
176+
));
177+
}
151178
}

0 commit comments

Comments
 (0)