diff --git a/kallithea/lib/diffs.py b/kallithea/lib/diffs.py
--- a/kallithea/lib/diffs.py
+++ b/kallithea/lib/diffs.py
@@ -453,7 +453,7 @@ class DiffProcessor(object):
return self.adds, self.removes
-_escape_re = re.compile(r'(&)|(<)|(>)|(\t)|(\r)|(?<=.)( \n| $)')
+_escape_re = re.compile(r'(&)|(<)|(>)|(\t)(\n|$)?|(\r)|(?<=.)( \n| $)')
def _escaper(string):
@@ -470,10 +470,12 @@ def _escaper(string):
if groups[2]:
return '>'
if groups[3]:
+ if groups[4] is not None: # end of line
+ return '\t'
return '\t'
- if groups[4]:
+ if groups[5]:
return ''
- if groups[5]:
+ if groups[6]:
return ' '
assert False
diff --git a/kallithea/tests/models/test_diff_parsers.py b/kallithea/tests/models/test_diff_parsers.py
--- a/kallithea/tests/models/test_diff_parsers.py
+++ b/kallithea/tests/models/test_diff_parsers.py
@@ -297,13 +297,13 @@ class TestDiffLib(base.TestController):
assert s == r'''
context ... ... '@@ -51,6 +51,13 @@\n'
unmod 51 51 '\tbegin();\n'
-unmod 52 52 '\t\n'
+unmod 52 52 '\t'
add 53 '\tint foo;\n'
add 54 '\tint bar; \n'
add 55 '\tint baz;\t\n'
add 56 '\tint space; '
-add 57 '\tint tab;\t\n'
-add 58 '\t\n'
+add 57 '\tint tab;\t'
+add 58 '\t'
unmod 59 53 ' '
del 54 '\t#define MAX_STEPS (48)\n'
add 60 '\t\n'