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:
commit
3961412310
@ -31,7 +31,15 @@ class OutputFormatter implements OutputFormatterInterface
|
|||||||
*/
|
*/
|
||||||
public static function escape($text)
|
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;
|
$message = (string) $message;
|
||||||
$offset = 0;
|
$offset = 0;
|
||||||
$output = '';
|
$output = '';
|
||||||
$tagRegex = '[a-z][a-z0-9_=;-]*';
|
$tagRegex = '[a-z][a-z0-9_=;-]*+';
|
||||||
preg_match_all("#<(($tagRegex) | /($tagRegex)?)>#ix", $message, $matches, PREG_OFFSET_CAPTURE);
|
preg_match_all("#<(($tagRegex) | /($tagRegex)?)>#ix", $message, $matches, PREG_OFFSET_CAPTURE);
|
||||||
foreach ($matches[0] as $i => $match) {
|
foreach ($matches[0] as $i => $match) {
|
||||||
$pos = $match[1];
|
$pos = $match[1];
|
||||||
@ -164,6 +172,10 @@ class OutputFormatter implements OutputFormatterInterface
|
|||||||
|
|
||||||
$output .= $this->applyCurrentStyle(substr($message, $offset));
|
$output .= $this->applyCurrentStyle(substr($message, $offset));
|
||||||
|
|
||||||
|
if (false !== strpos($output, '<<')) {
|
||||||
|
return strtr($output, array('\\<' => '<', '<<' => '\\'));
|
||||||
|
}
|
||||||
|
|
||||||
return str_replace('\\<', '<', $output);
|
return str_replace('\\<', '<', $output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,8 +98,8 @@ class OutputFormatterTest extends \PHPUnit_Framework_TestCase
|
|||||||
$formatter = new OutputFormatter(true);
|
$formatter = new OutputFormatter(true);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
"(\033[32mz>=2.0,<a2.3\033[0m)",
|
"(\033[32mz>=2.0,<<<a2.3\\\033[0m)",
|
||||||
$formatter->format('(<info>'.$formatter->escape('z>=2.0,<a2.3').'</info>)')
|
$formatter->format('(<info>'.$formatter->escape('z>=2.0,<\\<<a2.3\\').'</info>)')
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
|
Reference in New Issue
Block a user