Skip to content
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

[10.x] Improved Handling of Empty Component Slots with HTML Comments or Line Breaks #49966

Merged
merged 4 commits into from
Feb 9, 2024
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
20 changes: 20 additions & 0 deletions src/Illuminate/View/ComponentSlot.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\View;

use Illuminate\Contracts\Support\Htmlable;
use InvalidArgumentException;

class ComponentSlot implements Htmlable
{
Expand Down Expand Up @@ -77,6 +78,25 @@ public function isNotEmpty()
return ! $this->isEmpty();
}

/**
* Determine if the slot has non-comment content.
*
* @param callable|string|null $callable
* @return bool
*/
public function hasActualContent(callable|string|null $callable = null)
{
if (is_string($callable) && ! function_exists($callable)) {
throw new InvalidArgumentException('Callable does not exist.');
}

return filter_var(
$this->contents,
FILTER_CALLBACK,
['options' => $callable ?? fn ($input) => trim(preg_replace("/<!--([\s\S]*?)-->/", '', $input))]
) !== '';
}

/**
* Get the slot's HTML string.
*
Expand Down
58 changes: 58 additions & 0 deletions tests/View/ComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Support\Facades\Facade;
use Illuminate\Support\HtmlString;
use Illuminate\View\Component;
use Illuminate\View\ComponentSlot;
use Illuminate\View\Factory;
use Illuminate\View\View;
use Mockery as m;
Expand Down Expand Up @@ -305,6 +306,63 @@ public function testFactoryGetsSharedBetweenComponents()
Component::forgetFactory();
$this->assertNotSame($this->viewFactory, $getFactory($inline));
}

public function testComponentSlotIsEmpty()
{
$slot = new ComponentSlot();

$this->assertTrue((bool) $slot->isEmpty());
}

public function testComponentSlotSanitizedEmpty()
{
// default sanitizer should remove all html tags
$slot = new ComponentSlot('<!-- test -->');

$linebreakingSlot = new ComponentSlot("\n \t");

$moreComplexSlot = new ComponentSlot('<!--
<p>commented HTML</p>
<img border="0" src="" alt="">
-->');

$this->assertFalse((bool) $slot->hasActualContent());
$this->assertFalse((bool) $linebreakingSlot->hasActualContent('trim'));
$this->assertFalse((bool) $moreComplexSlot->hasActualContent());
}

public function testComponentSlotSanitizedNotEmpty()
{
// default sanitizer should remove all html tags
$slot = new ComponentSlot('<!-- test -->not empty');

$linebreakingSlot = new ComponentSlot("\ntest \t");

$moreComplexSlot = new ComponentSlot('before<!--
<p>commented HTML</p>
<img border="0" src="" alt="">
-->after');

$this->assertTrue((bool) $slot->hasActualContent());
$this->assertTrue((bool) $linebreakingSlot->hasActualContent('trim'));
$this->assertTrue((bool) $moreComplexSlot->hasActualContent());
}

public function testComponentSlotIsNotEmpty()
{
$slot = new ComponentSlot('test');

$anotherSlot = new ComponentSlot('test<!-- test -->');

$moreComplexSlot = new ComponentSlot('t<!--
<p>Look at this cool image:</p>
<img border="0" src="pic_trulli.jpg" alt="Trulli">
-->est');

$this->assertTrue((bool) $slot->hasActualContent());
$this->assertTrue((bool) $anotherSlot->hasActualContent());
$this->assertTrue((bool) $moreComplexSlot->hasActualContent());
}
}

class TestInlineViewComponent extends Component
Expand Down
Loading