8000 [11.x] Fix `Illuminate\Support\EncodedHtmlString` from causing breaking change by crynobone · Pull Request #55149 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[11.x] Fix Illuminate\Support\EncodedHtmlString from causing breaking change #55149

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
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
wip
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone committed Mar 24, 2025
commit a6e26ef6f4a968e3926af580a15cdb564a257587
26 changes: 22 additions & 4 deletions src/Illuminate/Mail/Markdown.php
8000
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,27 @@ public function render($view, array $data = [], $inliner = null)
$contents = $bladeCompiler->usingEchoFormat(
'new \Illuminate\Support\EncodedHtmlString(%s)',
function () use ($view, $data) {
return $this->view->replaceNamespace(
'mail', $this->htmlComponentPaths()
)->make($view, $data)->render();
EncodedHtmlString::encodeUsing(function ($value) {
$replacements = [
'[' => '\[',
'<' => '&lt;',
'>' => '&gt;',
];

$html = str_replace(array_keys($replacements), array_values($replacements), $value);

return $html;
});

try {
$contents = $this->view->replaceNamespace(
'mail', $this->htmlComponentPaths()
)->make($view, $data)->render();
} finally {
EncodedHtmlString::flushState();
}

return $contents;
}
);

Expand All @@ -84,7 +102,7 @@ function () use ($view, $data) {
}

return new HtmlString(($inliner ?: new CssToInlineStyles)->convert(
$contents, $this->view->make($theme, $data)->render()
str_replace('\[', '[', $contents), $this->view->make($theme, $data)->render()
));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Mail/resources/views/html/footer.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<table class="footer" align="center" width="570" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td class="content-cell" align="center">
{{ Illuminate\Mail\Markdown::parse($slot) }}
{!! Illuminate\Mail\Markdown::parse($slot) !!}
</td>
</tr>
</table>
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Mail/resources/views/html/layout.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
}
}
</style>
{{ $head ?? '' }}
{!! $head ?? '' !!}
</head>
<body>

Expand All @@ -40,7 +40,7 @@
<!-- Body content -->
<tr>
<td class="content-cell">
{{ Illuminate\Mail\Markdown::parse($slot) }}
{!! Illuminate\Mail\Markdown::parse($slot) !!}

{!! $subcopy ?? '' !!}
</td>
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Mail/resources/views/html/message.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@isset($subcopy)
<x-slot:subcopy>
<x-mail::subcopy>
{!! $subcopy !!}
{!! $subcopy !!}}
</x-mail::subcopy>
</x-slot:subcopy>
@endisset
Expand Down
10000 2 changes: 1 addition & 1 deletion src/Illuminate/Mail/resources/views/html/subcopy.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<table class="subcopy" width="100%" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td>
{{ Illuminate\Mail\Markdown::parse($slot) }}
{!! Illuminate\Mail\Markdown::parse($slot) !!}
</td>
</tr>
</table>
2 changes: 1 addition & 1 deletion src/Illuminate/Mail/resources/views/html/table.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div class="table">
{{ Illuminate\Mail\Markdown::parse($slot) }}
{!! Illuminate\Mail\Markdown::parse($slot) !!}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@component('mail::message')
*Hi* {{ $user->name }}

@endcomponent
57 changes: 57 additions & 0 deletions tests/Integration/Mail/MailableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

namespace Illuminate\Tests\Integration\Mail;

use Illuminate\Foundation\Auth\User;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Orchestra\Testbench\Attributes\WithMigration;
use Orchestra\Testbench\Factories\UserFactory;
use Orchestra\Testbench\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;

class MailableTest extends TestCase
{
use LazilyRefreshDatabase;

/** {@inheritdoc} */
#[\Override]
protected function defineEnvironment($app)
Expand Down Expand Up @@ -69,4 +75,55 @@ public static function markdownEncodedDataProvider()
'My message is: Visit &lt;span&gt;https://laravel.com/docs&lt;/span&gt; to browse the documentation',
];
}

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

$mailable = new class($user) extends Mailable
{
public function __construct(public User $user)
{
//
}

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

$mailable->assertSeeInHtml($expected, false);
}

public static function markdownEncodedTemplateDataProvider()
{
$hi = '<em style="box-sizing: border-box; font-family: -apple-system, BlinkMacSystemFont, \'Segoe UI\', Roboto, Helvetica, Arial, sans-serif, \'Apple Color Emoji\', \'Segoe UI Emoji\', \'Segoe UI Symbol\'; position: relative;">Hi</em>';

yield ['[Laravel](https://laravel.com)', "{$hi} [Laravel](https://laravel.com)"];

yield [
'![Welcome to Laravel](https://laravel.com/assets/img/welcome/background.svg)',
"{$hi} ![Welcome to Laravel](https://laravel.com/assets/img/welcome/background.svg)",
];

yield [
'Visit https://laravel.com/docs to browse the documentation',
"{$hi} Visit https://laravel.com/docs to browse the documentation",
];

yield [
'Visit <https://laravel.com/docs> to browse the documentation',
"{$hi} Visit &lt;https://laravel.com/docs&gt; to browse the documentation",
];

yield [
'Visit <span>https://laravel.com/docs</span> to browse the documentation',
"{$hi} Visit &lt;span&gt;https://laravel.com/docs&lt;/span&gt; to browse the documentation",
];
}
}
Loading
0