Skip to content
Snippets Groups Projects
Commit a24f3ca3 authored by catch's avatar catch
Browse files

Issue #309343 by pameeela, skipper-vp, mdranove, quietone, Liberation, mile23,...

Issue #309343 by pameeela, skipper-vp, mdranove, quietone, Liberation, mile23, alexpott: MailFormatHelper::htmlToText() incorrect handling of newlines in anchor links
parent 668ba96c
No related branches found
No related tags found
No related merge requests found
......@@ -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;
......
......@@ -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",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment