Beautiful Open-Source Form Builder
Fix email content disappearing with large base64 images (#1389)
Julien Nahumd4957dd20090
diff --git a/api/app/Notifications/Forms/FormEmailNotification.php b/api/app/Notifications/Forms/FormEmailNotification.phpindex 4a935b243..40386d53a 100644--- a/api/app/Notifications/Forms/FormEmailNotification.php+++ b/api/app/Notifications/Forms/FormEmailNotification.php@@ -461,35 +461,62 @@ class FormEmailNotification extends Notification'ql-resize-style-full' => 'width:100% !important;',];- foreach ($alignments as $class => $inlineStyle) {- $html = preg_replace_callback(- '/<([a-z]+)([^>]*\bclass\s*=\s*["\']([^"\']*' . preg_quote($class, '/') . '[^"\']*)["\'])([^>]*)>/i',- function ($m) use ($class, $inlineStyle) {- $tagName = $m[1];- $beforeClass = $m[2];- $classes = $m[3];- $after = $m[4];-- $newClasses = preg_replace('/\s*' . preg_quote($class, '/') . '\s*/', ' ', $classes);- $newClasses = trim(preg_replace('/\s+/', ' ', $newClasses));-- $style = $inlineStyle;- if (preg_match('/style\s*=\s*["\']([^"\']*)["\']/', $beforeClass . $after, $sm)) {- $style = $inlineStyle . $sm[1];- $beforeClass = preg_replace('/\s*style\s*=\s*["\'][^"\']*["\']/', '', $beforeClass);- $after = preg_replace('/\s*style\s*=\s*["\'][^"\']*["\']/', '', $after);+ // Match tags once, consuming large quoted attributes without backtracking.+ return preg_replace_callback(+ '/<([a-z]+)\b((?:[^>"\']++|"[^"]*+"|\'[^\']*+\')*+)>/i',+ function ($match) use ($alignments) {+ $attributes = $match[2];+ preg_match_all(+ '/\s+([a-z_:][a-z0-9_.:-]*)\s*=\s*(?:"([^"]*+)"|\'([^\']*+)\'|([^\s>]+))/i',+ $attributes,+ $attributeMatches,+ PREG_SET_ORDER | PREG_OFFSET_CAPTURE | PREG_UNMATCHED_AS_NULL+ );+ $parsedAttributes = [];+ foreach ($attributeMatches as $attribute) {+ $parsedAttributes[strtolower($attribute[1][0])] ??= [+ 'value' => $attribute[2][0] ?? $attribute[3][0] ?? $attribute[4][0],+ 'offset' => $attribute[0][1],+ 'length' => strlen($attribute[0][0]),+ ];+ }+ $classAttribute = $parsedAttributes['class'] ?? null;+ if (!$classAttribute) {+ return $match[0];+ }++ $classes = preg_split('/\s+/', trim($classAttribute['value']));+ $style = '';+ foreach ($alignments as $class => $inlineStyle) {+ if (in_array($class, $classes, true)) {+ $classes = array_values(array_diff($classes, [$class]));+ $style = $inlineStyle . $style;}+ }++ if ($style === '') {+ return $match[0];+ }- $beforeClass = preg_replace('/\s*class\s*=\s*["\'][^"\']*["\']/', '', $beforeClass);- $classAttr = $newClasses !== '' ? ' class="' . $newClasses . '"' : '';+ $replacements = [[+ 'offset' => $classAttribute['offset'],+ 'length' => $classAttribute['length'],+ 'value' => $classes ? ' class="' . e(implode(' ', $classes), false) . '"' : '',+ ]];+ if ($styleAttribute = $parsedAttributes['style'] ?? null) {+ $style .= $styleAttribute['value'];+ $replacements[] = [...$styleAttribute, 'value' => ''];+ }- return '<' . $tagName . $beforeClass . $classAttr . ' style="' . $style . '"' . $after . '>';- },- $html- );- }+ usort($replacements, fn ($left, $right) => $right['offset'] <=> $left['offset']);+ foreach ($replacements as $replacement) {+ $attributes = substr_replace($attributes, $replacement['value'], $replacement['offset'], $replacement['length']);+ }- return $html;+ return '<' . $match[1] . $attributes . ' style="' . e($style, false) . '">';+ },+ $html+ ) ?? $html;}private function getEncodedSubmissionId(): ?stringdiff --git a/api/tests/Feature/Integrations/Email/EmailIntegrationTest.php b/api/tests/Feature/Integrations/Email/EmailIntegrationTest.phpindex dfcb11aef..d0eb5a852 100644--- a/api/tests/Feature/Integrations/Email/EmailIntegrationTest.php+++ b/api/tests/Feature/Integrations/Email/EmailIntegrationTest.php@@ -488,6 +488,64 @@ test('email notification renders plain submission values once in the submissionexpect($html)->not->toContain("Café d'Art & <b>test</b>");});+test('email notification preserves content around a large base64 image', function (string $alignment) {+ $user = $this->actingAsUser();+ $workspace = $this->createUserWorkspace($user);+ $form = $this->createForm($user, $workspace);+ $imageUrl = 'data:image/jpeg;base64,' . str_repeat('A', 538000);+ $class = $alignment === '' ? '' : ' class="retained-class ql-resize-style-' . $alignment . '"';++ $notification = new FormEmailNotification(+ new \App\Events\Forms\FormSubmitted($form, []),+ (object) [+ 'sender_name' => 'Test Sender',+ 'subject' => 'Test Subject',+ 'email_content' => '<p>Before the large image</p><p><img src="' . $imageUrl . '"' . $class . ' style="max-width:100%;"></p><p>After the large image</p>',+ 'include_submission_data' => false,+ ]+ );++ $html = (string) $notification->toMail(new AnonymousNotifiable())->render();++ expect($html)->toContain('Before the large image', 'After the large image', $imageUrl);+ if ($alignment !== '') {+ $styles = ['left' => 'float: left', 'right' => 'float: right', 'center' => 'text-align: center', 'full' => 'width: 100%'];+ expect($html)->toContain($styles[$alignment], 'retained-class', 'max-width: 100%');+ expect($html)->not->toContain('ql-resize-style-' . $alignment);+ }+})->with(['unaligned' => '', 'left', 'right', 'center', 'full']);++test('email image alignment preserves quoted attribute values and existing styles', function (bool $styleFirst) {+ $user = $this->actingAsUser();+ $workspace = $this->createUserWorkspace($user);+ $form = $this->createForm($user, $workspace);+ $style = ' style="max-width:100%; font-family: \'Open Sans\';"';+ $class = ' class="retained-class ql-resize-style-right"';+ $notification = new FormEmailNotification(+ new \App\Events\Forms\FormSubmitted($form, []),+ (object) [+ 'sender_name' => 'Test Sender',+ 'subject' => 'Test Subject',+ 'email_content' => '<p><img alt="Example class=\'example\'" title="Example style=\'color:red;\'"'+ . ($styleFirst ? $style . $class : $class . $style)+ . ' src="data:image/jpeg;base64,AAAA"></p>',+ 'include_submission_data' => false,+ ]+ );++ $html = (string) $notification->toMail(new AnonymousNotifiable())->render();+ $document = new \DOMDocument();+ @$document->loadHTML($html);+ $image = $document->getElementsByTagName('img')->item(0);++ expect($image)->not->toBeNull();+ expect($image->getAttribute('alt'))->toBe("Example class='example'");+ expect($image->getAttribute('title'))->toBe("Example style='color:red;'");+ expect($image->getAttribute('class'))->toBe('retained-class');+ expect($image->getAttribute('style'))->toContain('float: right', 'max-width: 100%', 'Open Sans');+ expect($image->getAttribute('style'))->not->toContain('color: red');+})->with([false, true]);+test('email integration stores the inline uploaded images preference', function () {$user = $this->actingAsUser();$workspace = $this->createUserWorkspace($user);