Skip to content

[12.x] AbstractPaginator should implement CanBeEscapedWhenCastToString #55256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/Illuminate/Pagination/AbstractPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Pagination;

use Closure;
use Illuminate\Contracts\Support\CanBeEscapedWhenCastToString;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
Expand All @@ -18,7 +19,7 @@
*
* @mixin \Illuminate\Support\Collection<TKey, TValue>
*/
abstract class AbstractPaginator implements Htmlable, Stringable
abstract class AbstractPaginator implements CanBeEscapedWhenCastToString, Htmlable, Stringable
{
use ForwardsCalls, Tappable;

Expand Down Expand Up @@ -71,6 +72,13 @@ abstract class AbstractPaginator implements Htmlable, Stringable
*/
protected $pageName = 'page';

/**
* Indicates that the paginator's string representation should be escaped when __toString is invoked.
*
* @var bool
*/
protected $escapeWhenCastingToString = false;

/**
* The number of links to display on each side of current page link.
*
Expand Down Expand Up @@ -797,6 +805,21 @@ public function __call($method, $parameters)
*/
public function __toString()
{
return (string) $this->render();
return $this->escapeWhenCastingToString
? e((string) $this->render())
: (string) $this->render();
}

/**
* Indicate that the paginator's string representation should be escaped when __toString is invoked.
*
* @param bool $escape
* @return $this
*/
public function escapeWhenCastingToString($escape = true)
{
$this->escapeWhenCastingToString = $escape;

return $this;
}
}
5 changes: 5 additions & 0 deletions tests/View/Blade/BladeComponentTagCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\AbstractPaginator;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Compilers\ComponentTagCompiler;
use Illuminate\View\Component;
Expand Down Expand Up @@ -799,11 +800,15 @@ public function __toString()
$model = new class extends Model {
};

$paginator = new class extends AbstractPaginator {
};

$this->assertEquals(e('<hi>'), BladeCompiler::sanitizeComponentAttribute('<hi>'));
$this->assertEquals(e('1'), BladeCompiler::sanitizeComponentAttribute('1'));
$this->assertEquals(1, BladeCompiler::sanitizeComponentAttribute(1));
$this->assertEquals(e('<hi>'), BladeCompiler::sanitizeComponentAttribute($class));
$this->assertSame($model, BladeCompiler::sanitizeComponentAttribute($model));
$this->assertSame($paginator, BladeCompiler::sanitizeComponentAttribute($paginator));
}

public function testItThrowsAnExceptionForNonExistingAliases()
Expand Down
Loading