From 7d0d92aed69da7025a01c62fa632a5f5333175b6 Mon Sep 17 00:00:00 2001 From: Nicky Gerritsen Date: Tue, 17 Nov 2026 10:45:25 +0100 Subject: [PATCH] Fix: escape attribute values --- src/Component/BodyComponent.php | 4 ++++ tests/Integration/Component/MjImageTest.php | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/Component/BodyComponent.php b/src/Component/BodyComponent.php index 2ceadcb..747225e 100644 --- a/src/Component/BodyComponent.php +++ b/src/Component/BodyComponent.php @@ -70,6 +70,10 @@ protected function htmlAttributes(array $attributes): string } } + // The XML parser decodes " to a bare quote, which would end the attribute early. + // Other characters stay as MJML writes them, so placeholders like <% x %> keep working. + $value = str_replace('"', '"', $value); + $parts[] = "{$name}=\"{$value}\""; } diff --git a/tests/Integration/Component/MjImageTest.php b/tests/Integration/Component/MjImageTest.php index 072f7ca..86443f7 100644 --- a/tests/Integration/Component/MjImageTest.php +++ b/tests/Integration/Component/MjImageTest.php @@ -96,4 +96,24 @@ public function testSrcsetAndSizesAttributesArePassedThrough(): void $this->extractAttribute($html, 'img', 'sizes'), ); } + + public function testQuoteInAttributeValueStaysInsideTheAttribute(): void + { + $mjml = ' + + + + + + + + + '; + + $html = $this->renderMjml($mjml); + + self::assertStringContainsString('alt="The "Acme" logo"', $html); + self::assertSame('The "Acme" logo', $this->extractAttribute($html, 'img', 'alt')); + self::assertNull($this->extractAttribute($html, 'img', 'acme"')); + } }