From eda92ed4c0d2735144df8fa2136584de69634bdb Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Sun, 11 May 2014 11:03:45 +0200 Subject: Improved display of plain text messages and text to HTML conversion (#1488937) Now instead of
 we use 
styled with monospace font. We replace whitespace characters with non-breaking spaces where needed. I.e. plain text is always unwrappable, until it uses format=flowed, in such a case only flowed paragraphs are wrappable. Also conversion of text to HTML in compose editor was modified in the same way. --- tests/Framework/Text2Html.php | 81 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 tests/Framework/Text2Html.php (limited to 'tests/Framework/Text2Html.php') diff --git a/tests/Framework/Text2Html.php b/tests/Framework/Text2Html.php new file mode 100644 index 000000000..91dabf2b7 --- /dev/null +++ b/tests/Framework/Text2Html.php @@ -0,0 +1,81 @@ + '', + 'end' => '', + 'break' => '
', + 'links' => false, + 'flowed' => false, + 'space' => '_', // replace UTF-8 non-breaking space for simpler testing + ); + + $data[] = array(" aaaa", "_aaaa", $options); + $data[] = array("aaaa aaaa", "aaaa_aaaa", $options); + $data[] = array("aaaa aaaa", "aaaa__aaaa", $options); + $data[] = array("aaaa aaaa", "aaaa___aaaa", $options); + $data[] = array("aaaa\taaaa", "aaaa____aaaa", $options); + $data[] = array("aaaa\naaaa", "aaaa
aaaa", $options); + $data[] = array("aaaa\n aaaa", "aaaa
_aaaa", $options); + $data[] = array("aaaa\n aaaa", "aaaa
__aaaa", $options); + $data[] = array("aaaa\n aaaa", "aaaa
___aaaa", $options); + $data[] = array("\taaaa", "____aaaa", $options); + $data[] = array("\naaaa", "
aaaa", $options); + $data[] = array("\n aaaa", "
_aaaa", $options); + $data[] = array("\n aaaa", "
__aaaa", $options); + $data[] = array("\n aaaa", "
___aaaa", $options); + $data[] = array("aaaa\n\nbbbb", "aaaa

bbbb", $options); + $data[] = array(">aaaa \n>aaaa", "
aaaa_
aaaa
", $options); + $data[] = array(">aaaa\n>aaaa", "
aaaa
aaaa
", $options); + $data[] = array(">aaaa \n>bbbb\ncccc dddd", "
aaaa_
bbbb
cccc_dddd", $options); + + $options['flowed'] = true; + + $data[] = array(" aaaa", "aaaa", $options); + $data[] = array("aaaa aaaa", "aaaa_aaaa", $options); + $data[] = array("aaaa aaaa", "aaaa__aaaa", $options); + $data[] = array("aaaa aaaa", "aaaa___aaaa", $options); + $data[] = array("aaaa\taaaa", "aaaa____aaaa", $options); + $data[] = array("aaaa\naaaa", "aaaa
aaaa", $options); + $data[] = array("aaaa\n aaaa", "aaaa
aaaa", $options); + $data[] = array("aaaa\n aaaa", "aaaa
_aaaa", $options); + $data[] = array("aaaa\n aaaa", "aaaa
__aaaa", $options); + $data[] = array("\taaaa", "____aaaa", $options); + $data[] = array("\naaaa", "
aaaa", $options); + $data[] = array("\n aaaa", "
aaaa", $options); + $data[] = array("\n aaaa", "
_aaaa", $options); + $data[] = array("\n aaaa", "
__aaaa", $options); + $data[] = array("aaaa\n\nbbbb", "aaaa

bbbb", $options); + $data[] = array(">aaaa \n>aaaa", "
aaaa aaaa
", $options); + $data[] = array(">aaaa\n>aaaa", "
aaaa
aaaa
", $options); + $data[] = array(">aaaa \n>bbbb\ncccc dddd", "
aaaa bbbb
cccc_dddd", $options); + + return $data; + } + + /** + * Test text to html conversion + * + * @dataProvider data_text2html + */ + function test_text2html($input, $output, $options) + { + $t2h = new rcube_text2html($input, false, $options); + + $html = $t2h->get_html(); + + $this->assertEquals($output, $html); + } +} -- cgit v1.2.3 From f0992426d9c5af5046c76a2da86183d0c3a40084 Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Tue, 13 May 2014 19:40:00 +0200 Subject: Bring back the old behaviour where text messages without format=flowed are auto-wrapped. Make it the default in text2html class. --- program/lib/Roundcube/rcube_text2html.php | 2 +- program/steps/mail/func.inc | 3 ++- tests/Framework/Text2Html.php | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) (limited to 'tests/Framework/Text2Html.php') diff --git a/program/lib/Roundcube/rcube_text2html.php b/program/lib/Roundcube/rcube_text2html.php index 60016fffd..8bcda301c 100644 --- a/program/lib/Roundcube/rcube_text2html.php +++ b/program/lib/Roundcube/rcube_text2html.php @@ -49,7 +49,7 @@ class rcube_text2html // enables format=flowed parser 'flowed' => false, // enables wrapping for non-flowed text - 'wrap' => false, + 'wrap' => true, // line-break tag 'break' => "
\n", // prefix and suffix (wrapper element) diff --git a/program/steps/mail/func.inc b/program/steps/mail/func.inc index ac0d7fc5f..a1d1a4163 100644 --- a/program/steps/mail/func.inc +++ b/program/steps/mail/func.inc @@ -874,7 +874,8 @@ function rcmail_print_body($part, $p = array()) */ function rcmail_plain_body($body, $flowed = false) { - $text2html = new rcube_text2html($body, false, array('flowed' => $flowed)); + $options = array('flowed' => $flowed, 'wrap' => !$flowed); + $text2html = new rcube_text2html($body, false, $options); $body = $text2html->get_html(); return $body; diff --git a/tests/Framework/Text2Html.php b/tests/Framework/Text2Html.php index 91dabf2b7..af2604d8e 100644 --- a/tests/Framework/Text2Html.php +++ b/tests/Framework/Text2Html.php @@ -19,6 +19,7 @@ class Framework_Text2Html extends PHPUnit_Framework_TestCase 'break' => '
', 'links' => false, 'flowed' => false, + 'wrap' => false, 'space' => '_', // replace UTF-8 non-breaking space for simpler testing ); -- cgit v1.2.3 From c0a5aa5f5ff38ac7b8a650b07c134b7b86deb27f Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Thu, 15 May 2014 10:41:35 +0200 Subject: Improved handling of new-lines in quoted paragraphs on text2html conversion --- program/lib/Roundcube/rcube_text2html.php | 65 +++++++++++++++++++++---------- skins/classic/mail.css | 2 +- skins/larry/mail.css | 2 +- tests/Framework/Text2Html.php | 11 ++++++ 4 files changed, 58 insertions(+), 22 deletions(-) (limited to 'tests/Framework/Text2Html.php') diff --git a/program/lib/Roundcube/rcube_text2html.php b/program/lib/Roundcube/rcube_text2html.php index 8bcda301c..363f1b21f 100644 --- a/program/lib/Roundcube/rcube_text2html.php +++ b/program/lib/Roundcube/rcube_text2html.php @@ -158,10 +158,10 @@ class rcube_text2html // split body into single lines $text = preg_split('/\r?\n/', $text); $quote_level = 0; - $last = -1; + $last = null; - // find/mark quoted lines... - for ($n=0, $cnt=count($text); $n < $cnt; $n++) { + // wrap quoted lines with
+ for ($n = 0, $cnt = count($text); $n < $cnt; $n++) { $flowed = false; if ($this->config['flowed'] && ord($text[$n][0]) == $flowed_char) { $flowed = true; @@ -172,43 +172,71 @@ class rcube_text2html $q = substr_count($regs[0], '>'); $text[$n] = substr($text[$n], strlen($regs[0])); $text[$n] = $this->_convert_line($text[$n], $flowed || $this->config['wrap']); + $_length = strlen(str_replace(' ', '', $text[$n])); if ($q > $quote_level) { - $text[$n] = $replacer->get_replacement($replacer->add( - str_repeat('
', $q - $quote_level))) . $text[$n]; - $last = $n; + if ($last !== null) { + $text[$last] .= (!$length ? "\n" : '') + . $replacer->get_replacement($replacer->add( + str_repeat('
', $q - $quote_level))) + . $text[$n]; + + unset($text[$n]); + } + else { + $text[$n] = $replacer->get_replacement($replacer->add( + str_repeat('
', $q - $quote_level))) . $text[$n]; + + $last = $n; + } } else if ($q < $quote_level) { - $text[$n] = $replacer->get_replacement($replacer->add( - str_repeat('
', $quote_level - $q))) . $text[$n]; + $text[$last] .= (!$length ? "\n" : '') + . $replacer->get_replacement($replacer->add( + str_repeat('
', $quote_level - $q))) + . $text[$n]; + + unset($text[$n]); + } + else { $last = $n; } } else { $text[$n] = $this->_convert_line($text[$n], $flowed || $this->config['wrap']); - $q = 0; + $q = 0; + $_length = strlen(str_replace(' ', '', $text[$n])); if ($quote_level > 0) { - $text[$n] = $replacer->get_replacement($replacer->add( - str_repeat('
', $quote_level))) . $text[$n]; + $text[$last] .= (!$length ? "\n" : '') + . $replacer->get_replacement($replacer->add( + str_repeat('
', $quote_level))) + . $text[$n]; + + unset($text[$n]); + } + else { + $last = $n; } } $quote_level = $q; + $length = $_length; } if ($quote_level > 0) { - $text[$n] = $replacer->get_replacement($replacer->add( - str_repeat('', $quote_level))) . $text[$n]; + $text[$last] .= $replacer->get_replacement($replacer->add( + str_repeat('', $quote_level))); } $text = join("\n", $text); // colorize signature (up to lines) - $len = strlen($text); + $len = strlen($text); + $sig_sep = "--" . $this->config['space'] . "\n"; $sig_max_lines = rcube::get_instance()->config->get('sig_max_lines', 15); - while (($sp = strrpos($text, "-- \n", $sp ? -$len+$sp-1 : 0)) !== false) { + while (($sp = strrpos($text, $sig_sep, $sp ? -$len+$sp-1 : 0)) !== false) { if ($sp == 0 || $text[$sp-1] == "\n") { // do not touch blocks with more that X lines if (substr_count($text, "\n", $sp) < $sig_max_lines) { @@ -223,9 +251,6 @@ class rcube_text2html // insert url/mailto links and citation tags $text = $replacer->resolve($text); - // replace \n before - $text = str_replace("\n", "", $text); - // replace line breaks $text = str_replace("\n", $this->config['break'], $text); @@ -246,7 +271,7 @@ class rcube_text2html // skip signature separator if ($text == '-- ') { - return $text; + return '--' . $this->config['space']; } // replace HTML special characters @@ -276,7 +301,7 @@ class rcube_text2html } else { // make the whole line non-breakable - $text = str_replace(array(' ', '-'), array($nbsp, '-⁠'), $text); + $text = str_replace(array(' ', '-', '/'), array($nbsp, '-⁠', '/⁠'), $text); } return $text; diff --git a/skins/classic/mail.css b/skins/classic/mail.css index 47faa29af..6409b6b9b 100644 --- a/skins/classic/mail.css +++ b/skins/classic/mail.css @@ -1325,7 +1325,7 @@ div.message-part blockquote border-left: 2px solid blue; border-right: 2px solid blue; background-color: #F6F6F6; - margin: 0; + margin: 2px 0; padding: 0 0.4em; overflow: hidden; text-overflow: ellipsis; diff --git a/skins/larry/mail.css b/skins/larry/mail.css index 7afb14fba..8306afd9f 100644 --- a/skins/larry/mail.css +++ b/skins/larry/mail.css @@ -1119,7 +1119,7 @@ div.message-part blockquote { border-left: 2px solid blue; border-right: 2px solid blue; background-color: #F6F6F6; - margin: 0; + margin: 2px 0; padding: 0 0.4em; overflow: hidden; text-overflow: ellipsis; diff --git a/tests/Framework/Text2Html.php b/tests/Framework/Text2Html.php index af2604d8e..8d091d5c9 100644 --- a/tests/Framework/Text2Html.php +++ b/tests/Framework/Text2Html.php @@ -41,6 +41,7 @@ class Framework_Text2Html extends PHPUnit_Framework_TestCase $data[] = array(">aaaa \n>aaaa", "
aaaa_
aaaa
", $options); $data[] = array(">aaaa\n>aaaa", "
aaaa
aaaa
", $options); $data[] = array(">aaaa \n>bbbb\ncccc dddd", "
aaaa_
bbbb
cccc_dddd", $options); + $data[] = array("aaaa-bbbb/cccc", "aaaa-⁠bbbb/⁠cccc", $options); $options['flowed'] = true; @@ -63,6 +64,16 @@ class Framework_Text2Html extends PHPUnit_Framework_TestCase $data[] = array(">aaaa\n>aaaa", "
aaaa
aaaa
", $options); $data[] = array(">aaaa \n>bbbb\ncccc dddd", "
aaaa bbbb
cccc_dddd", $options); + $options['flowed'] = false; + $options['wrap'] = true; + + $data[] = array(">>aaaa bbbb\n>>\n>>>\n>cccc\n\ndddd eeee", + "
aaaa bbbb


cccc

dddd eeee", $options); + $data[] = array("\n>>aaaa\n\ndddd", + "
aaaa

dddd", $options); + $data[] = array("aaaa\n>bbbb\n>cccc\n\ndddd\n>>test", + "aaaa
bbbb
cccc

dddd
test
", $options); + return $data; } -- cgit v1.2.3 From 59b765d83927cb9e81bf69656db3a7dbdc1b1b41 Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Sun, 18 May 2014 09:01:12 +0200 Subject: Don't remove links when html signature is converted to text (#1489621) Fix so when switching editor mode original version of signature is used (#1488849) --- CHANGELOG | 2 ++ program/js/app.js | 62 +++++++++++++++++++++++++++++++++++++----- program/steps/mail/compose.inc | 2 +- tests/Framework/Html2text.php | 5 ++++ tests/Framework/Text2Html.php | 1 + 5 files changed, 64 insertions(+), 8 deletions(-) (limited to 'tests/Framework/Text2Html.php') diff --git a/CHANGELOG b/CHANGELOG index db21586e7..27286e8e5 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -20,6 +20,8 @@ CHANGELOG Roundcube Webmail - Add configurable LDAP_OPT_DEREF option (#1489864) - Optimize some framed pages content for better performance (#1489792) - Improve text messages display and conversion to HTML (#1488937) +- Don't remove links when html signature is converted to text (#1489621) +- Fix so when switching editor mode original version of signature is used (#1488849) - Fix mbox files import - Fix unintentional draft autosave request if autosave is disabled (#1489882) - Fix malformed References: header in send/saved mail (#1489891) diff --git a/program/js/app.js b/program/js/app.js index 97b8192b2..24d1f19e7 100644 --- a/program/js/app.js +++ b/program/js/app.js @@ -3439,10 +3439,27 @@ function rcube_webmail() { this.stop_spellchecking(); - var input = $('#' + props.id); + var ed, curr, content, result, + // these non-printable chars are not removed on text2html and html2text + // we can use them as temp signature replacement + sig_mark = "\u0002\u0003", + input = $('#' + props.id), + signature = this.env.identity ? this.env.signatures[this.env.identity] : null, + is_sig = signature && signature.text && signature.text.length > 1; + + if (props.mode == 'html') { + content = input.val(); + + // replace current text signature with temp mark + if (is_sig) + content = content.replace(signature.text, sig_mark); + + // convert to html + result = this.plain2html(content, function(data) { + // replace signature mark with html version of the signature + if (is_sig) + data = data.replace(sig_mark, '
' + signature.html + '
'); - if (props.mode == 'html') - this.plain2html(input.val(), function(data) { input.val(data); tinyMCE.execCommand('mceAddControl', false, props.id); @@ -3451,13 +3468,43 @@ function rcube_webmail() $(tinyMCE.get(props.id).getBody()).css('font-family', ref.env.default_font); }, 500); }); - else - this.html2plain(tinyMCE.get(props.id).getContent(), function(data) { + } + else { + ed = tinyMCE.get(props.id); + + if (is_sig) { + // get current version of signature, we'll need it in + // case of html2text conversion abort + if (curr = ed.dom.get('_rc_sig')) + curr = curr.innerHTML; + + // replace current signature with some non-printable characters + // we use non-printable characters, because this replacement + // is visible to the user + // doing this after getContent() would be hard + ed.dom.setHTML('_rc_sig', sig_mark); + } + + // get html content + content = ed.getContent(); + + // convert html to text + result = this.html2plain(content, function(data) { tinyMCE.execCommand('mceRemoveControl', false, props.id); + + // replace signture mark with text version of the signature + if (is_sig) + data = data.replace(sig_mark, "\n" + signature.text); + input.val(data); }); - return true; + // bring back current signature + if (!result && curr) + ed.dom.setHTML('_rc_sig', curr); + } + + return result; }; this.insert_response = function(key) @@ -6834,7 +6881,8 @@ function rcube_webmail() || (format != 'html' && !(text.replace(/\xC2\xA0|\s/g, '')).length) ) { // without setTimeout() here, textarea is filled with initial (onload) content - setTimeout(function() { if (func) func(''); }, 50); + if (func) + setTimeout(function() { func(''); }, 50); return true; } diff --git a/program/steps/mail/compose.inc b/program/steps/mail/compose.inc index 0ceb85db2..a3eb4b8a3 100644 --- a/program/steps/mail/compose.inc +++ b/program/steps/mail/compose.inc @@ -611,7 +611,7 @@ function rcmail_compose_header_from($attrib) $text = $html = $sql_arr['signature']; if ($sql_arr['html_signature']) { - $h2t = new rcube_html2text($sql_arr['signature'], false, false); + $h2t = new rcube_html2text($sql_arr['signature'], false, true); $text = trim($h2t->get_text()); } else { diff --git a/tests/Framework/Html2text.php b/tests/Framework/Html2text.php index 2c7759f7d..76b1f16cd 100644 --- a/tests/Framework/Html2text.php +++ b/tests/Framework/Html2text.php @@ -41,6 +41,11 @@ class rc_html2text extends PHPUnit_Framework_TestCase 'in' => 'ś', 'out' => 'Ś', ), + 6 => array( + 'title' => 'Don\'t remove non-printable chars', + 'in' => chr(0x002).chr(0x003), + 'out' => chr(0x002).chr(0x003), + ), ); } diff --git a/tests/Framework/Text2Html.php b/tests/Framework/Text2Html.php index 8d091d5c9..8d1325dee 100644 --- a/tests/Framework/Text2Html.php +++ b/tests/Framework/Text2Html.php @@ -63,6 +63,7 @@ class Framework_Text2Html extends PHPUnit_Framework_TestCase $data[] = array(">aaaa \n>aaaa", "
aaaa aaaa
", $options); $data[] = array(">aaaa\n>aaaa", "
aaaa
aaaa
", $options); $data[] = array(">aaaa \n>bbbb\ncccc dddd", "
aaaa bbbb
cccc_dddd", $options); + $data[] = array(chr(0x002).chr(0x003), chr(0x002).chr(0x003), $options); $options['flowed'] = false; $options['wrap'] = true; -- cgit v1.2.3