8000 [11.x] Fix `Illuminate\Support\EncodedHtmlString` from causing breaki… · laravel/framework@b5ce211 · GitHub
[go: up one dir, main page]

Skip to content

Commit b5ce211

Browse files
crynoboneshaedrich
andauthored
[11.x] Fix Illuminate\Support\EncodedHtmlString from causing breaking change (#55149)
* wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * Update src/Illuminate/Mail/Markdown.php Co-authored-by: Sebastian Hädrich <11225821+shaedrich@users.noreply.github.com> --------- Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> Co-authored-by: Sebastian Hädrich <11225821+shaedrich@users.noreply.github.com>
1 parent 46f97c4 commit b5ce211

File tree

9 files changed

+98
-16
lines changed

9 files changed

+98
-16
lines changed

src/Illuminate/Mail/Markdown.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,25 @@ public function render($view, array $data = [], $inliner = null)
6969
$contents = $bladeCompiler->usingEchoFormat(
7070
'new \Illuminate\Support\EncodedHtmlString(%s)',
7171
function () use ($view, $data) {
72-
return $this->view->replaceNamespace(
73-
'mail', $this->htmlComponentPaths()
74-
)->make($view, $data)->render();
72+
EncodedHtmlString::encodeUsing(function ($value) {
73+
$replacements = [
74+
'[' => '\[',
75+
'<' => '&lt;',
76+
'>' => '&gt;',
77+
];
78+
79+
return str_replace(array_keys($replacements), array_values($replacements), $value);
80+
});
81+
82+
try {
83+
$contents = $this->view->replaceNamespace(
84+
'mail', $this->htmlComponentPaths()
85+
)->make($view, $data)->render();
86+
} finally {
87+
EncodedHtmlString::flushState();
88+
}
89+
90+
return $contents;
7591
}
7692
);
7793

@@ -84,7 +100,7 @@ function () use ($view, $data) {
84100
}
85101

86102
return new HtmlString(($inliner ?: new CssToInlineStyles)->convert(
87-
$contents, $this->view->make($theme, $data)->render()
103+
str_replace('\[', '[', $contents), $this->view->make($theme, $data)->render()
88104
));
89105
}
90106

