/
HtmlDiff.cs
280 lines (222 loc) · 10.4 KB
/
HtmlDiff.cs
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
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Text.RegularExpressions;
using Majestic12;
using Menees.DiffUtils;
namespace HtmlDiff
{
/// <summary>
/// Summary description for HtmlDiff
/// </summary>
public class HtmlDiff
{
private string _html1;
private string _html2;
private bool _areDifferent;
private string _diffedSource1 = "";
private string _diffedSource2 = "";
private string _diffedHtml1 = "";
private string _diffedHtml2 = "";
private string _addedKeywords = "";
public string Html1 { get { return _html1; } }
public string Html2 { get { return _html2; } }
public bool AreDifferent { get { return _areDifferent; } }
public string DiffedSource1 { get { return _diffedSource1; } }
public string DiffedSource2 { get { return _diffedSource2; } }
public string DiffedHtml1 { get { return _diffedHtml1; } }
public string DiffedHtml2 { get { return _diffedHtml2; } }
public HtmlDiff(string html1, string html2)
{
_html1 = html1;
_html2 = html2;
diff();
}
static class Tags
{
public static string delete = "<div style='background-color:#FF7070; display: inline; position: static'>";
public static string add = "<div style='background-color:#77FF7C; display: inline; position: static'>";
public static string changeDelete = "<div style='background-color:#FFC45F; display: inline; position: static'>";
public static string changeAdd = "<div style='background-color:#FCFF5B; display: inline; position: static'>";
public static string close = "</div>";
}
private static int[] AsciiToIntArray(string ascii)
{
System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding();
byte[] bytes = encoder.GetBytes(ascii.ToCharArray());
int[] intArr = new int[bytes.Length];
for (int i = 0; i < bytes.Length; i++)
intArr[i] = System.Convert.ToInt32(bytes[i]);
return intArr;
}
/// <summary>
/// Breaks up the string by newline
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private string[] toStringList(string str)
{
string[] arr = str.Split('\n');
return arr;
}
private HTMLchunk[] htmlParse(string str)
{
//return value
ArrayList ret = new ArrayList();
//init parser
Majestic12.HTMLparser parser = new Majestic12.HTMLparser();
//keep raw html because we need to reconstruct it
parser.bKeepRawHTML = true;
//keep text... this is for parsing just tags
parser.bTextMode = true;
//initialize to parse the string
parser.Init(str);
Majestic12.HTMLchunk chunk = null;
// we parse until returned chunk is null indicating we reached end of parsing
while ((chunk = parser.ParseNext()) != null)
{
//discard empty blocks for performance increase
if (chunk.oHTML.Trim() != "")
{
//hard copy the chunk
HTMLchunk clone = new HTMLchunk(false);
clone.oHTML = String.Copy(chunk.oHTML);
clone.oType = chunk.oType;
clone.sTag = String.Copy(chunk.sTag);
ret.Add(clone);
}
}
parser.CleanUp();
//return string array
return (HTMLchunk[])ret.ToArray(typeof(HTMLchunk));
}
private int[] hash(HTMLchunk[] chunks)
{
//return value
int[] hash = new int[chunks.Length];
//hash the chunks
Menees.DiffUtils.StringHasher hasher = new Menees.DiffUtils.StringHasher(Menees.DiffUtils.HashType.CRC32, true, true, 0);
for (int i = 0; i < chunks.Length; i++)
hash[i] = hasher.GetHashCode(chunks[i].oHTML);
return hash;
}
/// <summary>
/// Diffs two html strings and marks them up
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
private void diff()
{
//parse the html
HTMLchunk[] chunks1 = htmlParse(_html1);
HTMLchunk[] chunks2 = htmlParse(_html2);
//make a hash array of the chunks
int[] hash1 = hash(chunks1);
int[] hash2 = hash(chunks2);
//diff the hashes
Menees.DiffUtils.MyersDiff myerdiff = new Menees.DiffUtils.MyersDiff(hash1, hash2);
//get a collection of changes
EditScript edits = myerdiff.Execute();
//record if there are any differences
_areDifferent = (edits.Count > 0);
//markup changes
_diffedSource1 = getMarkedUpSource(chunks1, edits, true);
_diffedSource2 = getMarkedUpSource(chunks2, edits, false);
_diffedHtml1 = getMarkedUpHtml(chunks1, edits, true);
_diffedHtml2 = getMarkedUpHtml(chunks2, edits, false);
_addedKeywords = getKewords(chunks2, edits);
}
private static string getMarkedUpSource(HTMLchunk[] chunks, Menees.DiffUtils.EditScript edits, bool isOlderVersion)
{
string[] str = new string[chunks.Length];
//html encode the source so it wont render
for (int i = 0; i < str.Length; i++) str[i] = System.Web.HttpUtility.HtmlEncode(chunks[i].oHTML);
//get an iterator for the changes
System.Collections.IEnumerator it = edits.GetEnumerator();
while (it.MoveNext())
{
Menees.DiffUtils.Edit curr = (Menees.DiffUtils.Edit)it.Current;
int start = (isOlderVersion ? curr.StartA : curr.StartB);
switch (curr.Type)
{
case Menees.DiffUtils.EditType.Change:
//changes are marked as deletes in older version and adds in newer version
str[start] = (isOlderVersion ? Tags.changeDelete : Tags.changeAdd) + str[start];
str[start + curr.Length] += Tags.close;
break;
case Menees.DiffUtils.EditType.Delete:
//deletes are marked in the older version
if (isOlderVersion)
{
str[start] = Tags.delete + str[start];
str[start + curr.Length] += Tags.close;
}
break;
case Menees.DiffUtils.EditType.Insert:
//Inserts are marked in the newer version
if (!isOlderVersion)
{
str[start] = Tags.add + str[start];
str[start + curr.Length] += Tags.close;
}
break;
}
}
return String.Join("", str);
}
private static string getKewords(HTMLchunk[] chunks, Menees.DiffUtils.EditScript edits)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
//get an iterator for the changes
System.Collections.IEnumerator it = edits.GetEnumerator();
while (it.MoveNext())
{
Menees.DiffUtils.Edit curr = (Menees.DiffUtils.Edit)it.Current;
//append only new text additions to versionB
if (curr.Type == EditType.Insert || curr.Type == EditType.Change)
for (int i = 0; i < curr.Length; i++)
//append only text changes
if (chunks[curr.StartB + i].oType == HTMLchunkType.Text)
sb.Append(" " + chunks[curr.StartB + i].oHTML);
}
return sb.ToString();
}
private static string getMarkedUpHtml(HTMLchunk[] chunks, Menees.DiffUtils.EditScript edits, bool isOlderVersion)
{
string[] str = new string[chunks.Length];
for (int i = 0; i < str.Length; i++) str[i] = chunks[i].oHTML;
//get an iterator for the changes
System.Collections.IEnumerator it = edits.GetEnumerator();
//for now only mark up text nodes!!! this needs improvement
while (it.MoveNext())
{
Menees.DiffUtils.Edit curr = (Menees.DiffUtils.Edit)it.Current;
int start = (isOlderVersion ? curr.StartA : curr.StartB);
switch (curr.Type)
{
case Menees.DiffUtils.EditType.Change:
for (int i = 0; i < curr.Length; i++)
if (chunks[start + i].oType == HTMLchunkType.Text)
str[start + i] = (isOlderVersion ? Tags.changeDelete : Tags.changeAdd) + str[start + i] + Tags.close;
break;
case Menees.DiffUtils.EditType.Delete:
//deletes are marked in the older version
if (isOlderVersion)
for (int i = 0; i < curr.Length; i++)
if (chunks[start + i].oType == HTMLchunkType.Text)
str[start + i] = Tags.delete + str[start + i] + Tags.close;
break;
case Menees.DiffUtils.EditType.Insert:
//Inserts are marked in the newer version
if (!isOlderVersion)
for (int i = 0; i < curr.Length; i++)
if (chunks[start + i].oType == HTMLchunkType.Text)
str[start + i] = Tags.add + str[start + i] + Tags.close;
break;
}
}
return String.Join("", str);
}
}
}