Commit 55286952 authored by alexpott's avatar alexpott

Issue #2358999 by rpayanm, JeroenT, er.pushpinderrana, javivf, Ec1ipsis,...

Issue #2358999 by rpayanm, JeroenT, er.pushpinderrana, javivf, Ec1ipsis, aczietlow | Les Lim: Remove usage of drupal_html_to_text().
parent 031d0f6c
......@@ -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']);
......
......@@ -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;
......
......@@ -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';
......
......@@ -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 = <pre>' . $this->stringToHtml($html)
. '</pre><br />' . 'result = <pre>' . $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 <b>Drupal</b> Drupal',
"Drupal *Drupal* Drupal\n",
......@@ -235,7 +238,7 @@ public function testDrupalHtmlToTextBlockTagToNewline() {
. '<ul><li>[ul-li]</li>'
. '<li>[li-ul]</li></ul>'
. '[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<br /><p>' . str_repeat('x', 2100) . '</><br />Drupal';
$output = drupal_html_to_text($input);
$output = MailFormatHelper::htmlToText($input);
$eol = Settings::get('mail_line_endings', PHP_EOL);
$maximum_line_length = 0;
......
......@@ -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);
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment