Skip to content

Commit

Permalink
[11.x] Handle HtmlString constructed with a null (#53367)
Browse files Browse the repository at this point in the history
  • Loading branch information
sperelson authored Nov 1, 2024
1 parent 61e221c commit db76573
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Support/HtmlString.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function toHtml()
*/
public function isEmpty()
{
return $this->html === '';
return ($this->html ?? '') === '';
}

/**
Expand All @@ -62,6 +62,6 @@ public function isNotEmpty()
*/
public function __toString()
{
return $this->toHtml();
return $this->toHtml() ?? '';
}
}
7 changes: 7 additions & 0 deletions tests/Support/SupportHtmlStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,20 @@ public function testToString()
$str = '<h1>foo</h1>';
$html = new HtmlString('<h1>foo</h1>');
$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
{
// 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());

Expand Down

0 comments on commit db76573

Please sign in to comment.