Skip to content

Commit 83d07e9

Browse files
[12.x] AbstractPaginator should implement CanBeEscapedWhenCastToString (#55256)
* Add failing test * Update AbstractPaginator * Update AbstractPaginator.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent e6b7c31 commit 83d07e9

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/Illuminate/Pagination/AbstractPaginator.php

+25-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Pagination;
44

55
use Closure;
6+
use Illuminate\Contracts\Support\CanBeEscapedWhenCastToString;
67
use Illuminate\Contracts\Support\Htmlable;
78
use Illuminate\Support\Arr;
89
use Illuminate\Support\Collection;
@@ -18,7 +19,7 @@
1819
*
1920
* @mixin \Illuminate\Support\Collection<TKey, TValue>
2021
*/
21-
abstract class AbstractPaginator implements Htmlable, Stringable
22+
abstract class AbstractPaginator implements CanBeEscapedWhenCastToString, Htmlable, Stringable
2223
{
2324
use ForwardsCalls, Tappable;
2425

@@ -71,6 +72,13 @@ abstract class AbstractPaginator implements Htmlable, Stringable
7172
*/
7273
protected $pageName = 'page';
7374

75+
/**
76+
* Indicates that the paginator's string representation should be escaped when __toString is invoked.
77+
*
78+
* @var bool
79+
*/
80+
protected $escapeWhenCastingToString = false;
81+
7482
/**
7583
* The number of links to display on each side of current page link.
7684
*
@@ -797,6 +805,21 @@ public function __call($method, $parameters)
797805
*/
798806
public function __toString()
799807
{
800-
return (string) $this->render();
808+
return $this->escapeWhenCastingToString
809+
? e((string) $this->render())
810+
: (string) $this->render();
811+
}
812+
813+
/**
814+
* Indicate that the paginator's string representation should be escaped when __toString is invoked.
815+
*
816+
* @param bool $escape
817+
* @return $this
818+
*/
819+
public function escapeWhenCastingToString($escape = true)
820+
{
821+
$this->escapeWhenCastingToString = $escape;
822+
823+
return $this;
801824
}
802825
}

tests/View/Blade/BladeComponentTagCompilerTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Contracts\Foundation\Application;
77
use Illuminate\Contracts\View\Factory;
88
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Pagination\AbstractPaginator;
910
use Illuminate\View\Compilers\BladeCompiler;
1011
use Illuminate\View\Compilers\ComponentTagCompiler;
1112
use Illuminate\View\Component;
@@ -799,11 +800,15 @@ public function __toString()
799800
$model = new class extends Model {
800801
};
801802

803+
$paginator = new class extends AbstractPaginator {
804+
};
805+
802806
$this->assertEquals(e('<hi>'), BladeCompiler::sanitizeComponentAttribute('<hi>'));
803807
$this->assertEquals(e('1'), BladeCompiler::sanitizeComponentAttribute('1'));
804808
$this->assertEquals(1, BladeCompiler::sanitizeComponentAttribute(1));
805809
$this->assertEquals(e('<hi>'), BladeCompiler::sanitizeComponentAttribute($class));
806810
$this->assertSame($model, BladeCompiler::sanitizeComponentAttribute($model));
811+
$this->assertSame($paginator, BladeCompiler::sanitizeComponentAttribute($paginator));
807812
}
808813

809814
public function testItThrowsAnExceptionForNonExistingAliases()

0 commit comments

Comments
 (0)