bug #17914 [Console] Fix escaping of trailing backslashes (nicolas-grekas)

This PR was merged into the 2.3 branch.

Discussion
----------

[Console] Fix escaping of trailing backslashes

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

Commits
-------

44ae785 [Console] Fix escaping of trailing backslashes
This commit is contained in:
Fabien Potencier 2016-02-26 05:56:43 +01:00
commit 3961412310
2 changed files with 16 additions and 4 deletions

View File

@ -31,7 +31,15 @@ class OutputFormatter implements OutputFormatterInterface
*/
public static function escape($text)
{
return preg_replace('/([^\\\\]?)</', '$1\\<', $text);
$text = preg_replace('/([^\\\\]?)</', '$1\\<', $text);
if ('\\' === substr($text, -1)) {
$len = strlen($text);
$text = rtrim($text, '\\');
$text .= str_repeat('<<', $len - strlen($text));
}
return $text;
}
/**
@ -129,7 +137,7 @@ class OutputFormatter implements OutputFormatterInterface
$message = (string) $message;
$offset = 0;
$output = '';
$tagRegex = '[a-z][a-z0-9_=;-]*';
$tagRegex = '[a-z][a-z0-9_=;-]*+';
preg_match_all("#<(($tagRegex) | /($tagRegex)?)>#ix", $message, $matches, PREG_OFFSET_CAPTURE);
foreach ($matches[0] as $i => $match) {
$pos = $match[1];
@ -164,6 +172,10 @@ class OutputFormatter implements OutputFormatterInterface
$output .= $this->applyCurrentStyle(substr($message, $offset));
if (false !== strpos($output, '<<')) {
return strtr($output, array('\\<' => '<', '<<' => '\\'));
}
return str_replace('\\<', '<', $output);
}

View File

@ -98,8 +98,8 @@ class OutputFormatterTest extends \PHPUnit_Framework_TestCase
$formatter = new OutputFormatter(true);
$this->assertEquals(
"(\033[32mz>=2.0,<a2.3\033[0m)",
$formatter->format('(<info>'.$formatter->escape('z>=2.0,<a2.3').'</info>)')
"(\033[32mz>=2.0,<<<a2.3\\\033[0m)",
$formatter->format('(<info>'.$formatter->escape('z>=2.0,<\\<<a2.3\\').'</info>)')
);
$this->assertEquals(