forked from cosmiclattes/htmlDiff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmlDiff.js
More file actions
82 lines (78 loc) · 1.95 KB
/
htmlDiff.js
File metadata and controls
82 lines (78 loc) · 1.95 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
/**
* Based on the code by menway@gmail.com ,
* https://code.google.com/p/google-diff-match-patch/wiki/Plaintext
* The html tags are replaced by a unicode character , the diff is performed
* & then the unicode is converted back to html tags
*/
function htmlDiff() {
var _htmlHash;
var _revHtmlHash;
var _currentHash;
var _is_debug = false;
function pushHash(tag) {
if (typeof(_htmlHash[tag]) == 'undefined') {
var value = eval('"\\u'+_currentHash.toString(16)+'"');
_htmlHash[tag] = value;
_revHtmlHash[value] = tag;
_currentHash++;
}
return _htmlHash[tag];
}
this.clearHash = function() {
_htmlHash = {};
_revHtmlHash = {};
_currentHash = 44032; //朝鲜文音节 Hangul Syllables
};
function html2plain(html) {
html = html.replace(/<(S*?)[^>]*>.*?|<.*?\/>/g, function(tag){
//debug:
if (_is_debug) {
return pushHash(tag.toUpperCase().replace(/</g, '<').replace(/>/g, '>'));
} else {
return pushHash(tag.toUpperCase());
}
});
return html;
}
function plain2html(plain) {
var back;
for (i=0;i<plain.length;i++){
if(_revHtmlHash[plain[i]]){
back += _revHtmlHash[plain[i]];
}
else{
back += plain[i];
}
}
return back;
};
/*
function plain2html(plain) {
for(var tag in _htmlHash){
plain = plain.replace(RegExp(_htmlHash[tag], 'g'), tag);
}
return plain;
}*/
var dmp = new diff_match_patch();
this.diff = function(first,second){
var convertedFirst = html2plain(first);
var convertedSecond = html2plain(second);
var diffs = dmp.diff_main(convertedFirst,convertedSecond);
dmp.diff_cleanupSemantic(diffs);
var modified = '';
for (i=0;i<diffs.length;i++){
var diff = diffs[i];
if (diff[0]==0){
modified += diff[1];
}
else if (diff[0]==1){
modified += '<ins>'+diff[1]+'</ins>';
}
else {
modified += '<del>'+diff[1]+'</del>';
}
}
var complete = plain2html(modified);
return complete;
};
}