jquery.pretty-text-diff.js
2.99 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
// Generated by CoffeeScript 1.4.0
/*
@preserve jQuery.PrettyTextDiff 1.0.2
See https://github.com/arnab/jQuery.PrettyTextDiff/
Modified to show with and without HTML: Mark Dexter, Joomla Project.
*/
(function() {
var $;
$ = jQuery;
$.fn.extend({
prettyTextDiff: function(options) {
var dmp, settings;
settings = {
originalContainer: ".original",
changedContainer: ".changed",
diffContainer: ".diff",
originalHtmlContainer: ".originalhtml",
changedHtmlContainer: ".changedhtml",
diffHtmlContainer: ".diffhtml",
cleanup: true,
debug: false
};
settings = $.extend(settings, options);
$.fn.prettyTextDiff.debug("Options: ", settings, settings);
dmp = new diff_match_patch();
return this.each(function() {
var changed, diff_as_html, diffs, original, changedhtml, originalhtml, diffshtml;
original = $(settings.originalContainer, this).text();
$.fn.prettyTextDiff.debug("Original text found: ", original, settings);
changed = $(settings.changedContainer, this).text();
$.fn.prettyTextDiff.debug("Changed text found: ", changed, settings);
originalhtml = $(settings.originalHtmlContainer, this).text();
$.fn.prettyTextDiff.debug("Original text found: ", original, settings);
changedhtml = $(settings.changedHtmlContainer, this).text();
$.fn.prettyTextDiff.debug("Changed text found: ", changed, settings);
diffs = dmp.diff_main(original, changed);
diffshtml = dmp.diff_main(originalhtml, changedhtml);
if (settings.cleanup) {
dmp.diff_cleanupSemantic(diffs);
dmp.diff_cleanupSemantic(diffshtml);
}
$.fn.prettyTextDiff.debug("Diffs: ", diffs, settings);
diff_as_html = diffs.map(function(diff) {
return $.fn.prettyTextDiff.createHTML(diff);
});
diffhtml_as_html = diffshtml.map(function(diff) {
return $.fn.prettyTextDiff.createHTML(diff);
});
$(settings.diffContainer, this).html(diff_as_html.join(''));
$(settings.diffHtmlContainer, this).html(diffhtml_as_html.join(''));
return this;
});
}
});
$.fn.prettyTextDiff.debug = function(message, object, settings) {
if (settings.debug) {
return console.log(message, object);
}
};
$.fn.prettyTextDiff.createHTML = function(diff) {
var data, html, operation, pattern_amp, pattern_gt, pattern_lt, pattern_para, text;
html = [];
pattern_amp = /&/g;
pattern_lt = /</g;
pattern_gt = />/g;
pattern_para = /\n/g;
operation = diff[0], data = diff[1];
text = data.replace(pattern_amp, '&').replace(pattern_lt, '<').replace(pattern_gt, '>').replace(pattern_para, '<br>');
switch (operation) {
case DIFF_INSERT:
return '<ins>' + text + '</ins>';
case DIFF_DELETE:
return '<del>' + text + '</del>';
case DIFF_EQUAL:
return '<span>' + text + '</span>';
}
};
}).call(this);