Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

diff.js 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /**
  2. * This library modifies the diff-patch-match library by Neil Fraser
  3. * by removing the patch and match functionality and certain advanced
  4. * options in the diff function. The original license is as follows:
  5. *
  6. * ===
  7. *
  8. * Diff Match and Patch
  9. *
  10. * Copyright 2006 Google Inc.
  11. * http://code.google.com/p/google-diff-match-patch/
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License");
  14. * you may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. */
  25. /**
  26. * The data structure representing a diff is an array of tuples:
  27. * [[DIFF_DELETE, 'Hello'], [DIFF_INSERT, 'Goodbye'], [DIFF_EQUAL, ' world.']]
  28. * which means: delete 'Hello', add 'Goodbye' and keep ' world.'
  29. */
  30. var DIFF_DELETE = -1;
  31. var DIFF_INSERT = 1;
  32. var DIFF_EQUAL = 0;
  33. /**
  34. * Find the differences between two texts. Simplifies the problem by stripping
  35. * any common prefix or suffix off the texts before diffing.
  36. * @param {string} text1 Old string to be diffed.
  37. * @param {string} text2 New string to be diffed.
  38. * @param {Int|Object} [cursor_pos] Edit position in text1 or object with more info
  39. * @return {Array} Array of diff tuples.
  40. */
  41. function diff_main(text1, text2, cursor_pos, _fix_unicode) {
  42. // Check for equality
  43. if (text1 === text2) {
  44. if (text1) {
  45. return [[DIFF_EQUAL, text1]];
  46. }
  47. return [];
  48. }
  49. if (cursor_pos != null) {
  50. var editdiff = find_cursor_edit_diff(text1, text2, cursor_pos);
  51. if (editdiff) {
  52. return editdiff;
  53. }
  54. }
  55. // Trim off common prefix (speedup).
  56. var commonlength = diff_commonPrefix(text1, text2);
  57. var commonprefix = text1.substring(0, commonlength);
  58. text1 = text1.substring(commonlength);
  59. text2 = text2.substring(commonlength);
  60. // Trim off common suffix (speedup).
  61. commonlength = diff_commonSuffix(text1, text2);
  62. var commonsuffix = text1.substring(text1.length - commonlength);
  63. text1 = text1.substring(0, text1.length - commonlength);
  64. text2 = text2.substring(0, text2.length - commonlength);
  65. // Compute the diff on the middle block.
  66. var diffs = diff_compute_(text1, text2);
  67. // Restore the prefix and suffix.
  68. if (commonprefix) {
  69. diffs.unshift([DIFF_EQUAL, commonprefix]);
  70. }
  71. if (commonsuffix) {
  72. diffs.push([DIFF_EQUAL, commonsuffix]);
  73. }
  74. diff_cleanupMerge(diffs, _fix_unicode);
  75. return diffs;
  76. };
  77. /**
  78. * Find the differences between two texts. Assumes that the texts do not
  79. * have any common prefix or suffix.
  80. * @param {string} text1 Old string to be diffed.
  81. * @param {string} text2 New string to be diffed.
  82. * @return {Array} Array of diff tuples.
  83. */
  84. function diff_compute_(text1, text2) {
  85. var diffs;
  86. if (!text1) {
  87. // Just add some text (speedup).
  88. return [[DIFF_INSERT, text2]];
  89. }
  90. if (!text2) {
  91. // Just delete some text (speedup).
  92. return [[DIFF_DELETE, text1]];
  93. }
  94. var longtext = text1.length > text2.length ? text1 : text2;
  95. var shorttext = text1.length > text2.length ? text2 : text1;
  96. var i = longtext.indexOf(shorttext);
  97. if (i !== -1) {
  98. // Shorter text is inside the longer text (speedup).
  99. diffs = [
  100. [DIFF_INSERT, longtext.substring(0, i)],
  101. [DIFF_EQUAL, shorttext],
  102. [DIFF_INSERT, longtext.substring(i + shorttext.length)]
  103. ];
  104. // Swap insertions for deletions if diff is reversed.
  105. if (text1.length > text2.length) {
  106. diffs[0][0] = diffs[2][0] = DIFF_DELETE;
  107. }
  108. return diffs;
  109. }
  110. if (shorttext.length === 1) {
  111. // Single character string.
  112. // After the previous speedup, the character can't be an equality.
  113. return [[DIFF_DELETE, text1], [DIFF_INSERT, text2]];
  114. }
  115. // Check to see if the problem can be split in two.
  116. var hm = diff_halfMatch_(text1, text2);
  117. if (hm) {
  118. // A half-match was found, sort out the return data.
  119. var text1_a = hm[0];
  120. var text1_b = hm[1];
  121. var text2_a = hm[2];
  122. var text2_b = hm[3];
  123. var mid_common = hm[4];
  124. // Send both pairs off for separate processing.
  125. var diffs_a = diff_main(text1_a, text2_a);
  126. var diffs_b = diff_main(text1_b, text2_b);
  127. // Merge the results.
  128. return diffs_a.concat([[DIFF_EQUAL, mid_common]], diffs_b);
  129. }
  130. return diff_bisect_(text1, text2);
  131. };
  132. /**
  133. * Find the 'middle snake' of a diff, split the problem in two
  134. * and return the recursively constructed diff.
  135. * See Myers 1986 paper: An O(ND) Difference Algorithm and Its Variations.
  136. * @param {string} text1 Old string to be diffed.
  137. * @param {string} text2 New string to be diffed.
  138. * @return {Array} Array of diff tuples.
  139. * @private
  140. */
  141. function diff_bisect_(text1, text2) {
  142. // Cache the text lengths to prevent multiple calls.
  143. var text1_length = text1.length;
  144. var text2_length = text2.length;
  145. var max_d = Math.ceil((text1_length + text2_length) / 2);
  146. var v_offset = max_d;
  147. var v_length = 2 * max_d;
  148. var v1 = new Array(v_length);
  149. var v2 = new Array(v_length);
  150. // Setting all elements to -1 is faster in Chrome & Firefox than mixing
  151. // integers and undefined.
  152. for (var x = 0; x < v_length; x++) {
  153. v1[x] = -1;
  154. v2[x] = -1;
  155. }
  156. v1[v_offset + 1] = 0;
  157. v2[v_offset + 1] = 0;
  158. var delta = text1_length - text2_length;
  159. // If the total number of characters is odd, then the front path will collide
  160. // with the reverse path.
  161. var front = (delta % 2 !== 0);
  162. // Offsets for start and end of k loop.
  163. // Prevents mapping of space beyond the grid.
  164. var k1start = 0;
  165. var k1end = 0;
  166. var k2start = 0;
  167. var k2end = 0;
  168. for (var d = 0; d < max_d; d++) {
  169. // Walk the front path one step.
  170. for (var k1 = -d + k1start; k1 <= d - k1end; k1 += 2) {
  171. var k1_offset = v_offset + k1;
  172. var x1;
  173. if (k1 === -d || (k1 !== d && v1[k1_offset - 1] < v1[k1_offset + 1])) {
  174. x1 = v1[k1_offset + 1];
  175. } else {
  176. x1 = v1[k1_offset - 1] + 1;
  177. }
  178. var y1 = x1 - k1;
  179. while (
  180. x1 < text1_length && y1 < text2_length &&
  181. text1.charAt(x1) === text2.charAt(y1)
  182. ) {
  183. x1++;
  184. y1++;
  185. }
  186. v1[k1_offset] = x1;
  187. if (x1 > text1_length) {
  188. // Ran off the right of the graph.
  189. k1end += 2;
  190. } else if (y1 > text2_length) {
  191. // Ran off the bottom of the graph.
  192. k1start += 2;
  193. } else if (front) {
  194. var k2_offset = v_offset + delta - k1;
  195. if (k2_offset >= 0 && k2_offset < v_length && v2[k2_offset] !== -1) {
  196. // Mirror x2 onto top-left coordinate system.
  197. var x2 = text1_length - v2[k2_offset];
  198. if (x1 >= x2) {
  199. // Overlap detected.
  200. return diff_bisectSplit_(text1, text2, x1, y1);
  201. }
  202. }
  203. }
  204. }
  205. // Walk the reverse path one step.
  206. for (var k2 = -d + k2start; k2 <= d - k2end; k2 += 2) {
  207. var k2_offset = v_offset + k2;
  208. var x2;
  209. if (k2 === -d || (k2 !== d && v2[k2_offset - 1] < v2[k2_offset + 1])) {
  210. x2 = v2[k2_offset + 1];
  211. } else {
  212. x2 = v2[k2_offset - 1] + 1;
  213. }
  214. var y2 = x2 - k2;
  215. while (
  216. x2 < text1_length && y2 < text2_length &&
  217. text1.charAt(text1_length - x2 - 1) === text2.charAt(text2_length - y2 - 1)
  218. ) {
  219. x2++;
  220. y2++;
  221. }
  222. v2[k2_offset] = x2;
  223. if (x2 > text1_length) {
  224. // Ran off the left of the graph.
  225. k2end += 2;
  226. } else if (y2 > text2_length) {
  227. // Ran off the top of the graph.
  228. k2start += 2;
  229. } else if (!front) {
  230. var k1_offset = v_offset + delta - k2;
  231. if (k1_offset >= 0 && k1_offset < v_length && v1[k1_offset] !== -1) {
  232. var x1 = v1[k1_offset];
  233. var y1 = v_offset + x1 - k1_offset;
  234. // Mirror x2 onto top-left coordinate system.
  235. x2 = text1_length - x2;
  236. if (x1 >= x2) {
  237. // Overlap detected.
  238. return diff_bisectSplit_(text1, text2, x1, y1);
  239. }
  240. }
  241. }
  242. }
  243. }
  244. // Diff took too long and hit the deadline or
  245. // number of diffs equals number of characters, no commonality at all.
  246. return [[DIFF_DELETE, text1], [DIFF_INSERT, text2]];
  247. };
  248. /**
  249. * Given the location of the 'middle snake', split the diff in two parts
  250. * and recurse.
  251. * @param {string} text1 Old string to be diffed.
  252. * @param {string} text2 New string to be diffed.
  253. * @param {number} x Index of split point in text1.
  254. * @param {number} y Index of split point in text2.
  255. * @return {Array} Array of diff tuples.
  256. */
  257. function diff_bisectSplit_(text1, text2, x, y) {
  258. var text1a = text1.substring(0, x);
  259. var text2a = text2.substring(0, y);
  260. var text1b = text1.substring(x);
  261. var text2b = text2.substring(y);
  262. // Compute both diffs serially.
  263. var diffs = diff_main(text1a, text2a);
  264. var diffsb = diff_main(text1b, text2b);
  265. return diffs.concat(diffsb);
  266. };
  267. /**
  268. * Determine the common prefix of two strings.
  269. * @param {string} text1 First string.
  270. * @param {string} text2 Second string.
  271. * @return {number} The number of characters common to the start of each
  272. * string.
  273. */
  274. function diff_commonPrefix(text1, text2) {
  275. // Quick check for common null cases.
  276. if (!text1 || !text2 || text1.charAt(0) !== text2.charAt(0)) {
  277. return 0;
  278. }
  279. // Binary search.
  280. // Performance analysis: http://neil.fraser.name/news/2007/10/09/
  281. var pointermin = 0;
  282. var pointermax = Math.min(text1.length, text2.length);
  283. var pointermid = pointermax;
  284. var pointerstart = 0;
  285. while (pointermin < pointermid) {
  286. if (
  287. text1.substring(pointerstart, pointermid) ==
  288. text2.substring(pointerstart, pointermid)
  289. ) {
  290. pointermin = pointermid;
  291. pointerstart = pointermin;
  292. } else {
  293. pointermax = pointermid;
  294. }
  295. pointermid = Math.floor((pointermax - pointermin) / 2 + pointermin);
  296. }
  297. if (is_surrogate_pair_start(text1.charCodeAt(pointermid - 1))) {
  298. pointermid--;
  299. }
  300. return pointermid;
  301. };
  302. /**
  303. * Determine the common suffix of two strings.
  304. * @param {string} text1 First string.
  305. * @param {string} text2 Second string.
  306. * @return {number} The number of characters common to the end of each string.
  307. */
  308. function diff_commonSuffix(text1, text2) {
  309. // Quick check for common null cases.
  310. if (!text1 || !text2 || text1.slice(-1) !== text2.slice(-1)) {
  311. return 0;
  312. }
  313. // Binary search.
  314. // Performance analysis: http://neil.fraser.name/news/2007/10/09/
  315. var pointermin = 0;
  316. var pointermax = Math.min(text1.length, text2.length);
  317. var pointermid = pointermax;
  318. var pointerend = 0;
  319. while (pointermin < pointermid) {
  320. if (
  321. text1.substring(text1.length - pointermid, text1.length - pointerend) ==
  322. text2.substring(text2.length - pointermid, text2.length - pointerend)
  323. ) {
  324. pointermin = pointermid;
  325. pointerend = pointermin;
  326. } else {
  327. pointermax = pointermid;
  328. }
  329. pointermid = Math.floor((pointermax - pointermin) / 2 + pointermin);
  330. }
  331. if (is_surrogate_pair_end(text1.charCodeAt(text1.length - pointermid))) {
  332. pointermid--;
  333. }
  334. return pointermid;
  335. };
  336. /**
  337. * Do the two texts share a substring which is at least half the length of the
  338. * longer text?
  339. * This speedup can produce non-minimal diffs.
  340. * @param {string} text1 First string.
  341. * @param {string} text2 Second string.
  342. * @return {Array.<string>} Five element Array, containing the prefix of
  343. * text1, the suffix of text1, the prefix of text2, the suffix of
  344. * text2 and the common middle. Or null if there was no match.
  345. */
  346. function diff_halfMatch_(text1, text2) {
  347. var longtext = text1.length > text2.length ? text1 : text2;
  348. var shorttext = text1.length > text2.length ? text2 : text1;
  349. if (longtext.length < 4 || shorttext.length * 2 < longtext.length) {
  350. return null; // Pointless.
  351. }
  352. /**
  353. * Does a substring of shorttext exist within longtext such that the substring
  354. * is at least half the length of longtext?
  355. * Closure, but does not reference any external variables.
  356. * @param {string} longtext Longer string.
  357. * @param {string} shorttext Shorter string.
  358. * @param {number} i Start index of quarter length substring within longtext.
  359. * @return {Array.<string>} Five element Array, containing the prefix of
  360. * longtext, the suffix of longtext, the prefix of shorttext, the suffix
  361. * of shorttext and the common middle. Or null if there was no match.
  362. * @private
  363. */
  364. function diff_halfMatchI_(longtext, shorttext, i) {
  365. // Start with a 1/4 length substring at position i as a seed.
  366. var seed = longtext.substring(i, i + Math.floor(longtext.length / 4));
  367. var j = -1;
  368. var best_common = '';
  369. var best_longtext_a, best_longtext_b, best_shorttext_a, best_shorttext_b;
  370. while ((j = shorttext.indexOf(seed, j + 1)) !== -1) {
  371. var prefixLength = diff_commonPrefix(
  372. longtext.substring(i), shorttext.substring(j));
  373. var suffixLength = diff_commonSuffix(
  374. longtext.substring(0, i), shorttext.substring(0, j));
  375. if (best_common.length < suffixLength + prefixLength) {
  376. best_common = shorttext.substring(
  377. j - suffixLength, j) + shorttext.substring(j, j + prefixLength);
  378. best_longtext_a = longtext.substring(0, i - suffixLength);
  379. best_longtext_b = longtext.substring(i + prefixLength);
  380. best_shorttext_a = shorttext.substring(0, j - suffixLength);
  381. best_shorttext_b = shorttext.substring(j + prefixLength);
  382. }
  383. }
  384. if (best_common.length * 2 >= longtext.length) {
  385. return [
  386. best_longtext_a, best_longtext_b,
  387. best_shorttext_a, best_shorttext_b, best_common
  388. ];
  389. } else {
  390. return null;
  391. }
  392. }
  393. // First check if the second quarter is the seed for a half-match.
  394. var hm1 = diff_halfMatchI_(longtext, shorttext, Math.ceil(longtext.length / 4));
  395. // Check again based on the third quarter.
  396. var hm2 = diff_halfMatchI_(longtext, shorttext, Math.ceil(longtext.length / 2));
  397. var hm;
  398. if (!hm1 && !hm2) {
  399. return null;
  400. } else if (!hm2) {
  401. hm = hm1;
  402. } else if (!hm1) {
  403. hm = hm2;
  404. } else {
  405. // Both matched. Select the longest.
  406. hm = hm1[4].length > hm2[4].length ? hm1 : hm2;
  407. }
  408. // A half-match was found, sort out the return data.
  409. var text1_a, text1_b, text2_a, text2_b;
  410. if (text1.length > text2.length) {
  411. text1_a = hm[0];
  412. text1_b = hm[1];
  413. text2_a = hm[2];
  414. text2_b = hm[3];
  415. } else {
  416. text2_a = hm[0];
  417. text2_b = hm[1];
  418. text1_a = hm[2];
  419. text1_b = hm[3];
  420. }
  421. var mid_common = hm[4];
  422. return [text1_a, text1_b, text2_a, text2_b, mid_common];
  423. };
  424. /**
  425. * Reorder and merge like edit sections. Merge equalities.
  426. * Any edit section can move as long as it doesn't cross an equality.
  427. * @param {Array} diffs Array of diff tuples.
  428. * @param {boolean} fix_unicode Whether to normalize to a unicode-correct diff
  429. */
  430. function diff_cleanupMerge(diffs, fix_unicode) {
  431. diffs.push([DIFF_EQUAL, '']); // Add a dummy entry at the end.
  432. var pointer = 0;
  433. var count_delete = 0;
  434. var count_insert = 0;
  435. var text_delete = '';
  436. var text_insert = '';
  437. var commonlength;
  438. while (pointer < diffs.length) {
  439. if (pointer < diffs.length - 1 && !diffs[pointer][1]) {
  440. diffs.splice(pointer, 1);
  441. continue;
  442. }
  443. switch (diffs[pointer][0]) {
  444. case DIFF_INSERT:
  445. count_insert++;
  446. text_insert += diffs[pointer][1];
  447. pointer++;
  448. break;
  449. case DIFF_DELETE:
  450. count_delete++;
  451. text_delete += diffs[pointer][1];
  452. pointer++;
  453. break;
  454. case DIFF_EQUAL:
  455. var previous_equality = pointer - count_insert - count_delete - 1;
  456. if (fix_unicode) {
  457. // prevent splitting of unicode surrogate pairs. when fix_unicode is true,
  458. // we assume that the old and new text in the diff are complete and correct
  459. // unicode-encoded JS strings, but the tuple boundaries may fall between
  460. // surrogate pairs. we fix this by shaving off stray surrogates from the end
  461. // of the previous equality and the beginning of this equality. this may create
  462. // empty equalities or a common prefix or suffix. for example, if AB and AC are
  463. // emojis, `[[0, 'A'], [-1, 'BA'], [0, 'C']]` would turn into deleting 'ABAC' and
  464. // inserting 'AC', and then the common suffix 'AC' will be eliminated. in this
  465. // particular case, both equalities go away, we absorb any previous inequalities,
  466. // and we keep scanning for the next equality before rewriting the tuples.
  467. if (previous_equality >= 0 && ends_with_pair_start(diffs[previous_equality][1])) {
  468. var stray = diffs[previous_equality][1].slice(-1);
  469. diffs[previous_equality][1] = diffs[previous_equality][1].slice(0, -1);
  470. text_delete = stray + text_delete;
  471. text_insert = stray + text_insert;
  472. if (!diffs[previous_equality][1]) {
  473. // emptied out previous equality, so delete it and include previous delete/insert
  474. diffs.splice(previous_equality, 1);
  475. pointer--;
  476. var k = previous_equality - 1;
  477. if (diffs[k] && diffs[k][0] === DIFF_INSERT) {
  478. count_insert++;
  479. text_insert = diffs[k][1] + text_insert;
  480. k--;
  481. }
  482. if (diffs[k] && diffs[k][0] === DIFF_DELETE) {
  483. count_delete++;
  484. text_delete = diffs[k][1] + text_delete;
  485. k--;
  486. }
  487. previous_equality = k;
  488. }
  489. }
  490. if (starts_with_pair_end(diffs[pointer][1])) {
  491. var stray = diffs[pointer][1].charAt(0);
  492. diffs[pointer][1] = diffs[pointer][1].slice(1);
  493. text_delete += stray;
  494. text_insert += stray;
  495. }
  496. }
  497. if (pointer < diffs.length - 1 && !diffs[pointer][1]) {
  498. // for empty equality not at end, wait for next equality
  499. diffs.splice(pointer, 1);
  500. break;
  501. }
  502. if (text_delete.length > 0 || text_insert.length > 0) {
  503. // note that diff_commonPrefix and diff_commonSuffix are unicode-aware
  504. if (text_delete.length > 0 && text_insert.length > 0) {
  505. // Factor out any common prefixes.
  506. commonlength = diff_commonPrefix(text_insert, text_delete);
  507. if (commonlength !== 0) {
  508. if (previous_equality >= 0) {
  509. diffs[previous_equality][1] += text_insert.substring(0, commonlength);
  510. } else {
  511. diffs.splice(0, 0, [DIFF_EQUAL, text_insert.substring(0, commonlength)]);
  512. pointer++;
  513. }
  514. text_insert = text_insert.substring(commonlength);
  515. text_delete = text_delete.substring(commonlength);
  516. }
  517. // Factor out any common suffixes.
  518. commonlength = diff_commonSuffix(text_insert, text_delete);
  519. if (commonlength !== 0) {
  520. diffs[pointer][1] =
  521. text_insert.substring(text_insert.length - commonlength) + diffs[pointer][1];
  522. text_insert = text_insert.substring(0, text_insert.length - commonlength);
  523. text_delete = text_delete.substring(0, text_delete.length - commonlength);
  524. }
  525. }
  526. // Delete the offending records and add the merged ones.
  527. var n = count_insert + count_delete;
  528. if (text_delete.length === 0 && text_insert.length === 0) {
  529. diffs.splice(pointer - n, n);
  530. pointer = pointer - n;
  531. } else if (text_delete.length === 0) {
  532. diffs.splice(pointer - n, n, [DIFF_INSERT, text_insert]);
  533. pointer = pointer - n + 1;
  534. } else if (text_insert.length === 0) {
  535. diffs.splice(pointer - n, n, [DIFF_DELETE, text_delete]);
  536. pointer = pointer - n + 1;
  537. } else {
  538. diffs.splice(pointer - n, n, [DIFF_DELETE, text_delete], [DIFF_INSERT, text_insert]);
  539. pointer = pointer - n + 2;
  540. }
  541. }
  542. if (pointer !== 0 && diffs[pointer - 1][0] === DIFF_EQUAL) {
  543. // Merge this equality with the previous one.
  544. diffs[pointer - 1][1] += diffs[pointer][1];
  545. diffs.splice(pointer, 1);
  546. } else {
  547. pointer++;
  548. }
  549. count_insert = 0;
  550. count_delete = 0;
  551. text_delete = '';
  552. text_insert = '';
  553. break;
  554. }
  555. }
  556. if (diffs[diffs.length - 1][1] === '') {
  557. diffs.pop(); // Remove the dummy entry at the end.
  558. }
  559. // Second pass: look for single edits surrounded on both sides by equalities
  560. // which can be shifted sideways to eliminate an equality.
  561. // e.g: A<ins>BA</ins>C -> <ins>AB</ins>AC
  562. var changes = false;
  563. pointer = 1;
  564. // Intentionally ignore the first and last element (don't need checking).
  565. while (pointer < diffs.length - 1) {
  566. if (diffs[pointer - 1][0] === DIFF_EQUAL &&
  567. diffs[pointer + 1][0] === DIFF_EQUAL) {
  568. // This is a single edit surrounded by equalities.
  569. if (diffs[pointer][1].substring(diffs[pointer][1].length -
  570. diffs[pointer - 1][1].length) === diffs[pointer - 1][1]) {
  571. // Shift the edit over the previous equality.
  572. diffs[pointer][1] = diffs[pointer - 1][1] +
  573. diffs[pointer][1].substring(0, diffs[pointer][1].length -
  574. diffs[pointer - 1][1].length);
  575. diffs[pointer + 1][1] = diffs[pointer - 1][1] + diffs[pointer + 1][1];
  576. diffs.splice(pointer - 1, 1);
  577. changes = true;
  578. } else if (diffs[pointer][1].substring(0, diffs[pointer + 1][1].length) ==
  579. diffs[pointer + 1][1]) {
  580. // Shift the edit over the next equality.
  581. diffs[pointer - 1][1] += diffs[pointer + 1][1];
  582. diffs[pointer][1] =
  583. diffs[pointer][1].substring(diffs[pointer + 1][1].length) +
  584. diffs[pointer + 1][1];
  585. diffs.splice(pointer + 1, 1);
  586. changes = true;
  587. }
  588. }
  589. pointer++;
  590. }
  591. // If shifts were made, the diff needs reordering and another shift sweep.
  592. if (changes) {
  593. diff_cleanupMerge(diffs, fix_unicode);
  594. }
  595. };
  596. function is_surrogate_pair_start(charCode) {
  597. return charCode >= 0xD800 && charCode <= 0xDBFF;
  598. }
  599. function is_surrogate_pair_end(charCode) {
  600. return charCode >= 0xDC00 && charCode <= 0xDFFF;
  601. }
  602. function starts_with_pair_end(str) {
  603. return is_surrogate_pair_end(str.charCodeAt(0));
  604. }
  605. function ends_with_pair_start(str) {
  606. return is_surrogate_pair_start(str.charCodeAt(str.length - 1));
  607. }
  608. function remove_empty_tuples(tuples) {
  609. var ret = [];
  610. for (var i = 0; i < tuples.length; i++) {
  611. if (tuples[i][1].length > 0) {
  612. ret.push(tuples[i]);
  613. }
  614. }
  615. return ret;
  616. }
  617. function make_edit_splice(before, oldMiddle, newMiddle, after) {
  618. if (ends_with_pair_start(before) || starts_with_pair_end(after)) {
  619. return null;
  620. }
  621. return remove_empty_tuples([
  622. [DIFF_EQUAL, before],
  623. [DIFF_DELETE, oldMiddle],
  624. [DIFF_INSERT, newMiddle],
  625. [DIFF_EQUAL, after]
  626. ]);
  627. }
  628. function find_cursor_edit_diff(oldText, newText, cursor_pos) {
  629. // note: this runs after equality check has ruled out exact equality
  630. var oldRange = typeof cursor_pos === 'number' ?
  631. { index: cursor_pos, length: 0 } : cursor_pos.oldRange;
  632. var newRange = typeof cursor_pos === 'number' ?
  633. null : cursor_pos.newRange;
  634. // take into account the old and new selection to generate the best diff
  635. // possible for a text edit. for example, a text change from "xxx" to "xx"
  636. // could be a delete or forwards-delete of any one of the x's, or the
  637. // result of selecting two of the x's and typing "x".
  638. var oldLength = oldText.length;
  639. var newLength = newText.length;
  640. if (oldRange.length === 0 && (newRange === null || newRange.length === 0)) {
  641. // see if we have an insert or delete before or after cursor
  642. var oldCursor = oldRange.index;
  643. var oldBefore = oldText.slice(0, oldCursor);
  644. var oldAfter = oldText.slice(oldCursor);
  645. var maybeNewCursor = newRange ? newRange.index : null;
  646. editBefore: {
  647. // is this an insert or delete right before oldCursor?
  648. var newCursor = oldCursor + newLength - oldLength;
  649. if (maybeNewCursor !== null && maybeNewCursor !== newCursor) {
  650. break editBefore;
  651. }
  652. if (newCursor < 0 || newCursor > newLength) {
  653. break editBefore;
  654. }
  655. var newBefore = newText.slice(0, newCursor);
  656. var newAfter = newText.slice(newCursor);
  657. if (newAfter !== oldAfter) {
  658. break editBefore;
  659. }
  660. var prefixLength = Math.min(oldCursor, newCursor);
  661. var oldPrefix = oldBefore.slice(0, prefixLength);
  662. var newPrefix = newBefore.slice(0, prefixLength);
  663. if (oldPrefix !== newPrefix) {
  664. break editBefore;
  665. }
  666. var oldMiddle = oldBefore.slice(prefixLength);
  667. var newMiddle = newBefore.slice(prefixLength);
  668. return make_edit_splice(oldPrefix, oldMiddle, newMiddle, oldAfter);
  669. }
  670. editAfter: {
  671. // is this an insert or delete right after oldCursor?
  672. if (maybeNewCursor !== null && maybeNewCursor !== oldCursor) {
  673. break editAfter;
  674. }
  675. var cursor = oldCursor;
  676. var newBefore = newText.slice(0, cursor);
  677. var newAfter = newText.slice(cursor);
  678. if (newBefore !== oldBefore) {
  679. break editAfter;
  680. }
  681. var suffixLength = Math.min(oldLength - cursor, newLength - cursor);
  682. var oldSuffix = oldAfter.slice(oldAfter.length - suffixLength);
  683. var newSuffix = newAfter.slice(newAfter.length - suffixLength);
  684. if (oldSuffix !== newSuffix) {
  685. break editAfter;
  686. }
  687. var oldMiddle = oldAfter.slice(0, oldAfter.length - suffixLength);
  688. var newMiddle = newAfter.slice(0, newAfter.length - suffixLength);
  689. return make_edit_splice(oldBefore, oldMiddle, newMiddle, oldSuffix);
  690. }
  691. }
  692. if (oldRange.length > 0 && newRange && newRange.length === 0) {
  693. replaceRange: {
  694. // see if diff could be a splice of the old selection range
  695. var oldPrefix = oldText.slice(0, oldRange.index);
  696. var oldSuffix = oldText.slice(oldRange.index + oldRange.length);
  697. var prefixLength = oldPrefix.length;
  698. var suffixLength = oldSuffix.length;
  699. if (newLength < prefixLength + suffixLength) {
  700. break replaceRange;
  701. }
  702. var newPrefix = newText.slice(0, prefixLength);
  703. var newSuffix = newText.slice(newLength - suffixLength);
  704. if (oldPrefix !== newPrefix || oldSuffix !== newSuffix) {
  705. break replaceRange;
  706. }
  707. var oldMiddle = oldText.slice(prefixLength, oldLength - suffixLength);
  708. var newMiddle = newText.slice(prefixLength, newLength - suffixLength);
  709. return make_edit_splice(oldPrefix, oldMiddle, newMiddle, oldSuffix);
  710. }
  711. }
  712. return null;
  713. }
  714. function diff(text1, text2, cursor_pos) {
  715. // only pass fix_unicode=true at the top level, not when diff_main is
  716. // recursively invoked
  717. return diff_main(text1, text2, cursor_pos, true);
  718. }
  719. diff.INSERT = DIFF_INSERT;
  720. diff.DELETE = DIFF_DELETE;
  721. diff.EQUAL = DIFF_EQUAL;
  722. module.exports = diff;