From db76573f6a63384215679bfa7101102bbb8caecb Mon Sep 17 00:00:00 2001 From: Stephen Perelson Date: Fri, 1 Nov 2024 15:34:42 +0200 Subject: [PATCH] [11.x] Handle HtmlString constructed with a null (#53367) --- src/Illuminate/Support/HtmlString.php | 4 ++-- tests/Support/SupportHtmlStringTest.php | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Support/HtmlString.php b/src/Illuminate/Support/HtmlString.php index 624e7f3efa53..a25311132579 100644 --- a/src/Illuminate/Support/HtmlString.php +++ b/src/Illuminate/Support/HtmlString.php @@ -42,7 +42,7 @@ public function toHtml() */ public function isEmpty() { - return $this->html === ''; + return ($this->html ?? '') === ''; } /** @@ -62,6 +62,6 @@ public function isNotEmpty() */ public function __toString() { - return $this->toHtml(); + return $this->toHtml() ?? ''; } } diff --git a/tests/Support/SupportHtmlStringTest.php b/tests/Support/SupportHtmlStringTest.php index 1c3bb0fa313e..f349a8a1ad2a 100644 --- a/tests/Support/SupportHtmlStringTest.php +++ b/tests/Support/SupportHtmlStringTest.php @@ -39,6 +39,10 @@ public function testToString() $str = '

foo

'; $html = new HtmlString('

foo

'); $this->assertEquals($str, (string) $html); + + // Check if HtmlString gracefully handles a null value + $html = new HtmlString(null); + $this->assertIsString((string) $html); } public function testIsEmpty(): void @@ -46,6 +50,9 @@ public function testIsEmpty(): void // Check if HtmlString correctly identifies an empty string as empty $this->assertTrue((new HtmlString(''))->isEmpty()); + // Check if HtmlString identifies a null value as empty + $this->assertTrue((new HtmlString(null))->isEmpty()); + // HtmlString with whitespace should not be considered as empty $this->assertFalse((new HtmlString(' '))->isEmpty());