From 3f4521bcf4b538b6ac54817cfad22b51e347546d Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Wed, 17 Jun 2015 03:03:03 -0400
Subject: [PATCH] Fix so plain text signature field uses monospace font (#1490435)

---
 program/lib/Roundcube/rcube_text2html.php |  123 +++++++++++++++++++++++++++--------------
 1 files changed, 81 insertions(+), 42 deletions(-)

diff --git a/program/lib/Roundcube/rcube_text2html.php b/program/lib/Roundcube/rcube_text2html.php
index cb4390e..5c240d5 100644
--- a/program/lib/Roundcube/rcube_text2html.php
+++ b/program/lib/Roundcube/rcube_text2html.php
@@ -1,6 +1,6 @@
 <?php
 
-/**
+/*
  +-----------------------------------------------------------------------+
  | This file is part of the Roundcube Webmail client                     |
  | Copyright (C) 2008-2014, The Roundcube Dev Team                       |
@@ -45,18 +45,23 @@
      */
     protected $config = array(
         // non-breaking space
-        'space'  => "\xC2\xA0",
+        'space' => "\xC2\xA0",
         // enables format=flowed parser
         'flowed' => false,
         // enables wrapping for non-flowed text
-        'wrap'   => false,
+        'wrap' => true,
         // line-break tag
-        'break'  => "<br>\n",
+        'break' => "<br>\n",
         // prefix and suffix (wrapper element)
-        'begin'  => '<div class="pre">',
-        'end'    => '</div>',
+        'begin' => '<div class="pre">',
+        'end'   => '</div>',
         // enables links replacement
-        'links'  => true,
+        'links' => true,
+        // string replacer class
+        'replacer' => 'rcube_string_replacer',
+        // prefix and suffix of unwrappable line
+        'nobr_start' => '<span style="white-space:nowrap">',
+        'nobr_end'   => '</span>',
     );
 
 
@@ -127,10 +132,8 @@
      */
     protected function _convert()
     {
-        $text = stripslashes($this->text);
-
         // Convert TXT to HTML
-        $this->html       = $this->_converter($text);
+        $this->html       = $this->_converter($this->text);
         $this->_converted = true;
     }
 
@@ -143,7 +146,7 @@
     {
         // make links and email-addresses clickable
         $attribs  = array('link_attribs' => array('rel' => 'noreferrer', 'target' => '_blank'));
-        $replacer = new rcmail_string_replacer($attribs);
+        $replacer = new $this->config['replacer']($attribs);
 
         if ($this->config['flowed']) {
             $flowed_char = 0x01;
@@ -158,12 +161,12 @@
         // 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 <blockquote>
+        for ($n = 0, $cnt = count($text); $n < $cnt; $n++) {
             $flowed = false;
-            if ($this->config['flowed'] && ord($text[0]) == $flowed_char) {
+            if ($this->config['flowed'] && ord($text[$n][0]) == $flowed_char) {
                 $flowed   = true;
                 $text[$n] = substr($text[$n], 1);
             }
@@ -172,43 +175,71 @@
                 $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('<blockquote>', $q - $quote_level))) . $text[$n];
-                    $last = $n;
+                    if ($last !== null) {
+                        $text[$last] .= (!$length ? "\n" : '')
+                            . $replacer->get_replacement($replacer->add(
+                                str_repeat('<blockquote>', $q - $quote_level)))
+                            . $text[$n];
+
+                        unset($text[$n]);
+                    }
+                    else {
+                        $text[$n] = $replacer->get_replacement($replacer->add(
+                            str_repeat('<blockquote>', $q - $quote_level))) . $text[$n];
+
+                        $last = $n;
+                    }
                 }
                 else if ($q < $quote_level) {
-                    $text[$n] = $replacer->get_replacement($replacer->add(
-                        str_repeat('</blockquote>', $quote_level - $q))) . $text[$n];
+                    $text[$last] .= (!$length ? "\n" : '')
+                        . $replacer->get_replacement($replacer->add(
+                            str_repeat('</blockquote>', $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('</blockquote>', $quote_level))) . $text[$n];
+                    $text[$last] .= (!$length ? "\n" : '')
+                        . $replacer->get_replacement($replacer->add(
+                            str_repeat('</blockquote>', $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('</blockquote>', $quote_level))) . $text[$n];
+            $text[$last] .= $replacer->get_replacement($replacer->add(
+                str_repeat('</blockquote>', $quote_level)));
         }
 
         $text = join("\n", $text);
 
         // colorize signature (up to <sig_max_lines> 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) {
@@ -222,9 +253,6 @@
 
         // insert url/mailto links and citation tags
         $text = $replacer->resolve($text);
-
-        // replace \n before </blockquote>
-        $text = str_replace("\n</blockquote>", "</blockquote>", $text);
 
         // replace line breaks
         $text = str_replace("\n", $this->config['break'], $text);
@@ -246,30 +274,41 @@
 
         // skip signature separator
         if ($text == '-- ') {
-            return $text;
+            return '--' . $this->config['space'];
         }
 
         // replace HTML special characters
         $text = strtr($text, $table);
 
-        $nbsp = $this->config['space'];
-
         // replace some whitespace characters
         $text = str_replace(array("\r", "\t"), array('', '    '), $text);
 
+        $nbsp = $this->config['space'];
+
         // replace spaces with non-breaking spaces
         if ($is_flowed) {
-            $text = preg_replace_callback('/(^|[^ ])( +)/', function($matches) {
-                    if (!strlen($matches[2])) {
-                        return str_repeat($nbsp, strlen($matches[2]));
-                    }
-                    else {
-                        return $matches[1] . ' ' . str_repeat($nbsp, strlen($matches[2])-1);
-                    }
-                }, $text);
+            $pos  = 0;
+            $diff = 0;
+            $len  = strlen($nbsp);
+            $copy = $text;
+
+            while (($pos = strpos($text, ' ', $pos)) !== false) {
+                if ($pos == 0 || $text[$pos-1] == ' ') {
+                    $copy = substr_replace($copy, $nbsp, $pos + $diff, 1);
+                    $diff += $len - 1;
+                }
+                $pos++;
+            }
+
+            $text = $copy;
         }
-        else {
+        // make the whole line non-breakable if needed
+        else if ($text !== '' && preg_match('/[^a-zA-Z0-9_]/', $text)) {
+            // use non-breakable spaces to correctly display
+            // trailing/leading spaces and multi-space inside
             $text = str_replace(' ', $nbsp, $text);
+            // wrap in nobr element, so it's not wrapped on e.g. - or /
+            $text = $this->config['nobr_start'] . $text .  $this->config['nobr_end'];
         }
 
         return $text;

--
Gitblit v1.9.1