Skip to content

Commit f4fe36a

Browse files
committed
bug #19961 [Console] Escape question text and default value in SymfonyStyle::ask() (chalasr)
This PR was merged into the 2.7 branch. Discussion ---------- [Console] Escape question text and default value in SymfonyStyle::ask() | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #19493 | License | MIT | Doc PR | n/a Commits ------- eed3cc5 [Console] Escape default value and question in SymfonyStyle::ask()
2 parents 4c854e7 + eed3cc5 commit f4fe36a

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

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

+5-4
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.
@@ -52,7 +53,7 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu
5253
*/
5354
protected function writePrompt(OutputInterface $output, Question $question)
5455
{
55-
$text = $question->getQuestion();
56+
$text = OutputFormatter::escape($question->getQuestion());
5657
$default = $question->getDefault();
5758

5859
switch (true) {
@@ -74,18 +75,18 @@ protected function writePrompt(OutputInterface $output, Question $question)
7475
$default[$key] = $choices[trim($value)];
7576
}
7677

77-
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, implode(', ', $default));
78+
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape(implode(', ', $default)));
7879

7980
break;
8081

8182
case $question instanceof ChoiceQuestion:
8283
$choices = $question->getChoices();
83-
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, $choices[$default]);
84+
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($choices[$default]));
8485

8586
break;
8687

8788
default:
88-
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, $default);
89+
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($default));
8990
}
9091

9192
$output->writeln($text);

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

+18
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,24 @@ public function testAskReturnsNullIfValidatorAllowsIt()
8383
$this->assertNull($questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
8484
}
8585

86+
public function testAskEscapeDefaultValue()
87+
{
88+
$helper = new SymfonyQuestionHelper();
89+
$helper->setInputStream($this->getInputStream('\\'));
90+
$helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Can I have a backslash?', '\\'));
91+
92+
$this->assertOutputContains('Can I have a backslash? [\]', $output);
93+
}
94+
95+
public function testAskEscapeLabel()
96+
{
97+
$helper = new SymfonyQuestionHelper();
98+
$helper->setInputStream($this->getInputStream('sure'));
99+
$helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Do you want a \?'));
100+
101+
$this->assertOutputContains('Do you want a \?', $output);
102+
}
103+
86104
protected function getInputStream($input)
87105
{
88106
$stream = fopen('php://memory', 'r+', false);

0 commit comments

Comments
 (0)