From 405fd27569a6fb9c8cc42f7fa3969f09c0ec642d Mon Sep 17 00:00:00 2001 From: ockle Date: Mon, 3 Jul 2017 21:34:19 +0100 Subject: [PATCH] [5.4] Don't re-escape a View instance passed as the default value to 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 --- .../View/Concerns/ManagesLayouts.php | 5 ++- tests/View/ViewFactoryTest.php | 43 +++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/View/Concerns/ManagesLayouts.php b/src/Illuminate/View/Concerns/ManagesLayouts.php index c9fc1e2cdd74..b3b4aefe3699 100644 --- a/src/Illuminate/View/Concerns/ManagesLayouts.php +++ b/src/Illuminate/View/Concerns/ManagesLayouts.php @@ -3,6 +3,7 @@ namespace Illuminate\View\Concerns; use InvalidArgumentException; +use Illuminate\Contracts\View\View; trait ManagesLayouts { @@ -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)); } } @@ -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]; diff --git a/tests/View/ViewFactoryTest.php b/tests/View/ViewFactoryTest.php index 23afb6157e4f..571525ae63fe 100755 --- a/tests/View/ViewFactoryTest.php +++ b/tests/View/ViewFactoryTest.php @@ -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('<p>hi</p>', $factory->yieldContent('foo', '

hi

')); + } + + public function testYieldDefaultViewIsNotEscapedTwice() + { + $factory = $this->getFactory(); + $view = m::mock('Illuminate\View\View'); + $view->shouldReceive('__toString')->once()->andReturn('

hi

<p>already escaped</p>'); + $this->assertEquals('

hi

<p>already escaped</p>', $factory->yieldContent('foo', $view)); + } + public function testBasicSectionHandling() { $factory = $this->getFactory(); @@ -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', '

hi

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

hi

<p>already escaped</p>'); + $factory->startSection('foo', $view); + $this->assertEquals('

hi

<p>already escaped</p>', $factory->yieldContent('foo')); + } + public function testSectionExtending() { $placeholder = \Illuminate\View\Factory::parentPlaceholder('foo');