Skip to content

Commit

Permalink
Simple quick fix for laravel#10068
Browse files Browse the repository at this point in the history
  • Loading branch information
mcgrogan91 committed Oct 2, 2015
1 parent 6189acd commit 5d19d07
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 36 deletions.
19 changes: 19 additions & 0 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,25 @@ function e($value)
}
}

if(! function_exists('be')) {
/**
* Escape Blade-specific HTML entities in a string.
*
* @param \Illuminate\Support\Htmlable|string $value
* @return string
*/
function be($value)
{
if ($value instanceof Htmlable) {
return $value->toHtml();
}

$value = e($value, ENT_QUOTES, 'UTF-8', false);
$value = str_replace("@", "@", $value);
return $value;
}
}

if (! function_exists('ends_with')) {
/**
* Determine if a given string ends with a given substring.
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class BladeCompiler extends Compiler implements CompilerInterface
*
* @var string
*/
protected $echoFormat = 'e(%s)';
protected $echoFormat = 'be(%s)';

/**
* Array of footer lines to be added to template.
Expand Down Expand Up @@ -331,7 +331,7 @@ protected function compileEscapedEchos($value)
$callback = function ($matches) {
$whitespace = empty($matches[3]) ? '' : $matches[3].$matches[3];

return $matches[1] ? $matches[0] : '<?php echo e('.$this->compileEchoDefaults($matches[2]).'); ?>'.$whitespace;
return $matches[1] ? $matches[0] : '<?php echo be('.$this->compileEchoDefaults($matches[2]).'); ?>'.$whitespace;
};

return preg_replace_callback($pattern, $callback, $value);
Expand Down
68 changes: 34 additions & 34 deletions tests/View/ViewBladeCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,52 +94,52 @@ public function testEchosAreCompiled()
!!}'));
$this->assertEquals('<?php echo isset($name) ? $name : \'foo\'; ?>', $compiler->compileString('{!! $name or \'foo\' !!}'));

$this->assertEquals('<?php echo e($name); ?>', $compiler->compileString('{{{$name}}}'));
$this->assertEquals('<?php echo e($name); ?>', $compiler->compileString('{{$name}}'));
$this->assertEquals('<?php echo e($name); ?>', $compiler->compileString('{{ $name }}'));
$this->assertEquals('<?php echo e($name); ?>', $compiler->compileString('{{
$this->assertEquals('<?php echo be($name); ?>', $compiler->compileString('{{{$name}}}'));
$this->assertEquals('<?php echo be($name); ?>', $compiler->compileString('{{$name}}'));
$this->assertEquals('<?php echo be($name); ?>', $compiler->compileString('{{ $name }}'));
$this->assertEquals('<?php echo be($name); ?>', $compiler->compileString('{{
$name
}}'));
$this->assertEquals("<?php echo e(\$name); ?>\n\n", $compiler->compileString("{{ \$name }}\n"));
$this->assertEquals("<?php echo e(\$name); ?>\r\n\r\n", $compiler->compileString("{{ \$name }}\r\n"));
$this->assertEquals("<?php echo e(\$name); ?>\n\n", $compiler->compileString("{{ \$name }}\n"));
$this->assertEquals("<?php echo e(\$name); ?>\r\n\r\n", $compiler->compileString("{{ \$name }}\r\n"));

$this->assertEquals('<?php echo e(isset($name) ? $name : "foo"); ?>', $compiler->compileString('{{ $name or "foo" }}'));
$this->assertEquals('<?php echo e(isset($user->name) ? $user->name : "foo"); ?>', $compiler->compileString('{{ $user->name or "foo" }}'));
$this->assertEquals('<?php echo e(isset($name) ? $name : "foo"); ?>', $compiler->compileString('{{$name or "foo"}}'));
$this->assertEquals('<?php echo e(isset($name) ? $name : "foo"); ?>', $compiler->compileString('{{
$this->assertEquals("<?php echo be(\$name); ?>\n\n", $compiler->compileString("{{ \$name }}\n"));
$this->assertEquals("<?php echo be(\$name); ?>\r\n\r\n", $compiler->compileString("{{ \$name }}\r\n"));
$this->assertEquals("<?php echo be(\$name); ?>\n\n", $compiler->compileString("{{ \$name }}\n"));
$this->assertEquals("<?php echo be(\$name); ?>\r\n\r\n", $compiler->compileString("{{ \$name }}\r\n"));

$this->assertEquals('<?php echo be(isset($name) ? $name : "foo"); ?>', $compiler->compileString('{{ $name or "foo" }}'));
$this->assertEquals('<?php echo be(isset($user->name) ? $user->name : "foo"); ?>', $compiler->compileString('{{ $user->name or "foo" }}'));
$this->assertEquals('<?php echo be(isset($name) ? $name : "foo"); ?>', $compiler->compileString('{{$name or "foo"}}'));
$this->assertEquals('<?php echo be(isset($name) ? $name : "foo"); ?>', $compiler->compileString('{{
$name or "foo"
}}'));

$this->assertEquals('<?php echo e(isset($name) ? $name : \'foo\'); ?>', $compiler->compileString('{{ $name or \'foo\' }}'));
$this->assertEquals('<?php echo e(isset($name) ? $name : \'foo\'); ?>', $compiler->compileString('{{$name or \'foo\'}}'));
$this->assertEquals('<?php echo e(isset($name) ? $name : \'foo\'); ?>', $compiler->compileString('{{
$this->assertEquals('<?php echo be(isset($name) ? $name : \'foo\'); ?>', $compiler->compileString('{{ $name or \'foo\' }}'));
$this->assertEquals('<?php echo be(isset($name) ? $name : \'foo\'); ?>', $compiler->compileString('{{$name or \'foo\'}}'));
$this->assertEquals('<?php echo be(isset($name) ? $name : \'foo\'); ?>', $compiler->compileString('{{
$name or \'foo\'
}}'));

$this->assertEquals('<?php echo e(isset($age) ? $age : 90); ?>', $compiler->compileString('{{ $age or 90 }}'));
$this->assertEquals('<?php echo e(isset($age) ? $age : 90); ?>', $compiler->compileString('{{$age or 90}}'));
$this->assertEquals('<?php echo e(isset($age) ? $age : 90); ?>', $compiler->compileString('{{
$this->assertEquals('<?php echo be(isset($age) ? $age : 90); ?>', $compiler->compileString('{{ $age or 90 }}'));
$this->assertEquals('<?php echo be(isset($age) ? $age : 90); ?>', $compiler->compileString('{{$age or 90}}'));
$this->assertEquals('<?php echo be(isset($age) ? $age : 90); ?>', $compiler->compileString('{{
$age or 90
}}'));

$this->assertEquals('<?php echo e("Hello world or foo"); ?>', $compiler->compileString('{{ "Hello world or foo" }}'));
$this->assertEquals('<?php echo e("Hello world or foo"); ?>', $compiler->compileString('{{"Hello world or foo"}}'));
$this->assertEquals('<?php echo e($foo + $or + $baz); ?>', $compiler->compileString('{{$foo + $or + $baz}}'));
$this->assertEquals('<?php echo e("Hello world or foo"); ?>', $compiler->compileString('{{
$this->assertEquals('<?php echo be("Hello world or foo"); ?>', $compiler->compileString('{{ "Hello world or foo" }}'));
$this->assertEquals('<?php echo be("Hello world or foo"); ?>', $compiler->compileString('{{"Hello world or foo"}}'));
$this->assertEquals('<?php echo be($foo + $or + $baz); ?>', $compiler->compileString('{{$foo + $or + $baz}}'));
$this->assertEquals('<?php echo be("Hello world or foo"); ?>', $compiler->compileString('{{
"Hello world or foo"
}}'));

$this->assertEquals('<?php echo e(\'Hello world or foo\'); ?>', $compiler->compileString('{{ \'Hello world or foo\' }}'));
$this->assertEquals('<?php echo e(\'Hello world or foo\'); ?>', $compiler->compileString('{{\'Hello world or foo\'}}'));
$this->assertEquals('<?php echo e(\'Hello world or foo\'); ?>', $compiler->compileString('{{
$this->assertEquals('<?php echo be(\'Hello world or foo\'); ?>', $compiler->compileString('{{ \'Hello world or foo\' }}'));
$this->assertEquals('<?php echo be(\'Hello world or foo\'); ?>', $compiler->compileString('{{\'Hello world or foo\'}}'));
$this->assertEquals('<?php echo be(\'Hello world or foo\'); ?>', $compiler->compileString('{{
\'Hello world or foo\'
}}'));

$this->assertEquals('<?php echo e(myfunc(\'foo or bar\')); ?>', $compiler->compileString('{{ myfunc(\'foo or bar\') }}'));
$this->assertEquals('<?php echo e(myfunc("foo or bar")); ?>', $compiler->compileString('{{ myfunc("foo or bar") }}'));
$this->assertEquals('<?php echo e(myfunc("$name or \'foo\'")); ?>', $compiler->compileString('{{ myfunc("$name or \'foo\'") }}'));
$this->assertEquals('<?php echo be(myfunc(\'foo or bar\')); ?>', $compiler->compileString('{{ myfunc(\'foo or bar\') }}'));
$this->assertEquals('<?php echo be(myfunc("foo or bar")); ?>', $compiler->compileString('{{ myfunc("foo or bar") }}'));
$this->assertEquals('<?php echo be(myfunc("$name or \'foo\'")); ?>', $compiler->compileString('{{ myfunc("$name or \'foo\'") }}'));
}

public function testEscapedWithAtEchosAreCompiled()
Expand Down Expand Up @@ -498,7 +498,7 @@ public function testCustomPhpCodeIsCorrectlyHandled()
public function testMixingYieldAndEcho()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals('<?php echo $__env->yieldContent(\'title\'); ?> - <?php echo e(Config::get(\'site.title\')); ?>', $compiler->compileString("@yield('title') - {{Config::get('site.title')}}"));
$this->assertEquals('<?php echo $__env->yieldContent(\'title\'); ?> - <?php echo be(Config::get(\'site.title\')); ?>', $compiler->compileString("@yield('title') - {{Config::get('site.title')}}"));
}

public function testCustomExtensionsAreCompiled()
Expand Down Expand Up @@ -543,7 +543,7 @@ public function testRawTagsCanBeSetToLegacyValues()
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$compiler->setEchoFormat('%s');

$this->assertEquals('<?php echo e($name); ?>', $compiler->compileString('{{{ $name }}}'));
$this->assertEquals('<?php echo be($name); ?>', $compiler->compileString('{{{ $name }}}'));
$this->assertEquals('<?php echo $name; ?>', $compiler->compileString('{{ $name }}'));
$this->assertEquals('<?php echo $name; ?>', $compiler->compileString('{{
$name
Expand All @@ -559,9 +559,9 @@ public function testExpressionsOnTheSameLine()
public function testExpressionWithinHTML()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals('<html <?php echo e($foo); ?>>', $compiler->compileString('<html {{ $foo }}>'));
$this->assertEquals('<html<?php echo e($foo); ?>>', $compiler->compileString('<html{{ $foo }}>'));
$this->assertEquals('<html <?php echo e($foo); ?> <?php echo app(\'translator\')->get(\'foo\'); ?>>', $compiler->compileString('<html {{ $foo }} @lang(\'foo\')>'));
$this->assertEquals('<html <?php echo be($foo); ?>>', $compiler->compileString('<html {{ $foo }}>'));
$this->assertEquals('<html<?php echo be($foo); ?>>', $compiler->compileString('<html{{ $foo }}>'));
$this->assertEquals('<html <?php echo be($foo); ?> <?php echo app(\'translator\')->get(\'foo\'); ?>>', $compiler->compileString('<html {{ $foo }} @lang(\'foo\')>'));
}

protected function getFiles()
Expand Down

0 comments on commit 5d19d07

Please sign in to comment.