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

Persist indenting after newlines in replaced content blade templates #28768

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
16 changes: 8 additions & 8 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ protected function compileExtensions($value)
protected function compileStatements($value)
{
return preg_replace_callback(
'/\B@(@?\w+(?:::\w+)?)([ \t]*)(\( ( (?>[^()]+) | (?3) )* \))?/x', function ($match) {
'/\B([\h]*)@(@?\w+(?:::\w+)?)([ \t]*)(\( ( (?>[^()]+) | (?4) )* \))?/x', function ($match) {
return $this->compileStatement($match);
}, $value
);
Expand All @@ -355,15 +355,15 @@ protected function compileStatements($value)
*/
protected function compileStatement($match)
{
if (Str::contains($match[1], '@')) {
$match[0] = isset($match[3]) ? $match[1].$match[3] : $match[1];
} elseif (isset($this->customDirectives[$match[1]])) {
$match[0] = $this->callCustomDirective($match[1], Arr::get($match, 3));
} elseif (method_exists($this, $method = 'compile'.ucfirst($match[1]))) {
$match[0] = $this->$method(Arr::get($match, 3));
if (Str::contains($match[2], '@')) {
$match[0] = isset($match[4]) ? $match[1].$match[2].$match[4] : $match[1].$match[2];
} elseif (isset($this->customDirectives[$match[2]])) {
$match[0] = $match[1].$this->callCustomDirective($match[2], Arr::get($match, 4));
} elseif (method_exists($this, $method = 'compile'.ucfirst($match[2]))) {
$match[0] = $match[1].$this->$method(Arr::get($match, 4), $match[1]);
}

return isset($match[3]) ? $match[0] : $match[0].$match[2];
return isset($match[4]) ? $match[0] : $match[0].$match[3];
}

/**
Expand Down
28 changes: 16 additions & 12 deletions src/Illuminate/View/Compilers/Concerns/CompilesIncludes.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,52 +18,56 @@ protected function compileEach($expression)
/**
* Compile the include statements into valid PHP.
*
* @param string $expression
* @param string $expression
* @param string $indenting
* @return string
*/
protected function compileInclude($expression)
protected function compileInclude($expression, $indenting = '')
{
$expression = $this->stripParentheses($expression);

return "<?php echo \$__env->make({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>";
return "<?php echo \$__env->make({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']) + ['indenting' => '" . $indenting . "'])->render(); ?>";
}

/**
* Compile the include-if statements into valid PHP.
*
* @param string $expression
* @param string $expression
* @param string $indenting
* @return string
*/
protected function compileIncludeIf($expression)
protected function compileIncludeIf($expression, $indenting = '')
{
$expression = $this->stripParentheses($expression);

return "<?php if (\$__env->exists({$expression})) echo \$__env->make({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>";
return "<?php if (\$__env->exists({$expression})) echo \$__env->make({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']) + ['indenting' => '" . $indenting . "'])->render(); ?>";
}

/**
* Compile the include-when statements into valid PHP.
*
* @param string $expression
* @param string $expression
* @param string $indenting
* @return string
*/
protected function compileIncludeWhen($expression)
protected function compileIncludeWhen($expression, $indenting = '')
{
$expression = $this->stripParentheses($expression);

return "<?php echo \$__env->renderWhen($expression, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path'])); ?>";
return "<?php echo \$__env->renderWhen($expression, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']) + ['indenting' => '" . $indenting . "']); ?>";
}

/**
* Compile the include-first statements into valid PHP.
*
* @param string $expression
* @param string $expression
* @param string $indenting
* @return string
*/
protected function compileIncludeFirst($expression)
protected function compileIncludeFirst($expression, $indenting = '')
{
$expression = $this->stripParentheses($expression);

return "<?php echo \$__env->first({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>";
return "<?php echo \$__env->first({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']) + ['indenting' => '" . $indenting . "'])->render(); ?>";
}
}
12 changes: 11 additions & 1 deletion src/Illuminate/View/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,17 @@ protected function renderContents()
*/
protected function getContents()
{
return $this->engine->get($this->path, $this->gatherData());
$content = $this->engine->get($this->path, $this->gatherData());

if(isset($this->data['indenting'])){
$content = preg_replace('/(\r\n|\r|\n)/', '$1'.$this->data['indenting'], $content);
}

if (!in_array(substr($content,-1), ["\n", "\r", "\r\n"])){
$content .= PHP_EOL;
}

return $content;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/View/Blade/BladeIncludeFirstTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class BladeIncludeFirstTest extends AbstractBladeTestCase
{
public function testIncludeFirstsAreCompiled()
{
$this->assertEquals('<?php echo $__env->first(["one", "two"], \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']))->render(); ?>', $this->compiler->compileString('@includeFirst(["one", "two"])'));
$this->assertEquals('<?php echo $__env->first(["one", "two"], ["foo" => "bar"], \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']))->render(); ?>', $this->compiler->compileString('@includeFirst(["one", "two"], ["foo" => "bar"])'));
$this->assertEquals('<?php echo $__env->first(["one", "two"], \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']) + [\'indenting\' => \'\'])->render(); ?>', $this->compiler->compileString('@includeFirst(["one", "two"])'));
$this->assertEquals('<?php echo $__env->first(["one", "two"], ["foo" => "bar"], \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']) + [\'indenting\' => \'\'])->render(); ?>', $this->compiler->compileString('@includeFirst(["one", "two"], ["foo" => "bar"])'));
}
}
4 changes: 2 additions & 2 deletions tests/View/Blade/BladeIncludeIfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class BladeIncludeIfTest extends AbstractBladeTestCase
{
public function testIncludeIfsAreCompiled()
{
$this->assertEquals('<?php if ($__env->exists(\'foo\')) echo $__env->make(\'foo\', \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']))->render(); ?>', $this->compiler->compileString('@includeIf(\'foo\')'));
$this->assertEquals('<?php if ($__env->exists(name(foo))) echo $__env->make(name(foo), \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']))->render(); ?>', $this->compiler->compileString('@includeIf(name(foo))'));
$this->assertEquals('<?php if ($__env->exists(\'foo\')) echo $__env->make(\'foo\', \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']) + [\'indenting\' => \'\'])->render(); ?>', $this->compiler->compileString('@includeIf(\'foo\')'));
$this->assertEquals('<?php if ($__env->exists(name(foo))) echo $__env->make(name(foo), \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']) + [\'indenting\' => \'\'])->render(); ?>', $this->compiler->compileString('@includeIf(name(foo))'));
}
}
4 changes: 2 additions & 2 deletions tests/View/Blade/BladeIncludeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class BladeIncludeTest extends AbstractBladeTestCase
{
public function testIncludesAreCompiled()
{
$this->assertEquals('<?php echo $__env->make(\'foo\', \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']))->render(); ?>', $this->compiler->compileString('@include(\'foo\')'));
$this->assertEquals('<?php echo $__env->make(name(foo), \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']))->render(); ?>', $this->compiler->compileString('@include(name(foo))'));
$this->assertEquals('<?php echo $__env->make(\'foo\', \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']) + [\'indenting\' => \'\'])->render(); ?>', $this->compiler->compileString('@include(\'foo\')'));
$this->assertEquals('<?php echo $__env->make(name(foo), \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']) + [\'indenting\' => \'\'])->render(); ?>', $this->compiler->compileString('@include(name(foo))'));
}
}
4 changes: 2 additions & 2 deletions tests/View/Blade/BladeIncludeWhenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class BladeIncludeWhenTest extends AbstractBladeTestCase
{
public function testIncludeWhensAreCompiled()
{
$this->assertEquals('<?php echo $__env->renderWhen(true, \'foo\', ["foo" => "bar"], \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\'])); ?>', $this->compiler->compileString('@includeWhen(true, \'foo\', ["foo" => "bar"])'));
$this->assertEquals('<?php echo $__env->renderWhen(true, \'foo\', \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\'])); ?>', $this->compiler->compileString('@includeWhen(true, \'foo\')'));
$this->assertEquals('<?php echo $__env->renderWhen(true, \'foo\', ["foo" => "bar"], \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']) + [\'indenting\' => \'\']); ?>', $this->compiler->compileString('@includeWhen(true, \'foo\', ["foo" => "bar"])'));
$this->assertEquals('<?php echo $__env->renderWhen(true, \'foo\', \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']) + [\'indenting\' => \'\']); ?>', $this->compiler->compileString('@includeWhen(true, \'foo\')'));
}
}
2 changes: 1 addition & 1 deletion tests/View/Blade/BladeVerbatimTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function testMultilineTemplatesWithRawBlocksAreRenderedInTheRightOrder()
<?php echo e($third); ?>

