Skip to content

Commit 8b63611

Browse files
[8.x] Add htmlString helper method (#40247)
* Add `htmlString` helper method * rename `htmlString` method to `html` * test: added test * fix: Add FQN * fix: codestyle * formatting' Co-authored-by: Taylor Otwell <[email protected]>
1 parent ad24bc3 commit 8b63611

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/Illuminate/Support/Stringable.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,16 @@ public function wordCount()
821821
return str_word_count($this->value);
822822
}
823823

824+
/**
825+
* Convert the string into a `HtmlString` instance.
826+
*
827+
* @return \Illuminate\Support\HtmlString
828+
*/
829+
public function toHtmlString()
830+
{
831+
return new HtmlString($this->value);
832+
}
833+
824834
/**
825835
* Dump the string.
826836
*

tests/Support/SupportStringableTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Tests\Support;
44

55
use Illuminate\Support\Collection;
6+
use Illuminate\Support\HtmlString;
67
use Illuminate\Support\Stringable;
78
use PHPUnit\Framework\TestCase;
89

@@ -722,6 +723,14 @@ public function testWordCount()
722723
$this->assertEquals(10, $this->stringable('Hi, this is my first contribution to the Laravel framework.')->wordCount());
723724
}
724725

726+
public function testToHtmlString()
727+
{
728+
$this->assertEquals(
729+
new HtmlString('<h1>Test String</h1>'),
730+
$this->stringable('<h1>Test String</h1>')->toHtmlString()
731+
);
732+
}
733+
725734
public function testStripTags()
726735
{
727736
$this->assertSame('beforeafter', (string) $this->stringable('before<br>after')->stripTags());

0 commit comments

Comments
 (0)