[Console] SymfonyStyle: Escape trailing backslashes in user texts
This commit is contained in:
parent
9d46712103
commit
e189183689
@ -33,6 +33,20 @@ class OutputFormatter implements OutputFormatterInterface
|
||||
{
|
||||
$text = preg_replace('/([^\\\\]?)</', '$1\\<', $text);
|
||||
|
||||
return self::escapeTrailingBackslash($text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Escapes trailing "\" in given text.
|
||||
*
|
||||
* @param string $text Text to escape
|
||||
*
|
||||
* @return string Escaped text
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public static function escapeTrailingBackslash($text)
|
||||
{
|
||||
if ('\\' === substr($text, -1)) {
|
||||
$len = strlen($text);
|
||||
$text = rtrim($text, '\\');
|
||||
|
@ -53,7 +53,7 @@ class SymfonyQuestionHelper extends QuestionHelper
|
||||
*/
|
||||
protected function writePrompt(OutputInterface $output, Question $question)
|
||||
{
|
||||
$text = $question->getQuestion();
|
||||
$text = OutputFormatter::escapeTrailingBackslash($question->getQuestion());
|
||||
$default = $question->getDefault();
|
||||
|
||||
switch (true) {
|
||||
|
@ -121,7 +121,7 @@ class SymfonyStyle extends OutputStyle
|
||||
{
|
||||
$this->autoPrependBlock();
|
||||
$this->writeln(array(
|
||||
sprintf('<comment>%s</>', $message),
|
||||
sprintf('<comment>%s</>', OutputFormatter::escapeTrailingBackslash($message)),
|
||||
sprintf('<comment>%s</>', str_repeat('=', strlen($message))),
|
||||
));
|
||||
$this->newLine();
|
||||
@ -134,7 +134,7 @@ class SymfonyStyle extends OutputStyle
|
||||
{
|
||||
$this->autoPrependBlock();
|
||||
$this->writeln(array(
|
||||
sprintf('<comment>%s</>', $message),
|
||||
sprintf('<comment>%s</>', OutputFormatter::escapeTrailingBackslash($message)),
|
||||
sprintf('<comment>%s</>', str_repeat('-', strlen($message))),
|
||||
));
|
||||
$this->newLine();
|
||||
|
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Tests\Style\SymfonyStyleWithForcedLineLength;
|
||||
|
||||
//Ensure symfony style helper methods handle trailing backslashes properly when decorating user texts
|
||||
return function (InputInterface $input, OutputInterface $output) {
|
||||
$output = new SymfonyStyleWithForcedLineLength($input, $output);
|
||||
|
||||
$output->title('Title ending with \\');
|
||||
$output->section('Section ending with \\');
|
||||
};
|
@ -0,0 +1,7 @@
|
||||
|
||||
Title ending with \
|
||||
===================
|
||||
|
||||
Section ending with \
|
||||
---------------------
|
||||
|
@ -79,9 +79,7 @@ class SymfonyQuestionHelperTest extends \PHPUnit_Framework_TestCase
|
||||
$questionHelper = new SymfonyQuestionHelper();
|
||||
$questionHelper->setInputStream($this->getInputStream("\n"));
|
||||
$question = new Question('What is your favorite superhero?');
|
||||
$question->setValidator(function ($value) {
|
||||
return $value;
|
||||
});
|
||||
$question->setValidator(function ($value) { return $value; });
|
||||
$this->assertNull($questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
|
||||
}
|
||||
|
||||
@ -103,6 +101,15 @@ class SymfonyQuestionHelperTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertOutputContains('Do you want to use Foo\\Bar or Foo\\Baz\\? [Foo\\Baz]:', $output);
|
||||
}
|
||||
|
||||
public function testLabelTrailingBackslash()
|
||||
{
|
||||
$helper = new SymfonyQuestionHelper();
|
||||
$helper->setInputStream($this->getInputStream('sure'));
|
||||
$helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Question with a trailing \\'));
|
||||
|
||||
$this->assertOutputContains('Question with a trailing \\', $output);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Console\Exception\RuntimeException
|
||||
* @expectedExceptionMessage Aborted
|
||||
|
Reference in New Issue
Block a user