diff --git a/src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php b/src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php
index 8abec46007e49..363449534049a 100644
--- a/src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php
+++ b/src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php
@@ -17,6 +17,7 @@
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
+use Symfony\Component\Console\Formatter\OutputFormatter;
/**
* Symfony Style Guide compliant question helper.
@@ -52,7 +53,7 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu
*/
protected function writePrompt(OutputInterface $output, Question $question)
{
- $text = $question->getQuestion();
+ $text = OutputFormatter::escape($question->getQuestion());
$default = $question->getDefault();
switch (true) {
@@ -74,18 +75,18 @@ protected function writePrompt(OutputInterface $output, Question $question)
$default[$key] = $choices[trim($value)];
}
- $text = sprintf(' %s [%s]:', $text, implode(', ', $default));
+ $text = sprintf(' %s [%s]:', $text, OutputFormatter::escape(implode(', ', $default)));
break;
case $question instanceof ChoiceQuestion:
$choices = $question->getChoices();
- $text = sprintf(' %s [%s]:', $text, $choices[$default]);
+ $text = sprintf(' %s [%s]:', $text, OutputFormatter::escape($choices[$default]));
break;
default:
- $text = sprintf(' %s [%s]:', $text, $default);
+ $text = sprintf(' %s [%s]:', $text, OutputFormatter::escape($default));
}
$output->writeln($text);
diff --git a/src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php
index 032e153f068f1..4b49d309784fd 100644
--- a/src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php
+++ b/src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php
@@ -6,6 +6,7 @@
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
use Symfony\Component\Console\Output\StreamOutput;
+use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Question\ChoiceQuestion;
/**
@@ -73,6 +74,24 @@ public function testAskChoice()
$this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
}
+ public function testAskEscapeDefaultValue()
+ {
+ $helper = new SymfonyQuestionHelper();
+ $helper->setInputStream($this->getInputStream('\\'));
+ $helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Can I have a backslash?', '\\'));
+
+ $this->assertOutputContains('Can I have a backslash? [\]', $output);
+ }
+
+ public function testAskEscapeLabel()
+ {
+ $helper = new SymfonyQuestionHelper();
+ $helper->setInputStream($this->getInputStream('sure'));
+ $helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Do you want a \?'));
+
+ $this->assertOutputContains('Do you want a \?', $output);
+ }
+
protected function getInputStream($input)
{
$stream = fopen('php://memory', 'r+', false);