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 fr 8000 om
Mar 24, 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
31 changes: 26 additions & 5 deletions src/Illuminate/Mail/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,25 @@ 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;',
];

return str_replace(array_keys($replacements), array_values($replacements), $value);
});

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

return $contents;
}
);

Expand All @@ -84,7 +100,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 Expand Up @@ -112,10 +128,15 @@ public function renderText($view, array $data = [])
* Parse the given Markdown text into HTML.
*
* @param string $text
* @param bool $encoded
* @return \Illuminate\Support\HtmlString
*/
public static function parse($text)
public static function parse($text, bool $encoded = false)
{
if ($encoded === false) {
return new HtmlString(static::converter()->convert($text)->getContent());
}

EncodedHtmlString::encodeUsing(function ($value) {
$replacements = [
'[' => '\[',
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Mail/resources/views/html/button.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<table border="0" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td>
<a href="{{ $url }}" class="button button-{{ $color }}" target="_blank" rel="noopener">{{ $slot }}</a>
<a href="{{ $url }}" class="button button-{{ $color }}" target="_blank" rel="noopener">{!! $slot !!}</a>
</td>
</tr>
</table>
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Mail/resources/views/html/header.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@if (trim($slot) === 'Laravel')
<img src="https://laravel.com/img/notification-logo.png" class="logo" alt="Laravel Logo">
@else
{{ $slot }}
{!! $slot !!}
@endif
</a>
</td>
Expand Down
10 changes: 5 additions & 5 deletions src/Illuminate/Mail/resources/views/html/layout.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
}
}
</style>
{{ $head ?? '' }}
{!! $head ?? '' !!}
</head>
<body>

<table class="wrapper" width="100%" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td align="center">
<table class="content" width="100%" cellpadding="0" cellspacing="0" role="presentation">
{{ $header ?? '' }}
{!! $header ?? '' !!}

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

{{ $subcopy ?? '' }}
{!! $subcopy ?? '' !!}
</td>
</tr>
</table>
</td>
</tr>

{{ $footer ?? '' }}
{!! $footer ?? '' !!}
</table>
</td>
</tr>
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Mail/resources/views/html/message.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
</x-slot:header>

{{-- Body --}}
{{ $slot }}
{!! $slot !!}

{{-- Subcopy --}}
@isset($subcopy)
<x-slot:subcopy>
<x-mail::subcopy>
{{ $subcopy }}
{!! $subcopy !!}}
</x-mail::subcopy>
</x-slot:subcopy>
@endisset
Expand Down
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
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 $theme = 'taylor';

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

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

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

public static function markdownEncodedTemplateDataProvider()
{
yield ['[Laravel](https://laravel.com)', '<em>Hi</em> [Laravel](https://laravel.com)'];

yield [
'![Welcome to Laravel](https://laravel.com/assets/img/welcome/background.svg)',
'<em>Hi</em> ![Welcome to Laravel](https://laravel.com/assets/img/welcome/background.svg)',
];

yield [
'Visit https://laravel.com/docs to browse the documentation',
'<em>Hi</em> Visit https://laravel.com/docs to browse the documentation',
];

yield [
'Visit <https://laravel.com/docs> to browse the documentation',
'<em>Hi</em> Visit &lt;https://laravel.com/docs&gt; to browse the documentation',
];

yield [
'Visit <span>https://laravel.com/docs</span> to browse the documentation',
'<em>Hi</em> Visit &lt;span&gt;https://laravel.com/docs&lt;/span&gt; to browse the documentation',
];
}
}
2 changes: 1 addition & 1 deletion tests/Integration/Mail/MarkdownParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testItCanParseMarkdownString($given, $expected)
#[DataProvider('markdownEncodedDataProvider')]
public function testItCanParseMarkdownEncodedString($given, $expected)
{
tap(Markdown::parse($given), function ($html) use ($expected) {
tap(Markdown::parse($given, encoded: true), function ($html) use ($expected) {
$this->assertInstanceOf(HtmlString::class, $html);

$this->assertStringEqualsStringIgnoringLineEndings($expected.PHP_EOL, (string) $html);
Expand Down
Loading
0