-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHtmlDiffFormatter.cs
More file actions
420 lines (365 loc) · 11.9 KB
/
HtmlDiffFormatter.cs
File metadata and controls
420 lines (365 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
using System;
using System.Collections.Generic;
using System.Linq;
using csga5000.DiffMatchPatch;
using System.Text.RegularExpressions;
using HtmlAgilityPack;
namespace csga5000.HtmlDiffFomratter
{
public class HtmlDiffFormatter
{
private static TextSymbolReader<string> _reader;
public static TextSymbolReader<string> SymbolReader
{
get
{
if (_reader == null)
_reader = new TextSymbolReader<string>();
return _reader;
}
}
private static HtmlSymbolParser _writer;
public static HtmlSymbolParser SymbolParser
{
get
{
if (_writer == null)
_writer = new HtmlSymbolParser(true);
return _writer;
}
}
private static diff_match_patch<string> _dmp;
public static diff_match_patch<string> dmp {
get {
if (_dmp == null)
_dmp = new diff_match_patch<string>();
return _dmp;
}
}
public abstract class Formatter
{
public abstract string textForChange(string text, DiffMatchPatch.Operation op);
}
protected class DefaultFormatter : HtmlDiffFormatter.Formatter
{
string[] tableElements = new string[] { "<table", "<td", "<tr" };
string[] listElements = new string[] { "<li", "<ol", "<ul" };
string image = "<img";
string div = "<div";
bool addedAttribute = false;
HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
override public string textForChange(string text, DiffMatchPatch.Operation op)
{
if (text == "" || text == null)
return "";
html.LoadHtml(text);
var containsHtml = (html.DocumentNode.Descendants().Count() > 0);
if (containsHtml)
{
if (listElements.Any(text.Contains))
{
var listNodes = html.DocumentNode.Descendants().Where(n => n.Name == "li" || n.Name == "ol");
foreach (var li in listNodes)
{
addAttribute("class", Tuple.Create("styleDel", "styleIns"), op, li);
}
}
if (tableElements.Any(text.Contains))
{
var tableNodes = html.DocumentNode.Descendants().Where(t => t.Name == "table" || t.Name == "td" || t.Name == "tr");
foreach (var table in tableNodes)
{
if (table.Name == "table")
{
addAttribute("bordercolor", Tuple.Create("red", "green"), op, table);
}
else if (table.Name == "td")
{
addAttribute("class", Tuple.Create("tdDel", "tdIns"), op, table);
}
else
{
addAttribute("class", Tuple.Create("color: red", "color: green"), op, table);
}
}
}
if (text.Contains(div))
{
var divs = html.DocumentNode.Descendants().Where(d => d.Name == "div");
foreach (var div in divs)
{
addAttribute("class", Tuple.Create("styleDel", "styleIns"), op, div);
}
}
if (text.Contains(image))
{
var images = html.DocumentNode.Descendants().Where(t => t.Name == "img").ToList();
var imagefile = (op == Operation.INSERT) ? "image-ins" : "image-del";
var svg = "<img src='/Bundles/mco/images/" + imagefile + ".svg' class='svg' >";
foreach (var image in images)
{
addAttribute("class", Tuple.Create("imgDel", "imgIns"), op, image);
if(op != Operation.EQUAL)
image.ParentNode.InnerHtml = "<div class='img'>" + image.OuterHtml + "<br />" + svg + "</div>";
}
}
if (addedAttribute)
{
addedAttribute = false;
return html.DocumentNode.OuterHtml;
}
}
switch (op)
{
case DiffMatchPatch.Operation.DELETE:
return "<del style=\"text-decoration: line-through;color: red;\">" + text + "</del>";
case DiffMatchPatch.Operation.INSERT:
return "<ins style=\"text-decoration: underline;color: green;\">" + text + "</ins>";
default:
return text;
}
}
private void addAttribute(string key, Tuple<string, string> values, Operation op, HtmlNode element)
{
addedAttribute = true;
switch (op)
{
case DiffMatchPatch.Operation.DELETE:
element.Attributes.Add(key, values.Item1);
break;
case DiffMatchPatch.Operation.INSERT:
element.Attributes.Add(key, values.Item2);
break;
}
}
}
protected class DiffSeg
{
public static string[] SELF_CLOSING_TAGS = new string[] { "area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr", "!DOCTYPE" };
public bool tag = false;
public string tagName;
protected string text;
public bool startTag { get; set; }
public bool selfClosing { get; set; }
public DiffMatchPatch.Operation op;
protected List<DiffSeg> children;
public DiffSeg(Operation op, string text)
{
this.op = op;
this.text = text;
text = text.Trim();
startTag = true;
if (text == null || text.Length == 0)
return;
if (text.IndexOf("<!--") == 0) {
tagName = "<!--";
selfClosing = true;
tag = true;
}
else if (text[0] == '<')
{
tag = true;
tagName = "";
bool named = false;
foreach (var c in text)
{
if (!named)
{
if (c == '<')
continue;
if (c == '/')
{
if (tagName.Length > 0)
{
selfClosing = true;
named = true;
break;
}
else
startTag = false;
}
else if (c != ' ' && c != '>')
tagName += c;
else if ((c == ' ' && tagName.Length > 0) || c == '>')
named = true;
}
else
{
if (c == '/')
{
selfClosing = true;
break;
}
}
}
//These elements are *always* self closing, so we'll let them slide if they didn't put the "/"
if (SELF_CLOSING_TAGS.Contains(tagName))
selfClosing = true;
}
if (CanHaveChildren)
children = new List<DiffSeg>();
}
public bool CanHaveChildren
{
get
{
return tag && startTag;
}
}
public void addChild(DiffSeg seg)
{
if (!CanHaveChildren)
throw new Exception("Non-tag diff segments have no children");
this.children.Add(seg);
}
public List<DiffSeg> getChildren()
{
if (!CanHaveChildren)
throw new Exception("Non-tag diff segments have no children");
return this.children;
}
public bool hasText()
{
if (!CanHaveChildren)
throw new Exception("Diff segs that aren't tags *are* text!");
return this.children.Any(c => !c.tag || c.hasText());
}
public bool childrenMatch()
{
//If we have 1 or 0 children then we're just a tag with no children.
if (!CanHaveChildren || children.Count < 2)
return true;
return this.children.All(c => c.op == op && c.childrenMatch());
}
public string getText()
{
String text = this.text;
if (CanHaveChildren)
this.children.ForEach(c => text += c.getText());
return text;
}
public string getInnerText()
{
String text = "";
if (CanHaveChildren)
this.children.ForEach(c => text += c.getText());
return text;
}
protected string textForGroup(List<DiffSeg> group)
{
var groupText = "";
group.ForEach(ds => groupText += ds.getText());
return groupText;
}
public string getFormattedText(Formatter formatter)
{
String text = this.text;
if (CanHaveChildren && children.Count > 0)
{
if (childrenMatch())
{
return formatter.textForChange(getText(), op);
}
else
{
//We could just append the text of all the children's results t xmo "getFormattedText" but instead we group bits in sequence that are all the same operation.
Operation currop = this.op;
List<DiffSeg> sameOp = new List<DiffSeg>();
for (var i = 0; i < children.Count; i++)
{
var curr = children[i];
var canGroup = curr.childrenMatch();
if (sameOp.Count == 0 && canGroup) {
currop = curr.op;
sameOp.Add(curr);
}
else if (canGroup && curr.op == currop)
sameOp.Add(curr);
else
{
if (sameOp.Count > 0)
{
text += formatter.textForChange(textForGroup(sameOp), currop);
sameOp.Clear();
}
if (canGroup)
{
currop = curr.op;
sameOp.Add(curr);
}
else
text += curr.getFormattedText(formatter);
}
}
if (sameOp.Count > 0)
text += formatter.textForChange(textForGroup(sameOp), currop);
}
}
else if (!tag)
return formatter.textForChange(text, this.op);
return text;
}
}
Formatter formatter;
public HtmlDiffFormatter()
{
formatter = new DefaultFormatter();
}
public HtmlDiffFormatter(Formatter formatter)
{
this.formatter = formatter;
}
public static bool change;
protected int addChildren(DiffSeg seg, List<DiffSeg> segs, int index)
{
if (!seg.tag || seg.selfClosing || !seg.startTag)
return index;
List<DiffSeg> children = seg.getChildren();
children.Clear();
for (index++; index < segs.Count; index++)
{
var cseg = segs[index];
var ender = !cseg.startTag && cseg.tag;
children.Add(cseg);
if (ender)
{
//The differ tends to make previous ending tags be "changed" if the same tag is on the end of what was really changed, and the real closing tag is "the same"
//If the HTML is valid, then we can be certain that if we're at an ending tag at this point in our recursion, then it must be the case where it was marked incorrectly
seg.op = cseg.op;
return index;
}
index = addChildren(cseg, segs, index);
}
//This *should* mean bad html. So we'll hack an end tag in.
children.Add(new DiffSeg(seg.op, "</" + seg.tagName + ">"));
return index;
}
public string diffOutput(string startText, string endText)
{
var diffs = dmp.diff_main(SymbolParser.SymbolsFromText(startText), SymbolParser.SymbolsFromText(endText));
dmp.diff_cleanupSemantic(diffs);
return diffOutput(diffs);
}
public string diffOutput(List<Diff<string>> diffs)
{
var segs = new List<DiffSeg>();
diffs.ForEach(d => d.text.ForEach(t => segs.Add(new DiffSeg(d.operation, t.value))));
var rootSegs = new List<DiffSeg>();
//Setup tree matching html using recursion. This only matches the HTML as far as elements with the same operation go..
for (var index = 0; index < segs.Count; index++)
{
var seg = segs[index];
rootSegs.Add(seg);
index = addChildren(seg, segs, index);
}
//Build diff output
var output = "";
foreach (var seg in rootSegs)
{
//Since segments only contain the children that match their operation, all children are of the same operation.
output += seg.getFormattedText(formatter);
}
return output;
}
}
}