From 8cf82b7a111094325ca1a599a8bbd711cd687c4c Mon Sep 17 00:00:00 2001 From: jfsimon Date: Tue, 10 Jul 2012 11:12:54 +0200 Subject: [PATCH 1/4] [Console] Added '<' escaping tests. --- .../Console/Tests/Formatter/OutputFormatterTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Symfony/Component/Console/Tests/Formatter/OutputFormatterTest.php b/src/Symfony/Component/Console/Tests/Formatter/OutputFormatterTest.php index 0d1bf4104a..e53f5f5850 100644 --- a/src/Symfony/Component/Console/Tests/Formatter/OutputFormatterTest.php +++ b/src/Symfony/Component/Console/Tests/Formatter/OutputFormatterTest.php @@ -22,6 +22,14 @@ class FormatterStyleTest extends \PHPUnit_Framework_TestCase $this->assertEquals("foo<>bar", $formatter->format('foo<>bar')); } + public function testLGCharEscaping() + { + $formatter = new OutputFormatter(true); + $this->assertEquals("fooformat('foo\\assertEquals("some info", $formatter->format('\\some info\\')); + $this->assertEquals("\\some info\\", OutputFormatter::escape('some info')); + } + public function testBundledStyles() { $formatter = new OutputFormatter(true); From aaf4950d4ffc6db1e9e8ce16f1a7559a94993b00 Mon Sep 17 00:00:00 2001 From: jfsimon Date: Tue, 10 Jul 2012 11:13:16 +0200 Subject: [PATCH 2/4] [Console] Implemented '<' escaping. --- .../Console/Formatter/OutputFormatter.php | 46 ++++++++++++++----- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/Console/Formatter/OutputFormatter.php b/src/Symfony/Component/Console/Formatter/OutputFormatter.php index 5cfadfc351..2f321ae5c3 100644 --- a/src/Symfony/Component/Console/Formatter/OutputFormatter.php +++ b/src/Symfony/Component/Console/Formatter/OutputFormatter.php @@ -23,12 +23,29 @@ class OutputFormatter implements OutputFormatterInterface /** * The pattern to phrase the format. */ - const FORMAT_PATTERN = '#<(/?)([a-z][a-z0-9_=;-]+)?>([^<]*)#is'; + const FORMAT_PATTERN = '#(\\\\?)<(/?)([a-z][a-z0-9_=;-]+)?>([^\\\\<]*)#is'; + + /** + * The escape sequence for LG char. + */ + const LG_CHAR_ESCAPING = '\\'; private $decorated; private $styles = array(); private $styleStack; + /** + * Escapes "<" special char in given text. + * + * @param string $text Text to escape + * + * @return string Escaped text + */ + public static function escape($text) + { + return preg_replace('/([^\\\\]?)" tag $this->styleStack->pop(); - return $this->applyStyle($this->styleStack->getCurrent(), $match[3]); + return $this->applyStyle($this->styleStack->getCurrent(), $match[4]); } // we got "<>" tag - return '<>'.$match[3]; + return '<>'.$match[4]; } - if (isset($this->styles[strtolower($match[2])])) { - $style = $this->styles[strtolower($match[2])]; + if (isset($this->styles[strtolower($match[3])])) { + $style = $this->styles[strtolower($match[3])]; } else { - $style = $this->createStyleFromString($match[2]); + $style = $this->createStyleFromString($match[3]); if (false === $style) { return $match[0]; } } - if ('/' === $match[1]) { + if ('/' === $match[2]) { $this->styleStack->pop($style); } else { $this->styleStack->push($style); } - return $this->applyStyle($this->styleStack->getCurrent(), $match[3]); + return $this->applyStyle($this->styleStack->getCurrent(), $match[4]); } /** From 14bd5ba37c1690b6c7fc54ca958c64d214a6a0f0 Mon Sep 17 00:00:00 2001 From: jfsimon Date: Tue, 10 Jul 2012 12:00:17 +0200 Subject: [PATCH 3/4] [Console] 'formatBlock' helper now escape messages. --- .../Component/Console/Helper/FormatterHelper.php | 3 +++ .../Console/Tests/Helper/FormatterHelperTest.php | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/Symfony/Component/Console/Helper/FormatterHelper.php b/src/Symfony/Component/Console/Helper/FormatterHelper.php index dd9615bc62..34ae394578 100644 --- a/src/Symfony/Component/Console/Helper/FormatterHelper.php +++ b/src/Symfony/Component/Console/Helper/FormatterHelper.php @@ -11,6 +11,8 @@ namespace Symfony\Component\Console\Helper; +use Symfony\Component\Console\Formatter\OutputFormatter; + /** * The Formatter class provides helpers to format messages. * @@ -48,6 +50,7 @@ class FormatterHelper extends Helper $len = 0; $lines = array(); foreach ($messages as $message) { + $message = OutputFormatter::escape($message); $lines[] = sprintf($large ? ' %s ' : ' %s ', $message); $len = max($this->strlen($message) + ($large ? 4 : 2), $len); } diff --git a/src/Symfony/Component/Console/Tests/Helper/FormatterHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/FormatterHelperTest.php index 430c077e82..5ab62a3933 100644 --- a/src/Symfony/Component/Console/Tests/Helper/FormatterHelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/FormatterHelperTest.php @@ -68,4 +68,17 @@ class FormatterHelperTest extends \PHPUnit_Framework_TestCase '::formatBlock() formats a message in a block' ); } + + public function testFormatBlockLGEscaping() + { + $formatter = new FormatterHelper(); + + $this->assertEquals( + ' ' . "\n" . + ' \some info\ ' . "\n" . + ' ', + $formatter->formatBlock('some info', 'error', true), + '::formatBlock() escapes \'<\' chars' + ); + } } From 50cf9287be5d3dc600164f3b99e5cb17d5919d04 Mon Sep 17 00:00:00 2001 From: jfsimon Date: Tue, 10 Jul 2012 14:40:39 +0200 Subject: [PATCH 4/4] [Console] Removed pointless constant. --- .../Component/Console/Formatter/OutputFormatter.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Component/Console/Formatter/OutputFormatter.php b/src/Symfony/Component/Console/Formatter/OutputFormatter.php index 2f321ae5c3..f7ead0dfc7 100644 --- a/src/Symfony/Component/Console/Formatter/OutputFormatter.php +++ b/src/Symfony/Component/Console/Formatter/OutputFormatter.php @@ -25,11 +25,6 @@ class OutputFormatter implements OutputFormatterInterface */ const FORMAT_PATTERN = '#(\\\\?)<(/?)([a-z][a-z0-9_=;-]+)?>([^\\\\<]*)#is'; - /** - * The escape sequence for LG char. - */ - const LG_CHAR_ESCAPING = '\\'; - private $decorated; private $styles = array(); private $styleStack; @@ -154,7 +149,7 @@ class OutputFormatter implements OutputFormatterInterface { $message = preg_replace_callback(self::FORMAT_PATTERN, array($this, 'replaceStyle'), $message); - return str_replace(self::LG_CHAR_ESCAPING.'<', '<', $message); + return str_replace('\\<', '<', $message); } /** @@ -167,7 +162,7 @@ class OutputFormatter implements OutputFormatterInterface private function replaceStyle($match) { // we got "\<" escaped char - if (self::LG_CHAR_ESCAPING === $match[1]) { + if ('\\' === $match[1]) { return $match[0]; }