Skip to content

Commit

Permalink
[5.4] Don't re-escape a View instance passed as the default value to …
Browse files Browse the repository at this point in the history
…yield or section blade directives (#19884)

* don't re-escape a View instance passed as the default value to @yield

* don't re-escape a View instance passed as the default value to @section

* Update ManagesLayouts.php
  • Loading branch information
ockle authored and taylorotwell committed Jul 3, 2017
1 parent ecbacfa commit 405fd27
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Illuminate/View/Concerns/ManagesLayouts.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\View\Concerns;

use InvalidArgumentException;
use Illuminate\Contracts\View\View;

trait ManagesLayouts
{
Expand Down Expand Up @@ -41,7 +42,7 @@ public function startSection($section, $content = null)
$this->sectionStack[] = $section;
}
} else {
$this->extendSection($section, e($content));
$this->extendSection($section, $content instanceof View ? $content : e($content));
}
}

Expand Down Expand Up @@ -143,7 +144,7 @@ protected function extendSection($section, $content)
*/
public function yieldContent($section, $default = '')
{
$sectionContent = e($default);
$sectionContent = $default instanceof View ? $default : e($default);

if (isset($this->sections[$section])) {
$sectionContent = $this->sections[$section];
Expand Down
43 changes: 43 additions & 0 deletions tests/View/ViewFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,26 @@ public function testRenderCountHandling()
$this->assertTrue($factory->doneRendering());
}

public function testYieldDefault()
{
$factory = $this->getFactory();
$this->assertEquals('hi', $factory->yieldContent('foo', 'hi'));
}

public function testYieldDefaultIsEscaped()
{
$factory = $this->getFactory();
$this->assertEquals('&lt;p&gt;hi&lt;/p&gt;', $factory->yieldContent('foo', '<p>hi</p>'));
}

public function testYieldDefaultViewIsNotEscapedTwice()
{
$factory = $this->getFactory();
$view = m::mock('Illuminate\View\View');
$view->shouldReceive('__toString')->once()->andReturn('<p>hi</p>&lt;p&gt;already escaped&lt;/p&gt;');
$this->assertEquals('<p>hi</p>&lt;p&gt;already escaped&lt;/p&gt;', $factory->yieldContent('foo', $view));
}

public function testBasicSectionHandling()
{
$factory = $this->getFactory();
Expand All @@ -211,6 +231,29 @@ public function testBasicSectionHandling()
$this->assertEquals('hi', $factory->yieldContent('foo'));
}

public function testBasicSectionDefault()
{
$factory = $this->getFactory();
$factory->startSection('foo', 'hi');
$this->assertEquals('hi', $factory->yieldContent('foo'));
}

public function testBasicSectionDefaultIsEscaped()
{
$factory = $this->getFactory();
$factory->startSection('foo', '<p>hi</p>');
$this->assertEquals('&lt;p&gt;hi&lt;/p&gt;', $factory->yieldContent('foo'));
}

public function testBasicSectionDefaultViewIsNotEscapedTwice()
{
$factory = $this->getFactory();
$view = m::mock('Illuminate\View\View');
$view->shouldReceive('__toString')->once()->andReturn('<p>hi</p>&lt;p&gt;already escaped&lt;/p&gt;');
$factory->startSection('foo', $view);
$this->assertEquals('<p>hi</p>&lt;p&gt;already escaped&lt;/p&gt;', $factory->yieldContent('foo'));
}

public function testSectionExtending()
{
$placeholder = \Illuminate\View\Factory::parentPlaceholder('foo');
Expand Down

0 comments on commit 405fd27

Please sign in to comment.