From 2d880650d862a116327ef08c24c9c7f5c0586797 Mon Sep 17 00:00:00 2001
From: Adrian Lang <lang@cosmocode.de>
Date: Tue, 12 Jan 2010 14:28:38 +0100
Subject: [PATCH] Finally fix whitespace and HTML problem in diff.

---
 inc/DifferenceEngine.php | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/inc/DifferenceEngine.php b/inc/DifferenceEngine.php
index e28826c1f..760f1c39a 100644
--- a/inc/DifferenceEngine.php
+++ b/inc/DifferenceEngine.php
@@ -976,10 +976,17 @@ class TableDiffFormatter extends DiffFormatter
     $this->trailing_context_lines = 2;
   }
 
+    function format($diff) {
+        // Preserve whitespaces by converting some to non-breaking spaces.
+        // Do not convert all of them to allow word-wrap.
+        $val = parent::format($diff);
+        $val = str_replace('  ','&nbsp; ', $val);
+        $val = preg_replace('/ (?=<)|(?<=[ >]) /', '&nbsp;', $val);
+        return $val;
+    }
+
   function _pre($text){
     $text = htmlspecialchars($text);
-    $text = str_replace('  ',' &nbsp;',$text);
-    if($text{0} == ' ') $text = '&nbsp;'.substr($text,1);
     return $text;
   }
 
@@ -1003,27 +1010,21 @@ class TableDiffFormatter extends DiffFormatter
   }
 
   function addedLine( $line ) {
-    $line = str_replace('  ',' &nbsp;',$line);
-    if($line{0} == ' ') $line = '&nbsp;'.substr($line,1);
     return '<td>+</td><td class="diff-addedline">' .
       $line.'</td>';
+
   }
 
   function deletedLine( $line ) {
-    $line = str_replace('  ',' &nbsp;',$line);
-    if($line{0} == ' ') $line = '&nbsp;'.substr($line,1);
     return '<td>-</td><td class="diff-deletedline">' .
       $line.'</td>';
   }
 
   function emptyLine() {
-    //$line = str_replace('  ','&nbsp; ',$line);
     return '<td colspan="2">&nbsp;</td>';
   }
 
   function contextLine( $line ) {
-    $line = str_replace('  ',' &nbsp;',$line);
-    if($line{0} == ' ') $line = '&nbsp;'.substr($line,1);
     return '<td> </td><td class="diff-context">'.$line.'</td>';
   }
 
-- 
GitLab