Skip to content

[11.x] Fix EncodedHtmlString to ignore instance of HtmlString #55543

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
merged 8 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Illuminate/Mail/resources/views/html/panel.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<table width="100%" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td class="panel-item">
{!! Illuminate\Mail\Markdown::parse($slot) !!}
{{ Illuminate\Mail\Markdown::parse($slot) }}
</td>
</tr>
</table>
Expand Down
29 changes: 27 additions & 2 deletions src/Illuminate/Support/EncodedHtmlString.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@

namespace Illuminate\Support;

use BackedEnum;
use Illuminate\Contracts\Support\DeferringDisplayableValue;
use Illuminate\Contracts\Support\Htmlable;

class EncodedHtmlString extends HtmlString
{
/**
* The HTML string.
*
* @var \Illuminate\Contracts\Support\DeferringDisplayableValue|\Illuminate\Contracts\Support\Htmlable|\BackedEnum|string|int|float|null
*/
protected $html;

/**
* The callback that should be used to encode the HTML strings.
*
Expand All @@ -14,7 +25,7 @@ class EncodedHtmlString extends HtmlString
/**
* Create a new encoded HTML string instance.
*
* @param string $html
* @param \Illuminate\Contracts\Support\DeferringDisplayableValue|\Illuminate\Contracts\Support\Htmlable|\BackedEnum|string|int|float|null $html
* @param bool $doubleEncode
* @return void
*/
Expand Down Expand Up @@ -48,9 +59,23 @@ public static function convert($value, bool $withQuote = true, bool $doubleEncod
#[\Override]
public function toHtml()
{
$value = $this->html;

if ($value instanceof DeferringDisplayableValue) {
$value = $value->resolveDisplayableValue();
}

if ($value instanceof Htmlable) {
return $value->toHtml();
}

if ($value instanceof BackedEnum) {
$value = $value->value;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy the logic from e() function

function e($value, $doubleEncode = true)
{
if ($value instanceof DeferringDisplayableValue) {
$value = $value->resolveDisplayableValue();
}
if ($value instanceof Htmlable) {
return $value->toHtml();
}
if ($value instanceof BackedEnum) {
$value = $value->value;
}
return htmlspecialchars($value ?? '', ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', $doubleEncode);
}


return (static::$encodeUsingFactory ?? function ($value, $doubleEncode) {
return static::convert($value, doubleEncode: $doubleEncode);
})($this->html, $this->doubleEncode);
})($value, $this->doubleEncode);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/Integration/Mail/Fixtures/table-with-template.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<x-mail::message subcopy="This is a subcopy">

<x-mail::table>
*Hi* {{ $user->name }}

| Laravel | Table | Example |
| ------------- | :-----------: | ------------: |
| Col 2 is | Centered | $10 |
| Col 3 is | Right-Aligned | $20 |
</x-mail::table>

</x-mail::message>
50 changes: 50 additions & 0 deletions tests/Integration/Mail/MailableWithSecuredEncodingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,56 @@ public function build()
$mailable->assertSeeInHtml($expected, false);
}

#[WithMigration]
#[DataProvider('markdownEncodedTemplateDataProvider')]
public function testItCanAssertMarkdownEncodedStringUsingTemplateWithTable($given, $expected)
{
$user = UserFactory::new()->create([
'name' => $given,
]);

$mailable = new class($user) extends Mailable
{
public $theme = 'taylor';

public function __construct(public User $user)
{
//
}

public function build()
{
return $this->markdown('table-with-template');
}
};

$mailable->assertSeeInHtml($expected, false);
$mailable->assertSeeInHtml('<p>This is a subcopy</p>', false);
$mailable->assertSeeInHtml(<<<'TABLE'
<table>
<thead>
<tr>
<th>Laravel</th>
<th align="center">Table</th>
<th align="right">Example</th>
</tr>
</thead>
<tbody>
<tr>
<td>Col 2 is</td>
<td align="center">Centered</td>
<td align="right">$10</td>
</tr>
<tr>
<td>Col 3 is</td>
<td align="center">Right-Aligned</td>
<td align="right">$20</td>
</tr>
</tbody>
</table>
TABLE, false);
}

public static function markdownEncodedTemplateDataProvider()
{
yield ['[Laravel](https://laravel.com)', '<em>Hi</em> [Laravel](https://laravel.com)'];
Expand Down
50 changes: 50 additions & 0 deletions tests/Integration/Mail/MailableWithoutSecuredEncodingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,56 @@ public function build()
$mailable->assertSeeInHtml($expected, false);
}

#[WithMigration]
#[DataProvider('markdownEncodedTemplateDataProvider')]
public function testItCanAssertMarkdownEncodedStringUsingTemplateWithTable($given, $expected)
{
$user = UserFactory::new()->create([
'name' => $given,
]);

$mailable = new class($user) extends Mailable
{
public $theme = 'taylor';

public function __construct(public User $user)
{
//
}

public function build()
{
return $this->markdown('table-with-template');
}
};

$mailable->assertSeeInHtml($expected, false);
$mailable->assertSeeInHtml('<p>This is a subcopy</p>', false);
$mailable->assertSeeInHtml(<<<'TABLE'
<table>
<thead>
<tr>
<th>Laravel</th>
<th align="center">Table</th>
<th align="right">Example</th>
</tr>
</thead>
<tbody>
<tr>
<td>Col 2 is</td>
<td align="center">Centered</td>
<td align="right">$10</td>
</tr>
<tr>
<td>Col 3 is</td>
<td align="center">Right-Aligned</td>
<td align="right">$20</td>
</tr>
</tbody>
</table>
TABLE, false);
}

public static function markdownEncodedTemplateDataProvider()
{
yield ['[Laravel](https://laravel.com)', '<p><em>Hi</em> <a href="https://laravel.com">Laravel</a></p>'];
Expand Down
5 changes: 5 additions & 0 deletions tests/Integration/Mail/MarkdownParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public static function markdownEncodedDataProvider()
'<p>Visit &lt;span&gt;https://laravel.com/docs&lt;/span&gt; to browse the documentation</p>',
];

yield [
new EncodedHtmlString(new HtmlString('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 [
'![Welcome to Laravel](https://laravel.com/assets/img/welcome/background.svg)<br />'.new EncodedHtmlString('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 &lt;span&gt;https://laravel.com/docs&lt;/span&gt; to browse the documentation</p>',
Expand Down