From 8c293e2f280e9b4a61a8c6d83a52e0cf4c55f4eb Mon Sep 17 00:00:00 2001
From: Thomas Bruederli <thomas@roundcube.net>
Date: Sat, 19 Jan 2013 08:32:15 -0500
Subject: [PATCH] Fix format=flowed unfolding on quoted lines; added tests for rcube_mime::format_flowed() and rcube_mime::unfold_flowed()

---
 tests/src/format-flowed-unfolded.txt |   14 ++++++++++++++
 tests/src/format-flowed.txt          |   16 ++++++++++++++++
 tests/Framework/Mime.php             |   22 ++++++++++++++++++++++
 program/lib/Roundcube/rcube_mime.php |    3 ++-
 4 files changed, 54 insertions(+), 1 deletions(-)

diff --git a/program/lib/Roundcube/rcube_mime.php b/program/lib/Roundcube/rcube_mime.php
index eef8ca1..d5fb35b 100644
--- a/program/lib/Roundcube/rcube_mime.php
+++ b/program/lib/Roundcube/rcube_mime.php
@@ -480,7 +480,8 @@
                 $q = strlen(str_replace(' ', '', $regs[0]));
                 $line = substr($line, strlen($regs[0]));
 
-                if ($q == $q_level && $line
+                if ($q == $q_level
+                    && strlen($line[$last]) > 1  // don't hit if line only consist of one single white space
                     && isset($text[$last])
                     && $text[$last][strlen($text[$last])-1] == ' '
                 ) {
diff --git a/tests/Framework/Mime.php b/tests/Framework/Mime.php
index dcd5599..1f9a8c5 100644
--- a/tests/Framework/Mime.php
+++ b/tests/Framework/Mime.php
@@ -120,4 +120,26 @@
             $this->assertEquals($item['out'], $res, "Header decoding for: " . $idx);
         }
     }
+
+    /**
+     * Test format=flowed unfolding
+     */
+    function test_format_flowed()
+    {
+        $raw = file_get_contents(TESTS_DIR . 'src/format-flowed-unfolded.txt');
+        $flowed = file_get_contents(TESTS_DIR . 'src/format-flowed.txt');
+
+        $this->assertEquals($flowed, rcube_mime::format_flowed($raw, 80), "Test correct folding and space-stuffing");
+    }
+
+    /**
+     * Test format=flowed unfolding
+     */
+    function test_unfold_flowed()
+    {
+        $flowed = file_get_contents(TESTS_DIR . 'src/format-flowed.txt');
+        $unfolded = file_get_contents(TESTS_DIR . 'src/format-flowed-unfolded.txt');
+
+        $this->assertEquals($unfolded, rcube_mime::unfold_flowed($flowed), "Test correct unfolding of quoted lines");
+    }
 }
diff --git a/tests/src/format-flowed-unfolded.txt b/tests/src/format-flowed-unfolded.txt
new file mode 100644
index 0000000..8245d59
--- /dev/null
+++ b/tests/src/format-flowed-unfolded.txt
@@ -0,0 +1,14 @@
+I'm replying on this with a very long line which is then wrapped and space-stuffed because the draft is saved as format=flowed.
+From what's specified in RFC 2646 some lines need to be space-stuffed to avoid muning during transport.
+
+X
+
+On XX.YY.YYYY Y:YY, Somebody wrote:
+> This part is a reply wihtout any flowing lines. rcube_mime::unfold_flowed()
+> has to be careful with empty quoted lines because they might end with a
+> space but still shouldn't be considered as flowed!
+> 
+> The above empty line should persist after unfolding.
+> xxxxxxxxxx. xxxx xxxxx xxxxx xxxx xx xx.xx. xxxxxx xxxxxxxxxxxx, xxxx xx
+> 
+> ... and this one as well.
diff --git a/tests/src/format-flowed.txt b/tests/src/format-flowed.txt
new file mode 100644
index 0000000..522f829
--- /dev/null
+++ b/tests/src/format-flowed.txt
@@ -0,0 +1,16 @@
+I'm replying on this with a very long line which is then wrapped and 
+space-stuffed because the draft is saved as format=flowed.
+ From what's specified in RFC 2646 some lines need to be space-stuffed to 
+avoid muning during transport.
+
+X
+
+On XX.YY.YYYY Y:YY, Somebody wrote:
+> This part is a reply wihtout any flowing lines. rcube_mime::unfold_flowed()
+> has to be careful with empty quoted lines because they might end with a
+> space but still shouldn't be considered as flowed!
+> 
+> The above empty line should persist after unfolding.
+> xxxxxxxxxx. xxxx xxxxx xxxxx xxxx xx xx.xx. xxxxxx xxxxxxxxxxxx, xxxx xx
+> 
+> ... and this one as well.

--
Gitblit v1.9.1