Theme Inspinia
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jquery.pretty-text-diff.min.js 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Generated by CoffeeScript 1.7.1
  2. /*
  3. @preserve jQuery.PrettyTextDiff 1.0.4
  4. See https://github.com/arnab/jQuery.PrettyTextDiff/
  5. */
  6. (function() {
  7. var $;
  8. $ = jQuery;
  9. $.fn.extend({
  10. prettyTextDiff: function(options) {
  11. var dmp, settings;
  12. settings = {
  13. originalContainer: ".original",
  14. changedContainer: ".changed",
  15. diffContainer: ".diff",
  16. cleanup: true,
  17. debug: false
  18. };
  19. settings = $.extend(settings, options);
  20. $.fn.prettyTextDiff.debug("Options: ", settings, settings);
  21. dmp = new diff_match_patch();
  22. return this.each(function() {
  23. var changed, diff_as_html, diffs, original;
  24. if (settings.originalContent && settings.changedContent) {
  25. original = $('<div />').html(settings.originalContent).text();
  26. changed = $('<div />').html(settings.changedContent).text();
  27. } else {
  28. original = $(settings.originalContainer, this).text();
  29. changed = $(settings.changedContainer, this).text();
  30. }
  31. $.fn.prettyTextDiff.debug("Original text found: ", original, settings);
  32. $.fn.prettyTextDiff.debug("Changed text found: ", changed, settings);
  33. diffs = dmp.diff_main(original, changed);
  34. if (settings.cleanup) {
  35. dmp.diff_cleanupSemantic(diffs);
  36. }
  37. $.fn.prettyTextDiff.debug("Diffs: ", diffs, settings);
  38. diff_as_html = $.map(diffs, function(diff) {
  39. return $.fn.prettyTextDiff.createHTML(diff);
  40. });
  41. $(settings.diffContainer, this).html(diff_as_html.join(''));
  42. return this;
  43. });
  44. }
  45. });
  46. $.fn.prettyTextDiff.debug = function(message, object, settings) {
  47. if (settings.debug) {
  48. return console.log(message, object);
  49. }
  50. };
  51. $.fn.prettyTextDiff.createHTML = function(diff) {
  52. var data, html, operation, pattern_amp, pattern_gt, pattern_lt, pattern_para, text;
  53. html = [];
  54. pattern_amp = /&/g;
  55. pattern_lt = /</g;
  56. pattern_gt = />/g;
  57. pattern_para = /\n/g;
  58. operation = diff[0], data = diff[1];
  59. text = data.replace(pattern_amp, '&amp;').replace(pattern_lt, '&lt;').replace(pattern_gt, '&gt;').replace(pattern_para, '<br>');
  60. switch (operation) {
  61. case DIFF_INSERT:
  62. return '<ins>' + text + '</ins>';
  63. case DIFF_DELETE:
  64. return '<del>' + text + '</del>';
  65. case DIFF_EQUAL:
  66. return '<span>' + text + '</span>';
  67. }
  68. };
  69. }).call(this);