<?php endif; ?>
<?php echo $__env->make("users", \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']))->render(); ?>
<?php echo $__env->make("users", \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']) + [\'indenting\' => \'\'])->render(); ?>

{{ $fourth }} @include("test")

Expand Down
2 changes: 1 addition & 1 deletion tests/View/ViewFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public function testComponentHandling()
$factory->endSlot();
echo 'component';
$contents = $factory->renderComponent();
$this->assertEquals('title<hr> component Taylor laravel.com', $contents);
$this->assertEquals('title<hr> component Taylor laravel.com'.PHP_EOL, $contents);
}

public function testTranslation()
Expand Down
8 changes: 4 additions & 4 deletions tests/View/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function testRenderHandlingCallbackReturnValues()
return '';
}));

$this->assertEquals('contents', $view->render(function () {
$this->assertEquals('contents'.PHP_EOL, $view->render(function () {
//
}));
}
Expand Down Expand Up @@ -99,8 +99,8 @@ public function testSectionsAreNotFlushedWhenNotDoneRendering()
$view->getFactory()->shouldReceive('decrementRender')->twice();
$view->getFactory()->shouldReceive('flushStateIfDoneRendering')->twice();

$this->assertEquals('contents', $view->render());
$this->assertEquals('contents', (string) $view);
$this->assertEquals('contents'.PHP_EOL, $view->render());
$this->assertEquals('contents'.PHP_EOL, (string) $view);
}

public function testViewNestBindsASubView()
Expand Down Expand Up @@ -192,7 +192,7 @@ public function testViewGatherDataWithRenderable()

$view->renderable = m::mock(Renderable::class);
$view->renderable->shouldReceive('render')->once()->andReturn('text');
$this->assertEquals('contents', $view->render());
$this->assertEquals('contents'.PHP_EOL, $view->render());
}

public function testViewRenderSections()
Expand Down