From 9b2dc1e690d5aa19f3d7085dd2fa777d9fc6674d Mon Sep 17 00:00:00 2001 From: Jeremias Wolff Date: Sat, 3 Feb 2024 16:03:14 +0100 Subject: [PATCH 1/4] implement alternative solution for PR#49935 --- src/Illuminate/View/ComponentSlot.php | 30 ++++++++++++++ tests/View/ComponentTest.php | 60 +++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/src/Illuminate/View/ComponentSlot.php b/src/Illuminate/View/ComponentSlot.php index 85665ad64575..9d66109c0b68 100644 --- a/src/Illuminate/View/ComponentSlot.php +++ b/src/Illuminate/View/ComponentSlot.php @@ -77,6 +77,36 @@ public function isNotEmpty() return ! $this->isEmpty(); } + /** + * Determine if the slot is empty after being sanitized. + * + * @param null|string|callable $callable + * @return bool + */ + public function sanitizedEmpty(null|string|callable $callable = null) + { + if (is_string($callable) && ! function_exists($callable)) { + throw new \InvalidArgumentException('Callable does not exist.'); + } + + $resolver = + $callable ?? + fn ($input) => trim(preg_replace("//", '', $input)); // replace everything between with empty string + + return filter_var($this->contents, FILTER_CALLBACK, ['options' => $resolver,]) === ''; + } + + /** + * Determine if the slot is not empty after being sanitized. + * + * @param null|string|callable $callable + * @return bool + */ + public function sanitizedNotEmpty(null|string|callable $callable = null) + { + return ! $this->sanitizedEmpty($callable); + } + /** * Get the slot's HTML string. * diff --git a/tests/View/ComponentTest.php b/tests/View/ComponentTest.php index a5ff36d82531..55f7d4cdc7cc 100644 --- a/tests/View/ComponentTest.php +++ b/tests/View/ComponentTest.php @@ -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; @@ -305,6 +306,65 @@ 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(''); + + $linebreakingSlot = new ComponentSlot("\n \t"); + + $moreComplexSlot = new ComponentSlot(''); + + $this->assertTrue((bool) $slot->sanitizedEmpty()); + $this->assertTrue((bool) $linebreakingSlot->sanitizedEmpty('trim')); + $this->assertTrue((bool) $moreComplexSlot->sanitizedEmpty()); + } + + public function testComponentSlotSanitizedNotEmpty() + { + // default sanitizer should remove all html tags + $slot = new ComponentSlot('not empty'); + + $linebreakingSlot = new ComponentSlot("\ntest \t"); + + $moreComplexSlot = new ComponentSlot('beforeafter'); + + $this->assertTrue((bool) $slot->sanitizedNotEmpty()); + $this->assertTrue((bool) $linebreakingSlot->sanitizedNotEmpty('trim')); + $this->assertTrue((bool) $moreComplexSlot->sanitizedNotEmpty()); + } + + public function testComponentSlotIsNotEmpty() + { + $slot = new ComponentSlot('test'); + + $anotherSlot = new ComponentSlot('test'); + + $moreComplexSlot = new ComponentSlot('test'); + + $this->assertTrue((bool) $slot->sanitizedNotEmpty()); + + $this->assertTrue((bool) $anotherSlot->sanitizedNotEmpty()); + + $this->assertTrue((bool) $moreComplexSlot->sanitizedNotEmpty()); + } } class TestInlineViewComponent extends Component From 72b15354b4405f35ef41188f7790e30112a61310 Mon Sep 17 00:00:00 2001 From: Jeremias Wolff Date: Sat, 3 Feb 2024 16:10:35 +0100 Subject: [PATCH 2/4] style ci --- src/Illuminate/View/ComponentSlot.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/View/ComponentSlot.php b/src/Illuminate/View/ComponentSlot.php index 9d66109c0b68..8c8fe25b7ed6 100644 --- a/src/Illuminate/View/ComponentSlot.php +++ b/src/Illuminate/View/ComponentSlot.php @@ -91,9 +91,10 @@ public function sanitizedEmpty(null|string|callable $callable = null) $resolver = $callable ?? - fn ($input) => trim(preg_replace("//", '', $input)); // replace everything between with empty string + fn ($input) => trim(preg_replace("//", '', $input)); + // replace everything between with empty string - return filter_var($this->contents, FILTER_CALLBACK, ['options' => $resolver,]) === ''; + return filter_var($this->contents, FILTER_CALLBACK, ['options' => $resolver]) === ''; } /** From 0e21f1b38b703a681d6939d3b0f64fe4900c441a Mon Sep 17 00:00:00 2001 From: Jeremias Wolff Date: Sat, 3 Feb 2024 16:11:52 +0100 Subject: [PATCH 3/4] style ci --- src/Illuminate/View/ComponentSlot.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/View/ComponentSlot.php b/src/Illuminate/View/ComponentSlot.php index 8c8fe25b7ed6..167da3db8f2b 100644 --- a/src/Illuminate/View/ComponentSlot.php +++ b/src/Illuminate/View/ComponentSlot.php @@ -92,7 +92,7 @@ public function sanitizedEmpty(null|string|callable $callable = null) $resolver = $callable ?? fn ($input) => trim(preg_replace("//", '', $input)); - // replace everything between with empty string + // replace everything between with empty string return filter_var($this->contents, FILTER_CALLBACK, ['options' => $resolver]) === ''; } From 0212f6c4848c5f8ae52603388f14cafa193bef79 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 9 Feb 2024 10:13:42 -0600 Subject: [PATCH 4/4] formatting --- src/Illuminate/View/ComponentSlot.php | 31 +++++++++------------------ tests/View/ComponentTest.php | 20 ++++++++--------- 2 files changed, 19 insertions(+), 32 deletions(-) diff --git a/src/Illuminate/View/ComponentSlot.php b/src/Illuminate/View/ComponentSlot.php index 167da3db8f2b..dc48dbc88798 100644 --- a/src/Illuminate/View/ComponentSlot.php +++ b/src/Illuminate/View/ComponentSlot.php @@ -3,6 +3,7 @@ namespace Illuminate\View; use Illuminate\Contracts\Support\Htmlable; +use InvalidArgumentException; class ComponentSlot implements Htmlable { @@ -78,34 +79,22 @@ public function isNotEmpty() } /** - * Determine if the slot is empty after being sanitized. + * Determine if the slot has non-comment content. * - * @param null|string|callable $callable + * @param callable|string|null $callable * @return bool */ - public function sanitizedEmpty(null|string|callable $callable = null) + public function hasActualContent(callable|string|null $callable = null) { if (is_string($callable) && ! function_exists($callable)) { - throw new \InvalidArgumentException('Callable does not exist.'); + throw new InvalidArgumentException('Callable does not exist.'); } - $resolver = - $callable ?? - fn ($input) => trim(preg_replace("//", '', $input)); - // replace everything between with empty string - - return filter_var($this->contents, FILTER_CALLBACK, ['options' => $resolver]) === ''; - } - - /** - * Determine if the slot is not empty after being sanitized. - * - * @param null|string|callable $callable - * @return bool - */ - public function sanitizedNotEmpty(null|string|callable $callable = null) - { - return ! $this->sanitizedEmpty($callable); + return filter_var( + $this->contents, + FILTER_CALLBACK, + ['options' => $callable ?? fn ($input) => trim(preg_replace("//", '', $input))] + ) !== ''; } /** diff --git a/tests/View/ComponentTest.php b/tests/View/ComponentTest.php index 55f7d4cdc7cc..87e716edcee7 100644 --- a/tests/View/ComponentTest.php +++ b/tests/View/ComponentTest.php @@ -326,9 +326,9 @@ public function testComponentSlotSanitizedEmpty() -->'); - $this->assertTrue((bool) $slot->sanitizedEmpty()); - $this->assertTrue((bool) $linebreakingSlot->sanitizedEmpty('trim')); - $this->assertTrue((bool) $moreComplexSlot->sanitizedEmpty()); + $this->assertFalse((bool) $slot->hasActualContent()); + $this->assertFalse((bool) $linebreakingSlot->hasActualContent('trim')); + $this->assertFalse((bool) $moreComplexSlot->hasActualContent()); } public function testComponentSlotSanitizedNotEmpty() @@ -343,9 +343,9 @@ public function testComponentSlotSanitizedNotEmpty() -->after'); - $this->assertTrue((bool) $slot->sanitizedNotEmpty()); - $this->assertTrue((bool) $linebreakingSlot->sanitizedNotEmpty('trim')); - $this->assertTrue((bool) $moreComplexSlot->sanitizedNotEmpty()); + $this->assertTrue((bool) $slot->hasActualContent()); + $this->assertTrue((bool) $linebreakingSlot->hasActualContent('trim')); + $this->assertTrue((bool) $moreComplexSlot->hasActualContent()); } public function testComponentSlotIsNotEmpty() @@ -359,11 +359,9 @@ public function testComponentSlotIsNotEmpty() Trulli -->est'); - $this->assertTrue((bool) $slot->sanitizedNotEmpty()); - - $this->assertTrue((bool) $anotherSlot->sanitizedNotEmpty()); - - $this->assertTrue((bool) $moreComplexSlot->sanitizedNotEmpty()); + $this->assertTrue((bool) $slot->hasActualContent()); + $this->assertTrue((bool) $anotherSlot->hasActualContent()); + $this->assertTrue((bool) $moreComplexSlot->hasActualContent()); } }