From 63032c7c89c9048775718152e36a3058057408a4 Mon Sep 17 00:00:00 2001 From: Sebastiaan Stok Date: Fri, 28 Feb 2014 13:42:15 +0100 Subject: [PATCH] fixed Kernel::stripComments() normalizing new-lines This makes normalizing new-lines less error-prone when a string contains multiple new line-lines --- src/Symfony/Component/HttpKernel/Kernel.php | 24 +++++++++++++++---- .../Component/HttpKernel/Tests/KernelTest.php | 15 ++++++++---- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index 29e43128b9..c7bc8576c0 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -759,23 +759,39 @@ abstract class Kernel implements KernelInterface, TerminableInterface $rawChunk = ''; $output = ''; $tokens = token_get_all($source); + $ignoreSpace = false; for (reset($tokens); false !== $token = current($tokens); next($tokens)) { if (is_string($token)) { $rawChunk .= $token; } elseif (T_START_HEREDOC === $token[0]) { - $output .= preg_replace(array('/\s+$/Sm', '/\n+/S'), "\n", $rawChunk).$token[1]; + $output .= $rawChunk.$token[1]; do { $token = next($tokens); $output .= $token[1]; } while ($token[0] !== T_END_HEREDOC); $rawChunk = ''; - } elseif (!in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) { + } elseif (T_WHITESPACE === $token[0]) { + if ($ignoreSpace) { + $ignoreSpace = false; + + continue; + } + + // replace multiple new lines with a single newline + $rawChunk .= preg_replace(array('/\n{2,}/S'), "\n", $token[1]); + } elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) { + $ignoreSpace = true; + } else { $rawChunk .= $token[1]; + + // The PHP-open tag already has a new-line + if (T_OPEN_TAG === $token[0]) { + $ignoreSpace = true; + } } } - // replace multiple new lines with a single newline - $output .= preg_replace(array('/\s+$/Sm', '/\n+/S'), "\n", $rawChunk); + $output .= $rawChunk; return $output; } diff --git a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php index 05d0c7507e..a19bc32679 100644 --- a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php @@ -274,6 +274,10 @@ class KernelTest extends \PHPUnit_Framework_TestCase $string = 'string should not be modified'; +$string = 'string should not be + +modified'; + $heredoc = <<