Skip to content

Commit 020e15e

Browse files
[Console] Fix escaping of trailing backslashes
1 parent a806a18 commit 020e15e

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

Formatter/OutputFormatter.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@ class OutputFormatter implements OutputFormatterInterface
3131
*/
3232
public static function escape($text)
3333
{
34-
return preg_replace('/([^\\\\]?)</', '$1\\<', $text);
34+
$text = preg_replace('/([^\\\\]?)</', '$1\\<', $text);
35+
36+
if ('\\' === substr($text, -1)) {
37+
$len = strlen($text);
38+
$text = rtrim($text, '\\');
39+
$text .= str_repeat('<<', $len - strlen($text));
40+
}
41+
42+
return $text;
3543
}
3644

3745
/**
@@ -129,7 +137,7 @@ public function format($message)
129137
$message = (string) $message;
130138
$offset = 0;
131139
$output = '';
132-
$tagRegex = '[a-z][a-z0-9_=;-]*';
140+
$tagRegex = '[a-z][a-z0-9_=;-]*+';
133141
preg_match_all("#<(($tagRegex) | /($tagRegex)?)>#ix", $message, $matches, PREG_OFFSET_CAPTURE);
134142
foreach ($matches[0] as $i => $match) {
135143
$pos = $match[1];
@@ -164,6 +172,10 @@ public function format($message)
164172

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

175+
if (false !== strpos($output, '<<')) {
176+
return strtr($output, array('\\<' => '<', '<<' => '\\'));
177+
}
178+
167179
return str_replace('\\<', '<', $output);
168180
}
169181

Tests/Formatter/OutputFormatterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ public function testStyleEscaping()
9898
$formatter = new OutputFormatter(true);
9999

100100
$this->assertEquals(
101-
"(\033[32mz>=2.0,<a2.3\033[0m)",
102-
$formatter->format('(<info>'.$formatter->escape('z>=2.0,<a2.3').'</info>)')
101+
"(\033[32mz>=2.0,<<<a2.3\\\033[0m)",
102+
$formatter->format('(<info>'.$formatter->escape('z>=2.0,<\\<<a2.3\\').'</info>)')
103103
);
104104

105105
$this->assertEquals(

0 commit comments

Comments
 (0)