From f35a540b1fe7d8d0748e8691b2d10cfb413ece30 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 14 Sep 2015 10:00:28 +0200 Subject: [PATCH] Prevent potential doc comments in escaped inlined source --- lib/Twig/Environment.php | 2 +- lib/Twig/Template.php | 2 +- test/Twig/Tests/EnvironmentTest.php | 8 +++++--- test/Twig/Tests/TemplateTest.php | 4 ++-- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 167d6c505..d31f9580e 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -647,7 +647,7 @@ class Twig_Environment $compiled = $this->compile($this->parse($this->tokenize($source, $name)), $source); if (isset($source[0])) { - $compiled .= '/* '.str_replace(array('*/', "\r\n", "\n", "\r"), array('*//*', "\n", "*/\n/* ", "*/\n/* "), $source)."*/\n"; + $compiled .= '/* '.str_replace(array('*/', "\r\n", "\r", "\n"), array('*//* ', "\n", "\n", "*/\n/* "), $source)."*/\n"; } return $compiled; diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index a854138b6..36d41621a 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -323,7 +323,7 @@ abstract class Twig_Template implements Twig_TemplateInterface $i = 0; while (isset($source[$i]) && '/* */' === substr_replace($source[$i], '', 3, -2)) { - $source[$i] = str_replace('*//*', '*/', substr($source[$i], 3, -2)); + $source[$i] = str_replace('*//* ', '*/', substr($source[$i], 3, -2)); ++$i; } array_splice($source, $i); diff --git a/test/Twig/Tests/EnvironmentTest.php b/test/Twig/Tests/EnvironmentTest.php index 7d78179aa..83a98ef52 100644 --- a/test/Twig/Tests/EnvironmentTest.php +++ b/test/Twig/Tests/EnvironmentTest.php @@ -142,10 +142,12 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase { $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface')); - $source = "\r\nbar\n"; - $expected = "/* */\n/* bar*/\n/* */\n"; + $source = "\r\nbar\n"; + $expected = "/* */\n/* bar*/\n/* */\n"; + $compiled = $twig->compileSource($source, 'index'); - $this->assertContains($expected, $twig->compileSource($source, 'index')); + $this->assertContains($expected, $compiled); + $this->assertNotContains('/**', $compiled); } public function testExtensionsAreNotInitializedWhenRenderingACompiledTemplate() diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index eb27dc380..37836fdc8 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -88,7 +88,7 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase { $template = new Twig_TemplateTest(new Twig_Environment($this->getMock('Twig_LoaderInterface')), false); - $this->assertSame("\n", $template->getSource()); + $this->assertSame("\n", $template->getSource()); } /** @@ -470,7 +470,7 @@ class Twig_TemplateTest extends Twig_Template } } } -/* */ +/* */ /* */ class Twig_TemplateArrayAccessObject implements ArrayAccess