10000 [Console] SymfonyStyle: Escape trailing backslashes in user texts · symfony/symfony@fd23354 · GitHub
[go: up one dir, main page]

Skip to content

Commit fd23354

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

File tree

6 files changed

+45
-6
lines changed

6 files changed

+45
-6
lines changed

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

Lines changed: 12 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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();
Lines changed: 13 additions & 0 deletions
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+
};
Lines changed: 7 additions & 0 deletions
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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@ public function testAskReturnsNullIfValidatorAllowsIt()
7979
$questionHelper = new SymfonyQuestionHelper();
8080
$questionHelper->setInputStream($this->getInputStream("\n"));
8181
$question = new Question('What is your favorite superhero?');
82-
$question->setValidator(function ($value) {
83-
return $value;
84-
});
82+
$question->setValidator(function ($value) { return $value; });
8583
$this->assertNull($questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
8684
}
8785

@@ -103,6 +101,15 @@ public function testAskEscapeAndFormatLabel()
103101
$this->assertOutputContains('Do you want to use Foo\\Bar or Foo\\Baz\\? [Foo\\Baz]:', $output);
104102
}
105103

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

0 commit comments

Comments
 (0)
0