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] Allow component slots to be empty, even if they have an html comment or line break #49935

Closed
wants to merge 7 commits into from
Closed
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
35 changes: 34 additions & 1 deletion src/Illuminate/View/ComponentSlot.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ class ComponentSlot implements Htmlable
*/
protected $contents;

/**
* The slot sanitizer.
*
* @var callable
*/
protected $sanitizerResolver;

/**
* Create a new slot instance.
*
Expand All @@ -32,6 +39,9 @@ public function __construct($contents = '', $attributes = [])
$this->contents = $contents;

$this->withAttributes($attributes);

// default sanitizer, return the input as it is
$this->sanitizerResolver = fn ($input) => $input;
}

/**
Expand All @@ -57,14 +67,37 @@ public function toHtml()
return $this->contents;
}

/**
* Setup the sanitizer for the slot.
*
* @param null|string|callable $callable
* @return $this
*/
public function sanitize(null|string|callable $callable = null)
{
if (is_string($callable) && ! function_exists($callable)) {
throw new \InvalidArgumentException('Callable does not exist.');
}

$this->sanitizerResolver =
$callable ??
fn ($input) => trim(preg_replace("/<!--([\s\S]*?)-->/", '', $input)); // replace everything between <!-- and --> with empty string

return $this;
}

/**
* Determine if the slot is empty.
*
* HTML comments and whitespace will be trimmed out.
*
* @return bool
*/
public function isEmpty()
{
return $this->contents === '';
return filter_var($this->contents, FILTER_CALLBACK, [
'options' => $this->sanitizerResolver,
]) === '';
}

/**
Expand Down
48 changes: 48 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,53 @@ public function testFactoryGetsSharedBetweenComponents()
Component::forgetFactory();
$this->assertNotSame($this->viewFactory, $getFactory($inline));
}

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

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

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

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

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

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

$this->assertTrue((bool) $slot->sanitize()->isEmpty());
$this->assertTrue((bool) $linebreakingSlot->sanitize()->isEmpty());
$this->assertTrue((bool) $moreComplexSlot->sanitize()->isEmpty());

$this->assertTrue((bool) (clone $whitespaceSlot)->sanitize('trim')->isEmpty());
$this->assertTrue((bool) $whitespaceSlot->isNotEmpty());
}

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->isNotEmpty());

$this->assertTrue((bool) $anotherSlot->sanitize()->isNotEmpty());

$this->assertTrue((bool) $moreComplexSlot->sanitize()->isNotEmpty());
}
}

class TestInlineViewComponent extends Component
Expand Down
Loading