diffview.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /***
  2. This is part of jsdifflib v1.0. <http://snowtide.com/jsdifflib>
  3. Copyright (c) 2007, Snowtide Informatics Systems, Inc.
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without modification,
  6. are permitted provided that the following conditions are met:
  7. * Redistributions of source code must retain the above copyright notice, this
  8. list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright notice,
  10. this list of conditions and the following disclaimer in the documentation
  11. and/or other materials provided with the distribution.
  12. * Neither the name of the Snowtide Informatics Systems nor the names of its
  13. contributors may be used to endorse or promote products derived from this
  14. software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  16. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  18. SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  20. TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  21. BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  23. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  24. DAMAGE.
  25. ***/
  26. /* Author: Chas Emerick <cemerick@snowtide.com> */
  27. /* Modified by Richard Bondi http://richardbondi.net */
  28. diffview = {
  29. /**
  30. * Builds and returns a visual diff view. The single parameter, `params', should contain
  31. * the following values:
  32. *
  33. * - baseTextLines: the array of strings that was used as the base text input to SequenceMatcher
  34. * - newTextLines: the array of strings that was used as the new text input to SequenceMatcher
  35. * - opcodes: the array of arrays returned by SequenceMatcher.get_opcodes()
  36. * - baseTextName: the title to be displayed above the base text listing in the diff view; defaults
  37. * to "Base Text"
  38. * - newTextName: the title to be displayed above the new text listing in the diff view; defaults
  39. * to "New Text"
  40. * - contextSize: the number of lines of context to show around differences; by default, all lines
  41. * are shown
  42. * - viewType: if 0, a side-by-side diff view is generated (default); if 1, an inline diff view is
  43. * generated
  44. */
  45. buildView: function (params) {
  46. var baseTextLines = params.baseTextLines;
  47. var newTextLines = params.newTextLines;
  48. var opcodes = params.opcodes;
  49. var baseTextName = params.baseTextName ? params.baseTextName : "Base Text";
  50. var newTextName = params.newTextName ? params.newTextName : "New Text";
  51. var contextSize = params.contextSize;
  52. var inline = (params.viewType == 0 || params.viewType == 1) ? params.viewType : 0;
  53. if (baseTextLines == null)
  54. throw "Cannot build diff view; baseTextLines is not defined.";
  55. if (newTextLines == null)
  56. throw "Cannot build diff view; newTextLines is not defined.";
  57. if (!opcodes)
  58. throw "Canno build diff view; opcodes is not defined.";
  59. function celt (name, clazz) {
  60. var e = document.createElement(name);
  61. e.className = clazz;
  62. return e;
  63. }
  64. function telt (name, text) {
  65. var e = document.createElement(name);
  66. e.appendChild(document.createTextNode(text));
  67. return e;
  68. }
  69. function ctelt (name, clazz, text) {
  70. var e = document.createElement(name);
  71. e.className = clazz;
  72. e.innerHTML = text;
  73. return e;
  74. }
  75. var tdata = document.createElement("thead");
  76. var node = document.createElement("tr");
  77. tdata.appendChild(node);
  78. if (inline) {
  79. node.appendChild(document.createElement("th"));
  80. node.appendChild(document.createElement("th"));
  81. node.appendChild(ctelt("th", "texttitle", baseTextName + " vs. " + newTextName));
  82. } else {
  83. node.appendChild(document.createElement("th"));
  84. node.appendChild(ctelt("th", "texttitle", baseTextName));
  85. node.appendChild(document.createElement("th"));
  86. node.appendChild(ctelt("th", "texttitle", newTextName));
  87. }
  88. tdata = [tdata];
  89. var rows = [];
  90. var node2;
  91. /**
  92. * Adds two cells to the given row; if the given row corresponds to a real
  93. * line number (based on the line index tidx and the endpoint of the
  94. * range in question tend), then the cells will contain the line number
  95. * and the line of text from textLines at position tidx (with the class of
  96. * the second cell set to the name of the change represented), and tidx + 1 will
  97. * be returned. Otherwise, tidx is returned, and two empty cells are added
  98. * to the given row.
  99. */
  100. function addCells (row, tidx, tend, textLines, change) {
  101. if (tidx < tend) {
  102. row.appendChild(telt("th", (tidx + 1).toString()));
  103. row.appendChild(ctelt("td", change, textLines[tidx].replace(/\t/g, "\u00a0\u00a0\u00a0\u00a0")));
  104. return tidx + 1;
  105. } else {
  106. row.appendChild(document.createElement("th"));
  107. row.appendChild(celt("td", "empty"));
  108. return tidx;
  109. }
  110. }
  111. function addCellsInline (row, tidx, tidx2, textLines, change) {
  112. row.appendChild(telt("th", tidx == null ? "" : (tidx + 1).toString()));
  113. row.appendChild(telt("th", tidx2 == null ? "" : (tidx2 + 1).toString()));
  114. row.appendChild(ctelt("td", change, textLines[tidx != null ? tidx : tidx2].replace(/\t/g, "\u00a0\u00a0\u00a0\u00a0")));
  115. }
  116. for (var idx = 0; idx < opcodes.length; idx++) {
  117. code = opcodes[idx];
  118. change = code[0];
  119. var b = code[1];
  120. var be = code[2];
  121. var n = code[3];
  122. var ne = code[4];
  123. var rowcnt = Math.max(be - b, ne - n);
  124. var toprows = [];
  125. var botrows = [];
  126. for (var i = 0; i < rowcnt; i++) {
  127. // jump ahead if we've alredy provided leading context or if this is the first range
  128. if (contextSize && opcodes.length > 1 && ((idx > 0 && i == contextSize) || (idx == 0 && i == 0)) && change=="equal") {
  129. var jump = rowcnt - ((idx == 0 ? 1 : 2) * contextSize);
  130. if (jump > 1) {
  131. toprows.push(node = document.createElement("tr"));
  132. b += jump;
  133. n += jump;
  134. i += jump - 1;
  135. node.appendChild(telt("th", "..."));
  136. if (!inline) node.appendChild(ctelt("td", "skip", ""));
  137. node.appendChild(telt("th", "..."));
  138. node.appendChild(ctelt("td", "skip", ""));
  139. // skip last lines if they're all equal
  140. if (idx + 1 == opcodes.length) {
  141. break;
  142. } else {
  143. continue;
  144. }
  145. }
  146. }
  147. toprows.push(node = document.createElement("tr"));
  148. if (inline) {
  149. if (change == "insert") {
  150. addCellsInline(node, null, n++, newTextLines, change);
  151. } else if (change == "replace") {
  152. botrows.push(node2 = document.createElement("tr"));
  153. if (b < be) addCellsInline(node, b++, null, baseTextLines, "delete");
  154. if (n < ne) addCellsInline(node2, null, n++, newTextLines, "insert");
  155. } else if (change == "delete") {
  156. addCellsInline(node, b++, null, baseTextLines, change);
  157. } else {
  158. // equal
  159. addCellsInline(node, b++, n++, baseTextLines, change);
  160. }
  161. } else {
  162. var wdiff = diffString2(b < be ? baseTextLines[b]:"", n < ne ? newTextLines[n]:"");
  163. if(b < be) baseTextLines[b] = wdiff.o;
  164. if(n < ne)newTextLines[n] = wdiff.n;
  165. b = addCells(node, b, be, baseTextLines, change=="replace" ? "delete" : change);
  166. n = addCells(node, n, ne, newTextLines, change=="replace" ? "insert" : change);
  167. }
  168. }
  169. for (var i = 0; i < toprows.length; i++) rows.push(toprows[i]);
  170. for (var i = 0; i < botrows.length; i++) rows.push(botrows[i]);
  171. }
  172. var msg = "combined <a href='http://snowtide.com/jsdifflib'>jsdifflib</a> ";
  173. msg += "and John Resig's <a href='http://ejohn.org/projects/javascript-diff-algorithm/'>diff</a> ";
  174. msg += "by <a href='http://richardbondi.net'>Richard Bondi</a>";
  175. rows.push(node = ctelt("th", "author", msg));
  176. node.setAttribute("colspan", inline ? 3 : 4);
  177. tdata.push(node = document.createElement("tbody"));
  178. for (var idx in rows) node.appendChild(rows[idx]);
  179. node = celt("table", "diff" + (inline ? " inlinediff" : ""));
  180. for (var idx in tdata) node.appendChild(tdata[idx]);
  181. return node;
  182. }
  183. }