Skip to content

Commit

Permalink
bug #17914 [Console] Fix escaping of trailing backslashes (nicolas-gr…
Browse files Browse the repository at this point in the history
…ekas)

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
  • Loading branch information
fabpot committed Feb 26, 2016
2 parents a7fee12 + 44ae785 commit 3961412
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
16 changes: 14 additions & 2 deletions src/Symfony/Component/Console/Formatter/OutputFormatter.php
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -129,7 +137,7 @@ public function format($message)
$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];
Expand Down Expand Up @@ -164,6 +172,10 @@ public function format($message)

$output .= $this->applyCurrentStyle(substr($message, $offset));

if (false !== strpos($output, '<<')) {
return strtr($output, array('\\<' => '<', '<<' => '\\'));
}

return str_replace('\\<', '<', $output);
}

Expand Down
Expand Up @@ -98,8 +98,8 @@ public function testStyleEscaping()
$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(
Expand Down

0 comments on commit 3961412

Please sign in to comment.