From 0f15219a93c7f6de97223d004125fb32438ffaf7 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Sat, 12 Oct 2013 07:03:31 -0400
Subject: [PATCH] Fix text wrapping issue with long unwrappable lines (#1489371)

---
 CHANGELOG                            |    1 +
 tests/Framework/Mime.php             |    4 ++++
 program/lib/Roundcube/rcube_mime.php |   14 +++++++-------
 3 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 36d35e3..e14f932 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
 CHANGELOG Roundcube Webmail
 ===========================
 
+- Fix text wrapping issue with long unwrappable lines (#1489371)
 - Add spellchecker backend for the After the Deadline service
 - Replace markdown-style [1] link indexes in plain text email bodies
 - Fixed issues where HTML comments inside style tag would hang Internet Explorer
diff --git a/program/lib/Roundcube/rcube_mime.php b/program/lib/Roundcube/rcube_mime.php
index 96a8eac..9c22203 100644
--- a/program/lib/Roundcube/rcube_mime.php
+++ b/program/lib/Roundcube/rcube_mime.php
@@ -637,7 +637,8 @@
                     if ($nextChar === ' ' || $nextChar === $separator) {
                         $afterNextChar = mb_substr($string, $width + 1, 1);
 
-                        if ($afterNextChar === false) {
+                        // Note: mb_substr() does never return False
+                        if ($afterNextChar === false || $afterNextChar === '') {
                             $subString .= $nextChar;
                         }
 
@@ -650,16 +651,16 @@
                             $subString = mb_substr($subString, 0, $spacePos);
                             $cutLength = $spacePos + 1;
                         }
-                        else if ($cut === false && $breakPos === false) {
-                            $subString = $string;
-                            $cutLength = null;
-                        }
                         else if ($cut === false) {
                             $spacePos = mb_strpos($string, ' ', 0);
 
-                            if ($spacePos !== false && $spacePos < $breakPos) {
+                            if ($spacePos !== false && ($breakPos === false || $spacePos < $breakPos)) {
                                 $subString = mb_substr($string, 0, $spacePos);
                                 $cutLength = $spacePos + 1;
+                            }
+                            else if ($breakPos === false) {
+                                $subString = $string;
+                                $cutLength = null;
                             }
                             else {
                                 $subString = mb_substr($string, 0, $breakPos);
@@ -667,7 +668,6 @@
                             }
                         }
                         else {
-                            $subString = mb_substr($subString, 0, $width);
                             $cutLength = $width;
                         }
                     }
diff --git a/tests/Framework/Mime.php b/tests/Framework/Mime.php
index 4db1856..1450b4f 100644
--- a/tests/Framework/Mime.php
+++ b/tests/Framework/Mime.php
@@ -197,6 +197,10 @@
                 array("http://xx.xxx.xx.xxx:8080/addressbooks/roundcubexxxxx%40xxxxxxxxxxxxxxxxxxxxxxx.xx.xx/testing/", 70),
                 "http://xx.xxx.xx.xxx:8080/addressbooks/roundcubexxxxx%40xxxxxxxxxxxxxxxxxxxxxxx.xx.xx/testing/",
             ),
+            array(
+                array("this-is-just-some-blabla-to-make-this-more-than-seventy-five-characters-in-a-row -- this line should be wrapped", 20, "\n"),
+                "this-is-just-some-blabla-to-make-this-more-than-seventy-five-characters-in-a-row\n-- this line should\nbe wrapped",
+            ),
         );
 
         foreach ($samples as $sample) {

--
Gitblit v1.9.1