From 03f767ebb8a975667dbbb8a79b9c9649dfbb05a6 Mon Sep 17 00:00:00 2001 From: Duilio Palacios Date: Fri, 14 Jul 2017 18:44:12 +0100 Subject: [PATCH] Prevent raw blocks from being restore in the wrong order --- .../View/Compilers/BladeCompiler.php | 38 +++++++------------ tests/View/Blade/BladeCommentsTest.php | 8 ++++ 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/Illuminate/View/Compilers/BladeCompiler.php b/src/Illuminate/View/Compilers/BladeCompiler.php index a64608889ebd..6a2b365bc05b 100644 --- a/src/Illuminate/View/Compilers/BladeCompiler.php +++ b/src/Illuminate/View/Compilers/BladeCompiler.php @@ -100,7 +100,7 @@ class BladeCompiler extends Compiler implements CompilerInterface * * @var string */ - protected $rawPlaceholder = '@__raw-block__@'; + protected $rawPlaceholder = '@__raw_block_#__@'; /** * Array to temporary store the raw blocks found in the template. @@ -158,13 +158,13 @@ public function setPath($path) public function compileString($value) { if (strpos($value, '@verbatim') !== false) { - $value = $this->storeVerbatimBlocks($value); + $value = $this->storeRawBlocks($value, 'verbatim'); } $this->footer = []; if (strpos($value, '@php') !== false) { - $value = $this->storePhpBlocks($value); + $value = $this->storeRawBlocks($value, 'php'); } $result = ''; @@ -191,32 +191,20 @@ public function compileString($value) } /** - * Store the verbatim blocks and replace them with a temporary placeholder. + * Store the verbatim and PHP blocks and replace them with a temporary placeholder. * * @param string $value + * @param string $tag * @return string */ - protected function storeVerbatimBlocks($value) + protected function storeRawBlocks($value, $tag) { - return preg_replace_callback('/(?rawBlocks[] = $matches[1]; + return preg_replace_callback("/(?rawBlocks[] = $tag == 'php' ? "" : $matches[1]; - return $this->rawPlaceholder; - }, $value); - } - - /** - * Store the PHP blocks and replace them with a temporary placeholder. - * - * @param string $value - * @return string - */ - protected function storePhpBlocks($value) - { - return preg_replace_callback('/(?rawBlocks[] = ""; + end($this->rawBlocks); - return $this->rawPlaceholder; + return str_replace('#', key($this->rawBlocks), $this->rawPlaceholder); }, $value); } @@ -228,8 +216,10 @@ protected function storePhpBlocks($value) */ protected function restoreRawContent($result) { - $result = preg_replace_callback('/'.preg_quote($this->rawPlaceholder).'/', function () { - return array_shift($this->rawBlocks); + $placeholderRegex = str_replace('#', '(\d+)', preg_quote($this->rawPlaceholder)); + + $result = preg_replace_callback("/{$placeholderRegex}/", function ($matches) { + return $this->rawBlocks[$matches[1]]; }, $result); $this->rawBlocks = []; diff --git a/tests/View/Blade/BladeCommentsTest.php b/tests/View/Blade/BladeCommentsTest.php index c5613a2c251d..bcc5df565b53 100644 --- a/tests/View/Blade/BladeCommentsTest.php +++ b/tests/View/Blade/BladeCommentsTest.php @@ -24,4 +24,12 @@ public function testBladeCodeInsideCommentsIsNotCompiled() $this->assertEmpty($this->compiler->compileString($string)); } + + public function testRawBlocksDontGetMixedUpWhenSomeAreRemovedByBladeComments() + { + $string = '{{-- @verbatim Block #1 @endverbatim --}} @php "Block #2" @endphp'; + $expected = ' '; + + $this->assertSame($expected, $this->compiler->compileString($string)); + } }