# HG changeset patch # User Mads Kiilerich # Date 2021-02-16 22:54:27 # Node ID 99ffd118f6af1935b812e2e7c154feb7c7b050e8 # Parent 51af759ef51866a54ea7fd9f32ed6ece6335f4da diffs: DiffParser should only HTML escape lines when generating HTML Move DiffParser HTML escaping so it only is applied when generating HTML. diff --git a/kallithea/lib/diffs.py b/kallithea/lib/diffs.py --- a/kallithea/lib/diffs.py +++ b/kallithea/lib/diffs.py @@ -402,9 +402,11 @@ class DiffProcessor(object): if not html: return _files - # highlight inline changes when one del is followed by one add for diff_data in _files: for chunk in diff_data['chunks']: + for change in chunk: + change['line'] = _escaper(change['line']) + # highlight inline changes when one del is followed by one add lineiter = iter(chunk) try: peekline = next(lineiter) @@ -447,44 +449,44 @@ _escape_re = re.compile(r'(&)|(<)|(>)|(\ def _escaper(diff_line): r""" - Do HTML escaping/markup of a single diff line (including first +/- column) + Do HTML escaping/markup of a single diff line (excluding first +/- column) >>> _escaper('foobar') 'foobar' >>> _escaper('@foo & bar') '@foo & bar' - >>> _escaper('+foo < bar') - '+foo < bar' - >>> _escaper('-foo > bar') - '-foo > bar' - >>> _escaper(' ') - ' <foo>' - >>> _escaper(' foo\tbar') - ' foo\tbar' - >>> _escaper(' foo\rbar\r') - ' foobar' - >>> _escaper(' foo\t') - ' foo\t' - >>> _escaper(' foo ') - ' foo ' - >>> _escaper(' foo ') - ' foo ' + >>> _escaper('foo < bar') + 'foo < bar' + >>> _escaper('foo > bar') + 'foo > bar' + >>> _escaper('') + '<foo>' + >>> _escaper('foo\tbar') + 'foo\tbar' + >>> _escaper('foo\rbar\r') + 'foobar' + >>> _escaper('foo\t') + 'foo\t' + >>> _escaper('foo ') + 'foo ' + >>> _escaper('foo ') + 'foo ' + >>> _escaper('') + '' >>> _escaper(' ') - ' ' - >>> _escaper(' ') - ' ' - >>> _escaper(' \t') - ' \t' - >>> _escaper(' \t ') - ' \t ' - >>> _escaper(' \t') - ' \t' - >>> _escaper(' \t\t ') - ' \t\t ' - >>> _escaper(' \t\t') - ' \t\t' - >>> _escaper(' foo&bar ') - ' foo&bar<baz> ' + ' ' + >>> _escaper('\t') + '\t' + >>> _escaper('\t ') + '\t ' + >>> _escaper(' \t') + ' \t' + >>> _escaper('\t\t ') + '\t\t ' + >>> _escaper(' \t\t') + ' \t\t' + >>> _escaper('foo&bar ') + 'foo&bar<baz> ' """ def substitute(m): @@ -502,8 +504,6 @@ def _escaper(diff_line): if groups[5]: return '' if groups[6]: - if m.start() == 0: - return ' ' # first column space shouldn't make empty lines show up as trailing space return ' ' assert False @@ -594,7 +594,7 @@ def _get_header(vcs, diff_chunk): if rest[-1:] != b'\n': # The diff will generally already have trailing \n (and be a memoryview). It might also be huge so we don't want to allocate it twice. But in this very rare case, we don't care. rest = bytes(rest) + b'\n' - diff_lines = (_escaper(safe_str(m.group(1))) for m in re.finditer(br'(.*)\n', rest)) + diff_lines = (safe_str(m.group(1)) for m in re.finditer(br'(.*)\n', rest)) return meta_info, diff_lines