Skip to content

Commit cba6f86

Browse files
ogizanagichalasr
authored andcommitted
[Console] SymfonyStyle: Escape trailing backslashes in user texts
1 parent 9d46712 commit cba6f86

File tree

6 files changed

+44
-3
lines changed

6 files changed

+44
-3
lines changed

src/Symfony/Component/Console/Formatter/OutputFormatter.php

+12
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ public static function escape($text)
3333
{
3434
$text = preg_replace('/([^\\\\]?)</', '$1\\<', $text);
3535

36+
return self::escapeTrailingBackslash($text);
37+
}
38+
39+
/**
40+
* Escapes trailing "\" in given text.
41+
*
42+
* @param string $text Text to escape
43+
*
44+
* @return string Escaped text
45+
*/
46+
public static function escapeTrailingBackslash($text)
47+
{
3648
if ('\\' === substr($text, -1)) {
3749
$len = strlen($text);
3850
$text = rtrim($text, '\\');

src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu
5353
*/
5454
protected function writePrompt(OutputInterface $output, Question $question)
5555
{
56-
$text = $question->getQuestion();
56+
$text = OutputFormatter::escapeTrailingBackslash($question->getQuestion());
5757
$default = $question->getDefault();
5858

5959
switch (true) {

src/Symfony/Component/Console/Style/SymfonyStyle.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function title($message)
121121
{
122122
$this->autoPrependBlock();
123123
$this->writeln(array(
124-
sprintf('<comment>%s</>', $message),
124+
sprintf('<comment>%s</>', OutputFormatter::escapeTrailingBackslash($message)),
125125
sprintf('<comment>%s</>', str_repeat('=', strlen($message))),
126126
));
127127
$this->newLine();
@@ -134,7 +134,7 @@ public function section($message)
134134
{
135135
$this->autoPrependBlock();
136136
$this->writeln(array(
137-
sprintf('<comment>%s</>', $message),
137+
sprintf('<comment>%s</>', OutputFormatter::escapeTrailingBackslash($message)),
138138
sprintf('<comment>%s</>', str_repeat('-', strlen($message))),
139139
));
140140
$this->newLine();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Input\InputInterface;
4+
use Symfony\Component\Console\Output\OutputInterface;
5+
use Symfony\Component\Console\Tests\Style\SymfonyStyleWithForcedLineLength;
6+
7+
//Ensure symfony style helper methods handle trailing backslashes properly when decorating user texts
8+
return function (InputInterface $input, OutputInterface $output) {
9+
$output = new SymfonyStyleWithForcedLineLength($input, $output);
10+
11+
$output->title('Title ending with \\');
12+
$output->section('Section ending with \\');
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
Title ending with \
3+
===================
4+
5+
Section ending with \
6+
---------------------
7+

src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ public function testAskEscapeAndFormatLabel()
103103
$this->assertOutputContains('Do you want to use Foo\\Bar or Foo\\Baz\\? [Foo\\Baz]:', $output);
104104
}
105105

106+
public function testLabelTrailingBackslash()
107+
{
108+
$helper = new SymfonyQuestionHelper();
109+
$helper->setInputStream($this->getInputStream('sure'));
110+
$helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Question with a trailing \\'));
111+
112+
$this->assertOutputContains('Question with a trailing \\', $output);
113+
}
114+
106115
/**
107116
* @expectedException \Symfony\Component\Console\Exception\RuntimeException
108117
* @expectedExceptionMessage Aborted

0 commit comments

Comments
 (0)