bug #23730 Fixed the escaping of back slashes and << in console output (javiereguiluz)

This PR was squashed before being merged into the 2.7 branch (closes #23730).

Discussion
----------

Fixed the escaping of back slashes and << in console output

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #18481
| License       | MIT
| Doc PR        | -

Not sure if it's a valid solution, but this is my attempt to solve #18481.

Commits
-------

d5cb1fe711 Fixed the escaping of back slashes and << in console output
This commit is contained in:
Fabien Potencier 2017-08-29 13:47:46 -07:00
commit 9c796b4e39
2 changed files with 7 additions and 3 deletions

View File

@ -50,7 +50,8 @@ class OutputFormatter implements OutputFormatterInterface
if ('\\' === substr($text, -1)) {
$len = strlen($text);
$text = rtrim($text, '\\');
$text .= str_repeat('<<', $len - strlen($text));
$text = str_replace("\0", '', $text);
$text .= str_repeat("\0", $len - strlen($text));
}
return $text;
@ -165,8 +166,8 @@ class OutputFormatter implements OutputFormatterInterface
$output .= $this->applyCurrentStyle(substr($message, $offset));
if (false !== strpos($output, '<<')) {
return strtr($output, array('\\<' => '<', '<<' => '\\'));
if (false !== strpos($output, "\0")) {
return strtr($output, array("\0" => '\\', '\\<' => '<'));
}
return str_replace('\\<', '<', $output);

View File

@ -28,6 +28,9 @@ class OutputFormatterTest extends TestCase
$formatter = new OutputFormatter(true);
$this->assertEquals('foo<bar', $formatter->format('foo\\<bar'));
$this->assertEquals('foo << bar', $formatter->format('foo << bar'));
$this->assertEquals('foo << bar \\', $formatter->format('foo << bar \\'));
$this->assertEquals("foo << \033[32mbar \\ baz\033[39m \\", $formatter->format('foo << <info>bar \\ baz</info> \\'));
$this->assertEquals('<info>some info</info>', $formatter->format('\\<info>some info\\</info>'));
$this->assertEquals('\\<info>some info\\</info>', OutputFormatter::escape('<info>some info</info>'));