Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

198 рядки
7.8 KiB

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