diff --git a/api/src/Mail/Html.php b/api/src/Mail/Html.php index 07e8de2fd0..0ec923a5e0 100644 --- a/api/src/Mail/Html.php +++ b/api/src/Mail/Html.php @@ -303,6 +303,13 @@ class Html $_html = str_replace(array("\r\n","\n"),($isHTML?'':' '),$_html); } } + + // Handle lists + if(stripos($_html, ' '~]*>\r*\n*~si', 1 => '~]*>\r*\n*~si', @@ -471,6 +478,48 @@ class Html } } + /** + * Replace HTML lists with a plain text equivalent + * + * @param string $html + * + * @return string + */ + static function replaceLists($html) + { + if(!$html || stripos($html, 'normalizeDocument(); + + foreach(array('ol','ul') as $list_type) + { + $lists = $dom->getElementsByTagName($list_type); + foreach($lists as $list) + { + $list_text = "\r\n"; + $item_count = 0; + $prefix = $list_type == 'ul' ? ' * ' : '. '; + foreach($list->getElementsByTagName('li') as $element) + { + $list_text .= ($list_type == 'ol' ? ' '. ++$item_count : '') . $prefix . $element->textContent . "\r\n"; + } + + $list->parentNode->replaceChild($dom->createTextNode($list_text), $list); + } + } + + return $dom->saveHTML(); + } + /** * split html by PRE tag, return array with all content pre-sections isolated in array elements * @author Leithoff, Klaus diff --git a/api/tests/Mail/HtmlTest.php b/api/tests/Mail/HtmlTest.php new file mode 100644 index 0000000000..7e791d4b23 --- /dev/null +++ b/api/tests/Mail/HtmlTest.php @@ -0,0 +1,66 @@ +assertEquals($expected_text, $replaced); + } + + /** + * Data for checking HTML list conversion to plain text + * + * HTML first, then expected text + */ + public function listDataProvider() + { + return array( + // HTML + // Plaintext + ['', ''], + ['Not actually HTML', 'Not actually HTML'], + ['HTML, but NO list here', 'HTML, but NO list here'], + ["

Unordered list:

  • First
  • \r\n
  • Second
  • \r\n
  • Third
  • \r\n
\r\nPost text

", + "

Unordered list:\r\n * First\r\n * Second\r\n * Third\r\n

\r\nPost text

\n"], + ["Ordered list:". + "
  1. First
  2. \r\n" + . "
  3. Second
  4. \r\n" + . "
  5. Third
  6. \r\n" + . "
Post text", + "

Ordered list:\r\n" + . " 1. First\r\n" + . " 2. Second\r\n" + . " 3. Third\r\n" + . "

Post text

\n"], + ); + } +}