8000 [Console] Escape default value in SymfonyStyle::ask() · symfony/symfony@ad76f98 · GitHub
[go: up one dir, main page]

Skip to content

Commit ad76f98

Browse files
committed
[Console] Escape default value in SymfonyStyle::ask()
1 parent b28cd81 commit ad76f98

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Console\Question\ConfirmationQuestion;
1818
use Symfony\Component\Console\Question\Question;
1919
use Symfony\Component\Console\Style\SymfonyStyle;
20+
use Symfony\Component\Console\Formatter\OutputFormatter;
2021

2122
/**
2223
* Symfony Style Guide compliant question helper.
@@ -55,6 +56,10 @@ protected function writePrompt(OutputInterface $output, Question $question)
5556
$text = $question->getQuestion();
5657
$default = $question->getDefault();
5758

59+
if (null !== $default) {
60+
$default = OutputFormatter::escape($default);
61+
}
62+
5863
switch (true) {
5964
case null === $default:
6065
$text = sprintf(' <info>%s</info>:', $text);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Symfony\Component\Console\Helper\HelperSet;
77
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
88
use Symfony\Component\Console\Output\StreamOutput;
9+
use Symfony\Component\Console\Question\Question;
910
use Symfony\Component\Console\Question\ChoiceQuestion;
1011

1112
/**
@@ -73,6 +74,15 @@ public function testAskChoice()
7374
$this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
7475
}
7576

77+
public function testAskEscapeDefaultValue()
78+
{
79+
$helper = new SymfonyQuestionHelper();
80+
$helper->setInputStream($this->getInputStream('\\'));
81+
$helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Can I have a backslash?', '\\'));
82+
83+
$this->assertOutputContains('Can I have a backslash? [\]', $output);
84+
}
85+
7686
protected function getInputStream($input)
7787
{
7888
$stream = fopen('php://memory', 'r+', false);

0 commit comments

Comments
 (0)
0