Skip to content

Commit db76573

Browse files
authored
[11.x] Handle HtmlString constructed with a null (#53367)
1 parent 61e221c commit db76573

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/Illuminate/Support/HtmlString.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function toHtml()
4242
*/
4343
public function isEmpty()
4444
{
45-
return $this->html === '';
45+
return ($this->html ?? '') === '';
4646
}
4747

4848
/**
@@ -62,6 +62,6 @@ public function isNotEmpty()
6262
*/
6363
public function __toString()
6464
{
65-
return $this->toHtml();
65+
return $this->toHtml() ?? '';
6666
}
6767
}

tests/Support/SupportHtmlStringTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,20 @@ public function testToString()
3939
$str = '<h1>foo</h1>';
4040
$html = new HtmlString('<h1>foo</h1>');
4141
$this->assertEquals($str, (string) $html);
42+
43+
// Check if HtmlString gracefully handles a null value
44+
$html = new HtmlString(null);
45+
$this->assertIsString((string) $html);
4246
}
4347

4448
public function testIsEmpty(): void
4549
{
4650
// Check if HtmlString correctly identifies an empty string as empty
4751
$this->assertTrue((new HtmlString(''))->isEmpty());
4852

53+
// Check if HtmlString identifies a null value as empty
54+
$this->assertTrue((new HtmlString(null))->isEmpty());
55+
4956
// HtmlString with whitespace should not be considered as empty
5057
$this->assertFalse((new HtmlString(' '))->isEmpty());
5158

0 commit comments

Comments
 (0)