Skip to content

Commit

Permalink
Prevent raw blocks from being restore in the wrong order
Browse files Browse the repository at this point in the history
  • Loading branch information
sileence committed Jul 14, 2017
1 parent 6c9093f commit 03f767e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
38 changes: 14 additions & 24 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 = '';
Expand All @@ -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('/(?<!@)@verbatim(.*?)@endverbatim/s', function ($matches) {
$this->rawBlocks[] = $matches[1];
return preg_replace_callback("/(?<!@)@{$tag}(.*?)@end{$tag}/s", function ($matches) use ($tag) {
$this->rawBlocks[] = $tag == 'php' ? "<?php{$matches[1]}?>" : $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('/(?<!@)@php(.*?)@endphp/s', function ($matches) {
$this->rawBlocks[] = "<?php{$matches[1]}?>";
end($this->rawBlocks);

return $this->rawPlaceholder;
return str_replace('#', key($this->rawBlocks), $this->rawPlaceholder);
}, $value);
}

Expand All @@ -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 = [];
Expand Down
8 changes: 8 additions & 0 deletions tests/View/Blade/BladeCommentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ' <?php "Block #2" ?>';

$this->assertSame($expected, $this->compiler->compileString($string));
}
}

0 comments on commit 03f767e

Please sign in to comment.