Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Prevent raw blocks from being restore in the wrong order #20072

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}