@@ -112,10 +128,15 @@ public function renderText($view, array $data = [])
112128
* Parse the given Markdown text into HTML.
113129
*
114130
* @param string $text
131+
* @param bool $encoded
115132
* @return \Illuminate\Support\HtmlString
116133
*/
117-
public static function parse($text)
134+
public static function parse($text, bool $encoded = false)
118135
{
136+
if ($encoded === false) {
137+
return new HtmlString(static::converter()->convert($text)->getContent());
138+
}
139+
119140
EncodedHtmlString::encodeUsing(function ($value) {
120141
$replacements = [
121142
'[' => '\[',

src/Illuminate/Mail/resources/views/html/button.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<table border="0" cellpadding="0" cellspacing="0" role="presentation">
1313
<tr>
1414
<td>
15-
<a href="{{ $url }}" class="button button-{{ $color }}" target="_blank" rel="noopener">{{ $slot }}</a>
15+
<a href="{{ $url }}" class="button button-{{ $color }}" target="_blank" rel="noopener">{!! $slot !!}</a>
1616
</td>
1717
</tr>
1818
</table>

src/Illuminate/Mail/resources/views/html/header.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
@if (trim($slot) === 'Laravel')
66
<img src="https://laravel.com/img/notification-logo.png" class="logo" alt="Laravel Logo">
77
@else
8-
{{ $slot }}
8+
{!! $slot !!}
99
@endif
1010
</a>
1111
</td>

src/Illuminate/Mail/resources/views/html/layout.blade.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
}
2424
}
2525
</style>
26-
{{ $head ?? '' }}
26+
{!! $head ?? '' !!}
2727
</head>
2828
<body>
2929

3030
<table class="wrapper" width="100%" cellpadding="0" cellspacing="0" role="presentation">
3131
<tr>
3232
<td align="center">
3333
<table class="content" width="100%" cellpadding="0" cellspacing="0" role="presentation">
34-
{{ $header ?? '' }}
34+
{!! $header ?? '' !!}
3535

3636
<!-- Email Body -->
3737
<tr>
@@ -40,16 +40,16 @@
4040
<!-- Body content -->
4141
<tr>
4242
<td class="content-cell">
43-
{{ Illuminate\Mail\Markdown::parse($slot) }}
43+
{!! Illuminate\Mail\Markdown::parse($slot) !!}
4444

45-
{{ $subcopy ?? '' }}
45+
{!! $subcopy ?? '' !!}
4646
</td>
4747
</tr>
4848
</table>
4949
</td>
5050
</tr>
5151

52-
{{ $footer ?? '' }}
52+
{!! $footer ?? '' !!}
5353
</table>
5454
</td>
5555
</tr>

src/Illuminate/Mail/resources/views/html/message.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
</x-slot:header>
88

99
{{-- Body --}}
10-
{{ $slot }}
10+
{!! $slot !!}
1111

1212
{{-- Subcopy --}}
1313
@isset($subcopy)
1414
<x-slot:subcopy>
1515
<x-mail::subcopy>
16-
{{ $subcopy }}
16+
{!! $subcopy !!}}
1717
</x-mail::subcopy>
1818
</x-slot:subcopy>
1919
@endisset

src/Illuminate/Mail/resources/views/html/panel.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<table width="100%" cellpadding="0" cellspacing="0" role="presentation">
55
<tr>
66
<td class="panel-item">
7-
{{ Illuminate\Mail\Markdown::parse($slot) }}
7+
{!! Illuminate\Mail\Markdown::parse($slot) !!}
88
</td>
99
</tr>
1010
</table>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@component('mail::message')
2+
*Hi* {{ $user->name }}
3+
4+
@endcomponent

tests/Integration/Mail/MailableTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@
22

33
namespace Illuminate\Tests\Integration\Mail;
44

5+
use Illuminate\Foundation\Auth\User;
6+
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
57
use Illuminate\Mail\Mailable;
68
use Illuminate\Mail\Mailables\Content;
79
use Illuminate\Mail\Mailables\Envelope;
10+
use Orchestra\Testbench\Attributes\WithMigration;
11+
use Orchestra\Testbench\Factories\UserFactory;
812
use Orchestra\Testbench\TestCase;
913
use PHPUnit\Framework\Attributes\DataProvider;
1014

1115
class MailableTest extends TestCase
1216
{
17+
use LazilyRefreshDatabase;
18+
1319
/** {@inheritdoc} */
1420
#[\Override]
1521
protected function defineEnvironment($app)
@@ -69,4 +75,55 @@ public static function markdownEncodedDataProvider()
6975
'My message is: Visit &lt;span&gt;https://laravel.com/docs&lt;/span&gt; to browse the documentation',
7076
];
7177
}
78+
79+
#[WithMigration]
80+
#[DataProvider('markdownEncodedTemplateDataProvider')]
81+
public function testItCanAssertMarkdownEncodedStringUsingTemplate($given, $expected)
82+
{
83+
$user = UserFactory::new()->create([
84+
'name' => $given,
85+
]);
86+
87+
$mailable = new class($user) extends Mailable
88+
{
89+
public $theme = 'taylor';
90+
91+
public function __construct(public User $user)
92+
{
93+
//
94+
}
95+
96+
public function build()
97+
{
98+
return $this->markdown('message-with-template');
99+
}
100+
};
101+
102+
$mailable->assertSeeInHtml($expected, false);
103+
}
104+
105+
public static function markdownEncodedTemplateDataProvider()
106+
{
107+
yield ['[Laravel](https://laravel.com)', '<em>Hi</em> [Laravel](https://laravel.com)'];
108+
109+
yield [
110+
'![Welcome to Laravel](https://laravel.com/assets/img/welcome/background.svg)',
111+
'<em>Hi</em> ![Welcome to Laravel](https://laravel.com/assets/img/welcome/background.svg)',
112+
];
113+
114+
yield [
115+
'Visit https://laravel.com/docs to browse the documentation',
116+
'<em>Hi</em> Visit https://laravel.com/docs to browse the documentation',
117+
];
118+
119+
yield [
120+
'Visit <https://laravel.com/docs> to browse the documentation',
121+
'<em>Hi</em> Visit &lt;https://laravel.com/docs&gt; to browse the documentation',
122+
];
123+
124+
yield [
125+
'Visit <span>https://laravel.com/docs</span> to browse the documentation',
126+
'<em>Hi</em> Visit &lt;span&gt;https://laravel.com/docs&lt;/span&gt; to browse the documentation',
127+
];
128+
}
72129
}

tests/Integration/Mail/MarkdownParserTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function testItCanParseMarkdownString($given, $expected)
2424
#[DataProvider('markdownEncodedDataProvider')]
2525
public function testItCanParseMarkdownEncodedString($given, $expected)
2626
{
27-
tap(Markdown::parse($given), function ($html) use ($expected) {
27+
tap(Markdown::parse($given, encoded: true), function ($html) use ($expected) {
2828
$this->assertInstanceOf(HtmlString::class, $html);
2929

3030
$this->assertStringEqualsStringIgnoringLineEndings($expected.PHP_EOL, (string) $html);

0 commit comments

Comments
 (0)
0