From a24f3ca327e46e7c1ed0205263b53bf7d7443f63 Mon Sep 17 00:00:00 2001 From: catch <6915-catch@users.noreply.drupalcode.org> Date: Mon, 17 Mar 2025 13:14:02 +0000 Subject: [PATCH] Issue #309343 by pameeela, skipper-vp, mdranove, quietone, Liberation, mile23, alexpott: MailFormatHelper::htmlToText() incorrect handling of newlines in anchor links --- core/lib/Drupal/Core/Mail/MailFormatHelper.php | 5 +++-- .../modules/system/tests/src/Unit/Mail/HtmlToTextTest.php | 8 +++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/core/lib/Drupal/Core/Mail/MailFormatHelper.php b/core/lib/Drupal/Core/Mail/MailFormatHelper.php index 260174eca748..31762c501477 100644 --- a/core/lib/Drupal/Core/Mail/MailFormatHelper.php +++ b/core/lib/Drupal/Core/Mail/MailFormatHelper.php @@ -130,7 +130,7 @@ public static function htmlToText($string, $allowed_tags = NULL) { // 'See <a href="https://www.drupal.org">the Drupal site</a>' becomes // 'See the Drupal site [1]' with the URL included as a footnote. static::htmlToMailUrls(NULL, TRUE); - $pattern = '@(<a[^>]+?href="([^"]*)"[^>]*?>(.+?)</a>)@i'; + $pattern = '@(<a[^>]+?href="([^"]*)"[^>]*?>([^<]*)</a>)@i'; $string = preg_replace_callback($pattern, [static::class, 'htmlToMailUrls'], $string); $urls = static::htmlToMailUrls(); $footnotes = ''; @@ -343,7 +343,8 @@ protected static function htmlToMailUrls($match = NULL, $reset = FALSE) { [, , $url, $label] = $match; // Ensure all URLs are absolute. static::$urls[] = strpos($url, '://') ? $url : preg_replace(static::$regexp, $base_url . '/', $url); - return $label . ' [' . count(static::$urls) . ']'; + // Strip newlines and carriage returns from anchor text. + return preg_replace('/\r?\n|\r/', '', $label) . ' [' . count(static::$urls) . ']'; } } return static::$urls; diff --git a/core/modules/system/tests/src/Unit/Mail/HtmlToTextTest.php b/core/modules/system/tests/src/Unit/Mail/HtmlToTextTest.php index 02c6e11d0878..34e1478a8be8 100644 --- a/core/modules/system/tests/src/Unit/Mail/HtmlToTextTest.php +++ b/core/modules/system/tests/src/Unit/Mail/HtmlToTextTest.php @@ -74,7 +74,13 @@ protected function assertHtmlToText(string $html, string $text, string $message, public function testTags(): void { global $base_path, $base_url; $tests = [ - // @todo Trailing linefeeds should be trimmed. + // Tests tag inside <a>. + '<a href = "https://www.drupal.org"><b>Drupal.org</b> our site</a>' => "*Drupal.org* our site [1]\n\n[1] https://www.drupal.org\n", + // Tests newlines are stripped from anchor text. + '<a href = "https://www.drupal.org">Drupal' . "\n.org</a>" => "Drupal.org [1]\n\n[1] https://www.drupal.org\n", + // Tests newlines and carriage returns are stripped from anchor text. + '<a href = "https://www.drupal.org">Drupal' . "\r\n.org</a>" => "Drupal.org [1]\n\n[1] https://www.drupal.org\n", + // @todo Trailing newlines should be trimmed. '<a href = "https://www.drupal.org">Drupal.org</a>' => "Drupal.org [1]\n\n[1] https://www.drupal.org\n", // @todo Footer URLs should be absolute. "<a href = \"$base_path\">Homepage</a>" => "Homepage [1]\n\n[1] $base_url/\n", -- GitLab