8000 [5.6] Fix translation escaping (#25858) · laravel/framework@1807c92 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1807c92

Browse files
X-Coder264taylorotwell
authored andcommitted
[5.6] Fix translation escaping (#25858)
* Fix translation escaping * Update Translator.php
1 parent fa5ea32 commit 1807c92

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

src/Illuminate/Translation/Translator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ protected function makeReplacements($line, array $replace)
264264
$replace = $this->sortReplacements($replace);
265265

266266
foreach ($replace as $key => $value) {
267+
$value = e($value);
268+
267269
$line = str_replace(
268270
[':'.$key, ':'.Str::upper($key), ':'.Str::ucfirst($key)],
269271
[$value, Str::upper($value), Str::ucfirst($value)],

src/Illuminate/View/Compilers/Concerns/CompilesTranslations.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ protected function compileLang($expression)
1818
return "<?php \$__env->startTranslation{$expression}; ?>";
1919
}
2020

21-
return "<?php echo e(app('translator')->getFromJson{$expression}); ?>";
21+
return "<?php echo app('translator')->getFromJson{$expression}; ?>";
2222
}
2323

2424
/**
@@ -28,7 +28,7 @@ protected function compileLang($expression)
2828
*/
2929
protected function compileEndlang()
3030
{
31-
return '<?php echo e($__env->renderTranslation()); ?>';
31+
return '<?php echo $__env->renderTranslation(); ?>';
3232
}
3333

3434
/**
@@ -39,6 +39,6 @@ protected function compileEndlang()
3939
*/
4040
protected function compileChoice($expression)
4141
{
42-
return "<?php echo e(app('translator')->choice{$expression}); ?>";
42+
return "<?php echo app('translator')->choice{$expression}; ?>";
4343
}
4444
}

tests/Translation/TranslationTranslatorTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ public function testGetMethodProperlyLoadsAndRetrievesItem()
4949
$this->assertEquals('foo', $t->get('foo::bar.foo'));
5050
}
5151

52+
public function testTransMethodProperlyLoadsAndRetrievesItemWithHTMLReplacements()
53+
{
54+
$t = new \Illuminate\Translation\Translator($this->getLoader(), 'en');
55+
$t->getLoader()->shouldReceive('load')->once()->with('en', 'foo', '*')->andReturn(['bar' => 'breeze :foo']);
56+
$this->assertSame('breeze &lt;p&gt;test&lt;/p&gt;', $t->trans('foo.bar', ['foo' => '<p>test</p>'], 'en'));
57+
}
58+
59+
public function testTransMethodProperlyLoadsAndRetrievesItemWithHTMLInTheMessage()
60+
{
61+
$t = new \Illuminate\Translation\Translator($this->getLoader(), 'en');
62+
$t->getLoader()->shouldReceive('load')->once()->with('en', 'foo', '*')->andReturn(['bar' => 'breeze <p>test</p>']);
63+
$this->assertSame('breeze <p>test</p>', $t->trans('foo.bar', [], 'en'));
64+
}
65+
5266
public function testGetMethodProperlyLoadsAndRetrievesItemWithCapitalization()
5367
{
5468
$t = $this->getMockBuilder('Illuminate\Translation\Translator')->setMethods(null)->setConstructorArgs([$this->getLoader(), 'en'])->getMock();

tests/View/Blade/BladeExpressionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ class BladeExpressionTest extends AbstractBladeTestCase
66
{
77
public function testExpressionsOnTheSameLine()
88
{
9-
$this->assertEquals('<?php echo e(app(\'translator\')->getFromJson(foo(bar(baz(qux(breeze())))))); ?> space () <?php echo e(app(\'translator\')->getFromJson(foo(bar))); ?>', $this->compiler->compileString('@lang(foo(bar(baz(qux(breeze()))))) space () @lang(foo(bar))'));
9+
$this->assertEquals('<?php echo app(\'translator\')->getFromJson(foo(bar(baz(qux(breeze()))))); ?> space () <?php echo app(\'translator\')->getFromJson(foo(bar)); ?>', $this->compiler->compileString('@lang(foo(bar(baz(qux(breeze()))))) space () @lang(foo(bar))'));
1010
}
1111

1212
public function testExpressionWithinHTML()
1313
{
1414
$this->assertEquals('<html <?php echo e($foo); ?>>', $this->compiler->compileString('<html {{ $foo }}>'));
1515
$this->assertEquals('<html<?php echo e($foo); ?>>', $this->compiler->compileString('<html{{ $foo }}>'));
16-
$this->assertEquals('<html <?php echo e($foo); ?> <?php echo e(app(\'translator\')->getFromJson(\'foo\')); ?>>', $this->compiler->compileString('<html {{ $foo }} @lang(\'foo\')>'));
16+
$this->assertEquals('<html <?php echo e($foo); ?> <?php echo app(\'translator\')->getFromJson(\'foo\'); ?>>', $this->compiler->compileString('<html {{ $foo }} @lang(\'foo\')>'));
1717
}
1818
}

tests/View/Blade/BladeLangTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ class BladeLangTest extends AbstractBladeTestCase
77
public function testStatementThatContainsNonConsecutiveParenthesisAreCompiled()
88
{
99
$string = "Foo @lang(function_call('foo(blah)')) bar";
10-
$expected = "Foo <?php echo e(app('translator')->getFromJson(function_call('foo(blah)'))); ?> bar";
10+
$expected = "Foo <?php echo app('translator')->getFromJson(function_call('foo(blah)')); ?> bar";
1111
$this->assertEquals($expected, $this->compiler->compileString($string));
1212
}
1313

1414
public function testLanguageAndChoicesAreCompiled()
1515
{
16-
$this->assertEquals('<?php echo e(app(\'translator\')->getFromJson(\'foo\')); ?>', $this->compiler->compileString("@lang('foo')"));
17-
$this->assertEquals('<?php echo e(app(\'translator\')->choice(\'foo\', 1)); ?>', $this->compiler->compileString("@choice('foo', 1)"));
16+
$this->assertEquals('<?php echo app(\'translator\')->getFromJson(\'foo\'); ?>', $this->compiler->compileString("@lang('foo')"));
17+
$this->assertEquals('<?php echo app(\'translator\')->choice(\'foo\', 1); ?>', $this->compiler->compileString("@choice('foo', 1)"));
1818
}
1919
}

0 commit comments

Comments
 (0)
0