-
Notifications
You must be signed in to change notification settings - Fork 11.4k
Add Illuminate\Support\EncodedHtmlString
#54737
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+329
−11
Merged
Changes from 28 commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
1744ba5
wip
crynobone 37a4a89
wip
crynobone ba5c5b0
wip
crynobone 98b52b1
wip
crynobone f2317c3
wip
crynobone 6aa1f01
wip
crynobone e54d8a0
wip
crynobone ca869ce
wip
crynobone f76996f
wip
crynobone 208256f
wip
crynobone cdca973
wip
crynobone d405551
wip
crynobone 1f0889f
wip
crynobone bbd61dc
wip
crynobone 1859539
Apply fixes from StyleCI
StyleCIBot e1b4315
wip
crynobone b3a8891
Merge remote-tracking branch 'upstream/markdown-string' into markdown…
crynobone dc4e4ba
wip
crynobone 4dfa33c
wip
crynobone 93d8418
wip
crynobone cd647d1
Apply fixes from StyleCI
StyleCIBot 4bf64bb
wip
crynobone cd5b117
Merge remote-tracking branch 'upstream/markdown-string' into markdown…
crynobone ab43534
wip
crynobone 823571a
Update EncodedHtmlString.php
crynobone 1c578fe
wip
crynobone 8b9fe3e
wip
crynobone 20b8503
wip
crynobone 7eac843
wip
crynobone 9fd2d7e
Apply fixes from StyleCI
StyleCIBot 18cb73d
Update EncodedHtmlString.php
crynobone 23e9df0
Update Markdown.php
crynobone 87abdeb
Update src/Illuminate/Mail/Markdown.php
crynobone 6138315
wip
crynobone a68cc34
Apply fixes from StyleCI
StyleCIBot 969b82c
wip
crynobone 26b21bd
Merge remote-tracking branch 'upstream/markdown-string' into markdown…
crynobone cfeeafb
wip
crynobone 38db599
wip
crynobone 35d8b50
wip
crynobone 0e68768
wip
crynobone 72f0075
wip
crynobone 2b4d4b4
wip
crynobone 72a85b3
wip
crynobone 5b4f029
formatting
taylorotwell 7f84895
formatting
taylorotwell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace Illuminate\Support; | ||
|
||
class EncodedHtmlString extends HtmlString | ||
{ | ||
/** | ||
* The callback that should be used to encode the html strings. | ||
* | ||
* @var callable|null | ||
*/ | ||
protected static $encodeUsingFactory; | ||
|
||
/** | ||
* Create a new Encoded HTML string instance. | ||
* | ||
* @param string $html | ||
* @param bool $doubleEncode | ||
* @return void | ||
*/ | ||
public function __construct($html = '', protected bool $doubleEncode = true) | ||
{ | ||
parent::__construct($html); | ||
} | ||
|
||
/** | ||
* Convert using default encoding. | ||
* | ||
* @param string|null $value | ||
* @param int $flag | ||
crynobone marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @param string $encoding | ||
crynobone marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @param bool $doubleEncode | ||
* @return string | ||
*/ | ||
public static function convert($value, int $flag = ENT_QUOTES | ENT_SUBSTITUTE, string $encoding = 'UTF-8', bool $doubleEncode = true) | ||
{ | ||
return htmlspecialchars($value ?? '', $flag, $encoding, $doubleEncode); | ||
} | ||
|
||
/** | ||
* Get the HTML string. | ||
* | ||
* @return string | ||
*/ | ||
#[\Override] | ||
public function toHtml() | ||
{ | ||
return (static::$encodeUsingFactory ?? function ($value, bool $doubleEncode) { | ||
return static::convert($value, doubleEncode: $doubleEncode); | ||
})($this->html, $this->doubleEncode); | ||
} | ||
|
||
/** | ||
* Set the callable that will be used to encode the html strings. | ||
* | ||
* @param callable|null $factory | ||
* @return void | ||
*/ | ||
public static function encodeUsing(?callable $factory = null) | ||
{ | ||
static::$encodeUsingFactory = $factory; | ||
} | ||
|
||
/** | ||
* Flush the class's global state. | ||
* | ||
* @return void | ||
*/ | ||
public static function flushState() | ||
{ | ||
static::$encodeUsingFactory = null; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Mail; | ||
|
||
use Illuminate\Mail\Markdown; | ||
use Illuminate\Support\HtmlString; | ||
use Orchestra\Testbench\TestCase; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
|
||
class MarkdownParserTest extends TestCase | ||
{ | ||
#[DataProvider('markdownDataProvider')] | ||
public function testItCanParseMarkdownString($given, $expected) | ||
{ | ||
tap(Markdown::parse($given), function ($html) use ($expected) { | ||
$this->assertInstanceOf(HtmlString::class, $html); | ||
|
||
$this->assertStringEqualsStringIgnoringLineEndings($expected.PHP_EOL, (string) $html); | ||
$this->assertSame((string) $html, (string) $html->toHtml()); | ||
}); | ||
} | ||
|
||
#[DataProvider('markdownEncodedDataProvider')] | ||
public function testItCanParseMarkdownEncodedString($given, $expected) | ||
{ | ||
tap(Markdown::parse($given), function ($html) use ($expected) { | ||
$this->assertInstanceOf(HtmlString::class, $html); | ||
|
||
$this->assertStringEqualsStringIgnoringLineEndings($expected.PHP_EOL, (string) $html); | ||
}); | ||
} | ||
|
||
public static function markdownDataProvider() | ||
{ | ||
yield ['[Laravel](https://laravel.com)', '<p><a href="https://laravel.com">Laravel</a></p>']; | ||
yield ['\[Laravel](https://laravel.com)', '<p>[Laravel](https://laravel.com)</p>']; | ||
yield ['', '<p><img src="https://laravel.com/assets/img/welcome/background.svg" alt="Welcome to Laravel" /></p>']; | ||
yield ['!\[Welcome to Laravel](https://laravel.com/assets/img/welcome/background.svg)', '<p></p>']; | ||
yield ['Visit https://laravel.com/docs to browse the documentation', '<p>Visit https://laravel.com/docs to browse the documentation</p>']; | ||
yield ['Visit <https://laravel.com/docs> to browse the documentation', '<p>Visit <a href="https://laravel.com/docs">https://laravel.com/docs</a> to browse the documentation</p>']; | ||
yield ['Visit <span>https://laravel.com/docs</span> to browse the documentation', '<p>Visit <span>https://laravel.com/docs</span> to browse the documentation</p>']; | ||
} | ||
|
||
public static function markdownEncodedDataProvider() | ||
{ | ||
yield [e('[Laravel](https://laravel.com)'), '<p>[Laravel](https://laravel.com)</p>']; | ||
|
||
yield [ | ||
e(''), | ||
'<p></p>', | ||
]; | ||
|
||
yield [ | ||
e('Visit https://laravel.com/docs to browse the documentation'), | ||
'<p>Visit https://laravel.com/docs to browse the documentation</p>', | ||
]; | ||
|
||
yield [ | ||
e('Visit <https://laravel.com/docs> to browse the documentation'), | ||
'<p>Visit <https://laravel.com/docs> to browse the documentation</p>', | ||
]; | ||
|
||
yield [ | ||
e('Visit <span>https://laravel.com/docs</span> to browse the documentation'), | ||
'<p>Visit <span>https://laravel.com/docs</span> to browse the documentation</p>', | ||
]; | ||
|
||
yield [ | ||
'<br />'.e('Visit <span>https://laravel.com/docs</span> to browse the documentation'), | ||
'<p><img src="https://laravel.com/assets/img/welcome/background.svg" alt="Welcome to Laravel" /><br />Visit <span>https://laravel.com/docs</span> to browse the documentation</p>', | ||
]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.