[Console] Fix escaping of trailing backslashes

This commit is contained in:
Nicolas Grekas 2016-02-24 15:09:34 +01:00
parent a7fee1244e
commit 44ae785064
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(