Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(163)

Side by Side Diff: third_party/WebKit/Tools/Scripts/webkitpy/common/html_diff.py

Issue 2695993002: hmtl_diff: Show only three lines before/after a diff block. (Closed)
Patch Set: apply review comments Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | third_party/WebKit/Tools/Scripts/webkitpy/common/html_diff_unittest.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2016 The Chromium Authors. All rights reserved. 1 # Copyright 2016 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 """Utility for outputting a HTML diff of two multi-line strings. 5 """Utility for outputting a HTML diff of two multi-line strings.
6 6
7 The main purpose of this utility is to show the difference between 7 The main purpose of this utility is to show the difference between
8 text baselines (-expected.txt files) and actual text results. 8 text baselines (-expected.txt files) and actual text results.
9 9
10 Note, in the standard library module difflib, there is also a HtmlDiff class, 10 Note, in the standard library module difflib, there is also a HtmlDiff class,
11 although it outputs a larger and more complex HTML table than we need. 11 although it outputs a larger and more complex HTML table than we need.
12 """ 12 """
13 13
14 import cgi 14 import cgi
15 import difflib 15 import difflib
16 16
17 _TEMPLATE = """<html> 17 _TEMPLATE = """<html>
18 <head> 18 <head>
19 <style> 19 <style>
20 pre { white-space: pre-wrap; } 20 table { white-space: pre-wrap; font-family: monospace; border-collapse: collapse ; }
21 th { color: #444; background: #eed; text-align: right; vertical-align: baseline; padding: 1px 4px 1px 4px; }
21 .del { background: #faa; } 22 .del { background: #faa; }
22 .add { background: #afa; } 23 .add { background: #afa; }
23 </style> 24 </style>
24 </head> 25 </head>
25 <body> 26 <body><table>%s</table></body>
26 <pre>%s</pre>
27 </body>
28 </html> 27 </html>
29 """ 28 """
30 29
31 30
32 def html_diff(a_text, b_text): 31 def html_diff(a_text, b_text):
33 """Returns a diff between two strings as HTML.""" 32 """Returns a diff between two strings as HTML."""
34 # Diffs can be between multiple text files of different encodings 33 # Diffs can be between multiple text files of different encodings
35 # so we always want to deal with them as byte arrays, not unicode strings. 34 # so we always want to deal with them as byte arrays, not unicode strings.
36 assert isinstance(a_text, str) 35 assert isinstance(a_text, str)
37 assert isinstance(b_text, str) 36 assert isinstance(b_text, str)
38 a_lines = a_text.splitlines(True) 37 a_lines = a_text.splitlines(True)
39 b_lines = b_text.splitlines(True) 38 b_lines = b_text.splitlines(True)
40 return _TEMPLATE % html_diff_body(a_lines, b_lines) 39 return _TEMPLATE % HtmlDiffGenerator().generate_tbody(a_lines, b_lines)
41 40
42 41
43 def html_diff_body(a_lines, b_lines): 42 class HtmlDiffGenerator(object):
44 matcher = difflib.SequenceMatcher(None, a_lines, b_lines)
45 output = []
46 for tag, a_start, a_end, b_start, b_end in matcher.get_opcodes():
47 a_chunk = ''.join(a_lines[a_start:a_end])
48 b_chunk = ''.join(b_lines[b_start:b_end])
49 output.append(_format_chunk(tag, a_chunk, b_chunk))
50 return ''.join(output)
51 43
44 def __init__(self):
45 self.a_line_no = None
46 self.b_line_no = None
47 self.a_lines_len = None
52 48
53 def _format_chunk(tag, a_chunk, b_chunk): 49 def generate_tbody(self, a_lines, b_lines):
54 if tag == 'delete': 50 self.a_line_no = 0
55 return _format_delete(a_chunk) 51 self.b_line_no = 0
56 if tag == 'insert': 52 self.a_lines_len = len(a_lines)
57 return _format_insert(b_chunk) 53 matcher = difflib.SequenceMatcher(None, a_lines, b_lines)
58 if tag == 'replace': 54 output = []
59 return _format_delete(a_chunk) + _format_insert(b_chunk) 55 for tag, a_start, a_end, b_start, b_end in matcher.get_opcodes():
60 assert tag == 'equal' 56 output.append(self._format_chunk(tag, a_lines[a_start:a_end], b_line s[b_start:b_end]))
61 return cgi.escape(a_chunk) 57 return ''.join(output)
62 58
59 def _format_chunk(self, tag, a_chunk, b_chunk):
60 if tag == 'delete':
61 return self._format_delete(a_chunk)
62 if tag == 'insert':
63 return self._format_insert(b_chunk)
64 if tag == 'replace':
65 return self._format_delete(a_chunk) + self._format_insert(b_chunk)
66 assert tag == 'equal'
67 return self._format_equal(a_chunk)
63 68
64 def _format_insert(chunk): 69 def _format_equal(self, common_chunk):
65 return '<span class="add">%s</span>' % cgi.escape(chunk) 70 output = ''
71 if len(common_chunk) <= 7:
72 for line in common_chunk:
73 output += self._format_equal_line(line)
74 else:
75 # Do not show context lines at the beginning of the file.
76 if self.a_line_no == 0:
77 self.a_line_no += 3
78 self.b_line_no += 3
79 else:
80 for line in common_chunk[0:3]:
81 output += self._format_equal_line(line)
82 self.a_line_no += len(common_chunk) - 6
83 self.b_line_no += len(common_chunk) - 6
84 output += '<tr><td colspan=3>\n\n</tr>'
85 # Do not show context lines at the end of the file.
86 if self.a_line_no + 3 != self.a_lines_len:
87 for line in common_chunk[len(common_chunk) - 3:len(common_chunk) ]:
88 output += self._format_equal_line(line)
89 return output
66 90
91 def _format_equal_line(self, line):
92 self.a_line_no += 1
93 self.b_line_no += 1
94 return '<tr><th>%d<th>%d<td>%s</tr>' % (self.a_line_no, self.b_line_no, cgi.escape(line))
67 95
68 def _format_delete(chunk): 96 def _format_insert(self, chunk):
69 return '<span class="del">%s</span>' % cgi.escape(chunk) 97 output = ''
98 for line in chunk:
99 self.b_line_no += 1
100 output += '<tr><th><th>%d<td class="add">%s</tr>' % (self.b_line_no, cgi.escape(line))
101 return output
102
103 def _format_delete(self, chunk):
104 output = ''
105 for line in chunk:
106 self.a_line_no += 1
107 output += '<tr><th>%d<th><td class="del">%s</tr>' % (self.a_line_no, cgi.escape(line))
108 return output
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/Tools/Scripts/webkitpy/common/html_diff_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698