8000 bug #17914 [Console] Fix escaping of trailing backslashes (nicolas-gr… · symfony/symfony@3961412 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3961412

Browse files
committed
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
2 parents a7fee12 + 44ae785 commit 3961412

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/Symfony/Component/Console/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

src/Symfony/Component/Console/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)
0