Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6782c78

Browse filesBrowse files
committed
merged branch jfsimon/issue-4752 (PR #4832)
Commits ------- 50cf928 [Console] Removed pointless constant. 14bd5ba [Console] 'formatBlock' helper now escape messages. aaf4950 [Console] Implemented '<' escaping. 8cf82b7 [Console] Added '<' escaping tests. Discussion ---------- Issue 4752 Bug fix: no Feature addition: no Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: #4752 This PR adds possibility to escape `<` chars with `\` to avoid formatting mess. In addition, `FormatterHelper::formatBlock()` method auto-escapes messages.
2 parents 884fffa + 50cf928 commit 6782c78
Copy full SHA for 6782c78

File tree

Expand file treeCollapse file tree

4 files changed

+54
-11
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+54
-11
lines changed

‎src/Symfony/Component/Console/Formatter/OutputFormatter.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Formatter/OutputFormatter.php
+30-11Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,24 @@ class OutputFormatter implements OutputFormatterInterface
2323
/**
2424
* The pattern to phrase the format.
2525
*/
26-
const FORMAT_PATTERN = '#<(/?)([a-z][a-z0-9_=;-]+)?>([^<]*)#is';
26+
const FORMAT_PATTERN = '#(\\\\?)<(/?)([a-z][a-z0-9_=;-]+)?>([^\\\\<]*)#is';
2727

2828
private $decorated;
2929
private $styles = array();
3030
private $styleStack;
3131

32+
/**
33+
* Escapes "<" special char in given text.
34+
*
35+
* @param string $text Text to escape
36+
*
37+
* @return string Escaped text
38+
*/
39+
public static function escape($text)
40+
{
41+
return preg_replace('/([^\\\\]?)</is', '$1\\<', $text);
42+
}
43+
3244
/**
3345
* Initializes console output formatter.
3446
*
@@ -135,7 +147,9 @@ public function getStyle($name)
135147
*/
136148
public function format($message)
137149
{
138-
return preg_replace_callback(self::FORMAT_PATTERN, array($this, 'replaceStyle'), $message);
150+
$message = preg_replace_callback(self::FORMAT_PATTERN, array($this, 'replaceStyle'), $message);
151+
152+
return str_replace('\\<', '<', $message);
139153
}
140154

141155
/**
@@ -155,35 +169,40 @@ public function getStyleStack()
155169
*/
156170
private function replaceStyle($match)
157171
{
158-
if ('' === $match[2]) {
159-
if ('/' === $match[1]) {
172+
// we got "\<" escaped char
173+
if ('\\' === $match[1]) {
174+
return $match[0];
175+
}
176+
177+
if ('' === $match[3]) {
178+
if ('/' === $match[2]) {
160179
// we got "</>" tag
161180
$this->styleStack->pop();
162181

163-
return $this->applyStyle($this->styleStack->getCurrent(), $match[3]);
182+
return $this->applyStyle($this->styleStack->getCurrent(), $match[4]);
164183
}
165184

166185
// we got "<>" tag
167-
return '<>'.$match[3];
186+
return '<>'.$match[4];
168187
}
169188

170-
if (isset($this->styles[strtolower($match[2])])) {
171-
$style = $this->styles[strtolower($match[2])];
189+
if (isset($this->styles[strtolower($match[3])])) {
190+
$style = $this->styles[strtolower($match[3])];
172191
} else {
173-
$style = $this->createStyleFromString($match[2]);
192+
$style = $this->createStyleFromString($match[3]);
174193

175194
if (false === $style) {
176195
return $match[0];
177196
}
178197
}
179198

180-
if ('/' === $match[1]) {
199+
if ('/' === $match[2]) {
181200
$this->styleStack->pop($style);
182201
} else {
183202
$this->styleStack->push($style);
184203
}
185204

186-
return $this->applyStyle($this->styleStack->getCurrent(), $match[3]);
205+
return $this->applyStyle($this->styleStack->getCurrent(), $match[4]);
187206
}
188207

189208
/**

‎src/Symfony/Component/Console/Helper/FormatterHelper.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Helper/FormatterHelper.php
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Console\Helper;
1313

14+
use Symfony\Component\Console\Formatter\OutputFormatter;
15+
1416
/**
1517
* The Formatter class provides helpers to format messages.
1618
*
@@ -48,6 +50,7 @@ public function formatBlock($messages, $style, $large = false)
4850
$len = 0;
4951
$lines = array();
5052
foreach ($messages as $message) {
53+
$message = OutputFormatter::escape($message);
5154
$lines[] = sprintf($large ? ' %s ' : ' %s ', $message);
5255
$len = max($this->strlen($message) + ($large ? 4 : 2), $len);
5356
}

‎src/Symfony/Component/Console/Tests/Formatter/OutputFormatterTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Formatter/OutputFormatterTest.php
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ public function testEmptyTag()
2222
$this->assertEquals("foo<>bar", $formatter->format('foo<>bar'));
2323
}
2424

25+
public function testLGCharEscaping()
26+
{
27+
$formatter = new OutputFormatter(true);
28+
$this->assertEquals("foo<bar", $formatter->format('foo\\<bar'));
29+
$this->assertEquals("<info>some info</info>", $formatter->format('\\<info>some info\\</info>'));
30+
$this->assertEquals("\\<info>some info\\</info>", OutputFormatter::escape('<info>some info</info>'));
31+
}
32+
2533
public function testBundledStyles()
2634
{
2735
$formatter = new OutputFormatter(true);

‎src/Symfony/Component/Console/Tests/Helper/FormatterHelperTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Helper/FormatterHelperTest.php
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,17 @@ public function testFormatBlockWithDiacriticLetters()
6868
'::formatBlock() formats a message in a block'
6969
);
7070
}
71+
72+
public function testFormatBlockLGEscaping()
73+
{
74+
$formatter = new FormatterHelper();
75+
76+
$this->assertEquals(
77+
'<error> </error>' . "\n" .
78+
'<error> \<info>some info\</info> </error>' . "\n" .
79+
'<error> </error>',
80+
$formatter->formatBlock('<info>some info</info>', 'error', true),
81+
'::formatBlock() escapes \'<\' chars'
82+
);
83+
}
7184
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.