diff --git a/core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php b/core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php index caa2af375400d8e3c289487c5bb6942be22b9df4..1a97b661ad1f350a62f118facf31352d91df0580 100644 --- a/core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php +++ b/core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php @@ -8,6 +8,7 @@ namespace Drupal\Core\Mail\Plugin\Mail; use Drupal\Component\Utility\Unicode; +use Drupal\Core\Mail\MailFormatHelper; use Drupal\Core\Mail\MailInterface; use Drupal\Core\Site\Settings; @@ -35,7 +36,7 @@ public function format(array $message) { // Join the body array into one string. $message['body'] = implode("\n\n", $message['body']); // Convert any HTML to plain-text. - $message['body'] = drupal_html_to_text($message['body']); + $message['body'] = MailFormatHelper::htmlToText($message['body']); // Wrap the mail body for sending. $message['body'] = drupal_wrap_mail($message['body']); diff --git a/core/modules/contact/src/MessageViewBuilder.php b/core/modules/contact/src/MessageViewBuilder.php index 23e9b4300432f45292993015aa6d947273af648d..47173386bf190ac280d6edda2b89e8decc856957 100644 --- a/core/modules/contact/src/MessageViewBuilder.php +++ b/core/modules/contact/src/MessageViewBuilder.php @@ -10,6 +10,7 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityViewBuilder; use Drupal\Component\Utility\String; +use Drupal\Core\Mail\MailFormatHelper; use Drupal\Core\Render\Element; /** @@ -55,7 +56,8 @@ public function view(EntityInterface $entity, $view_mode = 'full', $langcode = N if ($view_mode == 'mail') { // Convert field labels into headings. - // @todo Improve drupal_html_to_text() to convert DIVs correctly. + // @todo Improve \Drupal\Core\Mail\MailFormatHelper::htmlToText() to + // convert DIVs correctly. foreach (Element::children($build) as $key) { if (isset($build[$key]['#label_display']) && $build[$key]['#label_display'] == 'above') { $build[$key] += array('#prefix' => ''); @@ -64,7 +66,7 @@ public function view(EntityInterface $entity, $view_mode = 'full', $langcode = N } } $build = array( - '#markup' => drupal_html_to_text(drupal_render($build)), + '#markup' => MailFormatHelper::htmlToText(drupal_render($build)), ); } return $build; diff --git a/core/modules/contact/src/Tests/ContactSitewideTest.php b/core/modules/contact/src/Tests/ContactSitewideTest.php index 257468de1ec6a79ef085a314abc38455e5472e98..2f108663822a090d6150553e5d10e4776905c758 100644 --- a/core/modules/contact/src/Tests/ContactSitewideTest.php +++ b/core/modules/contact/src/Tests/ContactSitewideTest.php @@ -8,6 +8,7 @@ namespace Drupal\contact\Tests; use Drupal\Component\Utility\Unicode; +use Drupal\Core\Mail\MailFormatHelper; use Drupal\simpletest\WebTestBase; use Drupal\Core\Entity\EntityTypeInterface; @@ -299,7 +300,7 @@ function testAutoReply() { // We are testing the auto-reply, so there should be one email going to the sender. $captured_emails = $this->drupalGetMails(array('id' => 'contact_page_autoreply', 'to' => $email)); $this->assertEqual(count($captured_emails), 1); - $this->assertEqual(trim($captured_emails[0]['body']), trim(drupal_html_to_text($foo_autoreply))); + $this->assertEqual(trim($captured_emails[0]['body']), trim(MailFormatHelper::htmlToText($foo_autoreply))); // Test the auto-reply for form 'bar'. $email = $this->randomMachineName(32) . '@example.com'; @@ -308,7 +309,7 @@ function testAutoReply() { // Auto-reply for form 'bar' should result in one auto-reply email to the sender. $captured_emails = $this->drupalGetMails(array('id' => 'contact_page_autoreply', 'to' => $email)); $this->assertEqual(count($captured_emails), 1); - $this->assertEqual(trim($captured_emails[0]['body']), trim(drupal_html_to_text($bar_autoreply))); + $this->assertEqual(trim($captured_emails[0]['body']), trim(MailFormatHelper::htmlToText($bar_autoreply))); // Verify that no auto-reply is sent when the auto-reply field is left blank. $email = $this->randomMachineName(32) . '@example.com'; diff --git a/core/modules/system/src/Tests/Mail/HtmlToTextTest.php b/core/modules/system/src/Tests/Mail/HtmlToTextTest.php index e95e0b5d924bef852a2208d2379a45f381477a00..f26b49810d4a9ae516bf357f103574c7a7baaab6 100644 --- a/core/modules/system/src/Tests/Mail/HtmlToTextTest.php +++ b/core/modules/system/src/Tests/Mail/HtmlToTextTest.php @@ -9,11 +9,12 @@ use Drupal\Component\Utility\String; use Drupal\Component\Utility\Unicode; +use Drupal\Core\Mail\MailFormatHelper; use Drupal\Core\Site\Settings; use Drupal\simpletest\WebTestBase; /** - * Tests for drupal_html_to_text(). + * Tests for \Drupal\Core\Mail\MailFormatHelper::htmlToText(). * * @group Mail */ @@ -38,7 +39,7 @@ protected function stringToHtml($text) { } /** - * Helper function for testing drupal_html_to_text(). + * Helper function to test \Drupal\Core\Mail\MailFormatHelper::htmlToText(). * * @param $html * The source HTML string to be converted. @@ -48,13 +49,14 @@ protected function stringToHtml($text) { * A text message to display in the assertion message. * @param $allowed_tags * (optional) An array of allowed tags, or NULL to default to the full - * set of tags supported by drupal_html_to_text(). + * set of tags supported by + * \Drupal\Core\Mail\MailFormatHelper::htmlToText(). */ protected function assertHtmlToText($html, $text, $message, $allowed_tags = NULL) { preg_match_all('/<([a-z0-6]+)/', Unicode::strtolower($html), $matches); $tested_tags = implode(', ', array_unique($matches[1])); $message .= ' (' . $tested_tags . ')'; - $result = drupal_html_to_text($html, $allowed_tags); + $result = MailFormatHelper::htmlToText($html, $allowed_tags); $pass = $this->assertEqual($result, $text, String::checkPlain($message)); $verbose = 'html =
' . $this->stringToHtml($html)
       . '

' . 'result =
' . $this->stringToHtml($result)
@@ -67,7 +69,7 @@ protected function assertHtmlToText($html, $text, $message, $allowed_tags = NULL
   }
 
   /**
-   * Test all supported tags of drupal_html_to_text().
+   * Test supported tags of \Drupal\Core\Mail\MailFormatHelper::htmlToText().
    */
   public function testTags() {
     global $base_path, $base_url;
@@ -162,10 +164,11 @@ public function testTags() {
   }
 
   /**
-   * Test $allowed_tags argument of drupal_html_to_text().
+   * Tests allowing tags in \Drupal\Core\Mail\MailFormatHelper::htmlToText().
    */
   public function testDrupalHtmlToTextArgs() {
-    // The second parameter of drupal_html_to_text() overrules the allowed tags.
+    // The second parameter of \Drupal\Core\Mail\MailFormatHelper::htmlToText()
+    // overrules the allowed tags.
     $this->assertHtmlToText(
       'Drupal Drupal Drupal',
       "Drupal *Drupal* Drupal\n",
@@ -235,7 +238,7 @@ public function testDrupalHtmlToTextBlockTagToNewline() {
       . ''
       . '[text]';
-    $output = drupal_html_to_text($input);
+    $output = MailFormatHelper::htmlToText($input);
     $pass = $this->assertFalse(
       preg_match('/\][^\n]*\[/s', $output),
       'Block-level HTML tags should force newlines'
@@ -245,7 +248,7 @@ public function testDrupalHtmlToTextBlockTagToNewline() {
     }
     $output_upper = Unicode::strtoupper($output);
     $upper_input = Unicode::strtoupper($input);
-    $upper_output = drupal_html_to_text($upper_input);
+    $upper_output = MailFormatHelper::htmlToText($upper_input);
     $pass = $this->assertEqual(
       $upper_output,
       $output_upper,
@@ -331,7 +334,7 @@ public function testDrupalHtmlToTextParagraphs() {
   }
 
   /**
-   * Tests that drupal_html_to_text() wraps before 1000 characters.
+   * Tests \Drupal\Core\Mail\MailFormatHelper::htmlToText() wrapping.
    *
    * RFC 3676 says, "The Text/Plain media type is the lowest common
    * denominator of Internet email, with lines of no more than 998 characters."
@@ -344,7 +347,7 @@ public function testDrupalHtmlToTextParagraphs() {
    */
   public function testVeryLongLineWrap() {
     $input = 'Drupal

' . str_repeat('x', 2100) . '
Drupal'; - $output = drupal_html_to_text($input); + $output = MailFormatHelper::htmlToText($input); $eol = Settings::get('mail_line_endings', PHP_EOL); $maximum_line_length = 0; diff --git a/core/modules/system/system.api.php b/core/modules/system/system.api.php index e306b0920cd736a175b6767a4001ec27f4b2c349..4e98a643d40eafa9a42925baf099652dc67c2edb 100644 --- a/core/modules/system/system.api.php +++ b/core/modules/system/system.api.php @@ -6,6 +6,7 @@ */ use Drupal\Component\Utility\String; +use Drupal\Core\Mail\MailFormatHelper; use Drupal\Core\Url; /** @@ -482,7 +483,7 @@ function hook_mail($key, &$message, $params) { $subject = strtr($context['subject'], $variables); $body = strtr($context['message'], $variables); $message['subject'] .= str_replace(array("\r", "\n"), '', $subject); - $message['body'][] = drupal_html_to_text($body); + $message['body'][] = MailFormatHelper::htmlToText($body); } /**