Browse Source

Move wrap_and_quote() method to rcmail_action_mail_compose

pull/8233/head
Aleksander Machniak 4 years ago
parent
commit
d6c2e9c3f5
  1. 54
      program/actions/mail/compose.php
  2. 63
      program/actions/mail/index.php
  3. 21
      tests/Actions/Mail/Compose.php
  4. 21
      tests/Actions/Mail/Index.php

54
program/actions/mail/compose.php

@ -1713,4 +1713,58 @@ class rcmail_action_mail_compose extends rcmail_action_mail_index
return false;
}
/**
* Wrap text to a given number of characters per line
* but respect the mail quotation of replies messages (>).
* Finally add another quotation level by prepending the lines
* with >
*
* @param string $text Text to wrap
* @param int $length The line width
* @param bool $quote Enable quote indentation
*
* @return string The wrapped text
*/
public static function wrap_and_quote($text, $length = 72, $quote = true)
{
// Rebuild the message body with a maximum of $max chars, while keeping quoted message.
$max = max(75, $length + 8);
$lines = preg_split('/\r?\n/', trim($text));
$out = '';
foreach ($lines as $line) {
// don't wrap already quoted lines
if (isset($line[0]) && $line[0] == '>') {
$line = rtrim($line);
if ($quote) {
$line = '>' . $line;
}
}
// wrap lines above the length limit, but skip these
// special lines with links list created by rcube_html2text
else if (mb_strlen($line) > $max && !preg_match('|^\[[0-9]+\] https?://\S+$|', $line)) {
$newline = '';
foreach (explode("\n", rcube_mime::wordwrap($line, $length - 2)) as $l) {
if ($quote) {
$newline .= strlen($l) ? "> $l\n" : ">\n";
}
else {
$newline .= "$l\n";
}
}
$line = rtrim($newline);
}
else if ($quote) {
$line = '> ' . $line;
}
// Append the line
$out .= $line . "\n";
}
return rtrim($out, "\n");
}
}

63
program/actions/mail/index.php

@ -1501,61 +1501,6 @@ class rcmail_action_mail_index extends rcmail_action
return $out;
}
/**
* Wrap text to a given number of characters per line
* but respect the mail quotation of replies messages (>).
* Finally add another quotation level by prepending the lines
* with >
*
* @param string $text Text to wrap
* @param int $length The line width
* @param bool $quote Enable quote indentation
*
* @return string The wrapped text
*/
public static function wrap_and_quote($text, $length = 72, $quote = true)
{
// Rebuild the message body with a maximum of $max chars, while keeping quoted message.
$max = max(75, $length + 8);
$lines = preg_split('/\r?\n/', trim($text));
$out = '';
foreach ($lines as $line) {
// don't wrap already quoted lines
if (isset($line[0]) && $line[0] == '>') {
$line = rtrim($line);
if ($quote) {
$line = '>' . $line;
}
}
// wrap lines above the length limit, but skip these
// special lines with links list created by rcube_html2text
else if (mb_strlen($line) > $max && !preg_match('|^\[[0-9]+\] https?://\S+$|', $line)) {
$newline = '';
foreach (explode("\n", rcube_mime::wordwrap($line, $length - 2)) as $l) {
if ($quote) {
$newline .= strlen($l) ? "> $l\n" : ">\n";
}
else {
$newline .= "$l\n";
}
}
$line = rtrim($newline);
}
else if ($quote) {
$line = '> ' . $line;
}
// Append the line
$out .= $line . "\n";
}
return rtrim($out, "\n");
}
/**
* Return attachment filename, handle empty filename case
*
@ -1749,4 +1694,12 @@ class rcmail_action_mail_index extends rcmail_action
return array_values($mimetypes);
}
/**
* @deprecated Moved to rcmail_action_mail_compose
*/
public static function wrap_and_quote($text, $length = 72, $quote = true)
{
return rcmail_action_mail_compose::wrap_and_quote($text, $length, $quote);
}
}

21
tests/Actions/Mail/Compose.php

@ -16,4 +16,25 @@ class Actions_Mail_Compose extends ActionTestCase
$this->assertInstanceOf('rcmail_action', $object);
}
/**
* Test wrap_and_quote() method
*/
function test_wrap_and_quote()
{
$action = new rcmail_action_mail_compose;
$this->assertSame('> ', $action->wrap_and_quote(''));
$this->assertSame('', $action->wrap_and_quote('', 72, false));
$result = $action->wrap_and_quote("test1\ntest2");
$expected = "> test1\n> test2";
$this->assertSame($expected, $result);
$result = $action->wrap_and_quote("> test1\n> test2");
$expected = ">> test1\n>> test2";
$this->assertSame($expected, $result);
}
}

21
tests/Actions/Mail/Index.php

@ -242,27 +242,6 @@ class Actions_Mail_Index extends ActionTestCase
$this->assertSame($expected, $result);
}
/**
* Test wrap_and_quote() method
*/
function test_wrap_and_quote()
{
$action = new rcmail_action_mail_index;
$this->assertSame('> ', $action->wrap_and_quote(''));
$this->assertSame('', $action->wrap_and_quote('', 72, false));
$result = $action->wrap_and_quote("test1\ntest2");
$expected = "> test1\n> test2";
$this->assertSame($expected, $result);
$result = $action->wrap_and_quote("> test1\n> test2");
$expected = ">> test1\n>> test2";
$this->assertSame($expected, $result);
}
/**
* Test attachment_name() method
*/

Loading…
Cancel
Save