fastdiff.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global document */
  6. import fastDiff from '../src/fastdiff';
  7. import diff from '../src/diff';
  8. import diffToChanges from '../src/difftochanges';
  9. describe( 'fastDiff', () => {
  10. describe( 'input types', () => {
  11. it( 'should correctly handle strings', () => {
  12. const changes = fastDiff( '123', 'abc123' );
  13. expect( changes ).to.deep.equal( [ { index: 0, type: 'insert', values: [ 'a', 'b', 'c' ] } ] );
  14. } );
  15. it( 'should correctly handle arrays', () => {
  16. const changes = fastDiff( [ '1', '2', '3' ], [ 'a', 'b', 'c', '1', '2', '3' ] );
  17. expect( changes ).to.deep.equal( [ { index: 0, type: 'insert', values: [ 'a', 'b', 'c' ] } ] );
  18. } );
  19. it( 'should correctly handle node lists', () => {
  20. const el1 = document.createElement( 'p' );
  21. const el2 = document.createElement( 'h1' );
  22. el1.appendChild( document.createElement( 'span' ) );
  23. el1.appendChild( document.createElement( 'strong' ) );
  24. el2.appendChild( document.createElement( 'div' ) );
  25. el2.appendChild( document.createElement( 'strong' ) );
  26. const changes = fastDiff( el1.childNodes, el2.childNodes );
  27. expect( changes ).to.deep.equal( [
  28. { index: 0, type: 'insert', values: [ el2.childNodes[ 0 ], el2.childNodes[ 1 ] ] },
  29. { index: 2, type: 'delete', howMany: 2 }
  30. ] );
  31. } );
  32. } );
  33. describe( 'changes object', () => {
  34. it( 'should diff identical texts', () => {
  35. expectDiff( '123', '123', [] );
  36. } );
  37. it( 'should diff identical arrays', () => {
  38. expectDiff( [ '1', '2', '3' ], [ '1', '2', '3' ], [] );
  39. } );
  40. it( 'should diff arrays with custom comparator', () => {
  41. expectDiff( [ 'a', 'b', 'c' ], [ 'A', 'B', 'C' ], [], true, ( a, b ) => a.toLowerCase() === b.toLowerCase() );
  42. } );
  43. describe( 'insertion', () => {
  44. it( 'should diff if old text is empty', () => {
  45. expectDiff( '', '123', [ { index: 0, type: 'insert', values: [ '1', '2', '3' ] } ] );
  46. } );
  47. it( 'should diff if old array is empty', () => {
  48. expectDiff( [], [ '1', '2', '3' ], [ { index: 0, type: 'insert', values: [ '1', '2', '3' ] } ] );
  49. } );
  50. it( 'should diff insertion on the beginning', () => {
  51. expectDiff( '123', 'abc123', [ { index: 0, type: 'insert', values: [ 'a', 'b', 'c' ] } ] );
  52. } );
  53. it( 'should diff insertion on the beginning (repetitive substring)', () => {
  54. // Do not check compatibility with 'diffToChanges' as it generates:
  55. // [ { index: 0, type: 'insert', values: [ 'a', 'b' ] }, { index: 5, type: 'insert', values: [ 'c', '1', '2', '3' ] } ]
  56. expectDiff( '123', 'ab123c123', [ { index: 0, type: 'insert', values: [ 'a', 'b', '1', '2', '3', 'c' ] } ], false );
  57. } );
  58. it( 'should diff insertion on the end', () => {
  59. expectDiff( '123', '123abc', [ { index: 3, type: 'insert', values: [ 'a', 'b', 'c' ] } ] );
  60. } );
  61. it( 'should diff insertion on the end (repetitive substring)', () => {
  62. expectDiff( '123', '123ab123c', [ { index: 3, type: 'insert', values: [ 'a', 'b', '1', '2', '3', 'c' ] } ] );
  63. } );
  64. it( 'should diff insertion in the middle', () => {
  65. expectDiff( '123', '12abc3', [ { index: 2, type: 'insert', values: [ 'a', 'b', 'c' ] } ] );
  66. } );
  67. it( 'should diff insertion in the middle (repetitive substring)', () => {
  68. // Do not check compatibility with 'diffToChanges' as it generates:
  69. // [ { index: 2, type: 'insert', values: [ 'a', 'b', '1', '2' ] }, { index: 7, type: 'insert', values: [ 'c', '3' ] } ]
  70. expectDiff( '123', '12ab123c3', [ { index: 2, type: 'insert', values: [ 'a', 'b', '1', '2', '3', 'c' ] } ], false );
  71. } );
  72. it( 'should diff insertion of duplicated content', () => {
  73. expectDiff( '123', '123123', [ { index: 3, type: 'insert', values: [ '1', '2', '3' ] } ] );
  74. } );
  75. it( 'should diff insertion of partially duplicated content', () => {
  76. expectDiff( '123', '12323', [ { index: 3, type: 'insert', values: [ '2', '3' ] } ] );
  77. } );
  78. it( 'should diff insertion on both boundaries', () => {
  79. // Do not check compatibility with 'diffToChanges' as it generates:
  80. // [ { index: 2, type: 'insert', values: [ 'a', 'b' ] }, { index: 5, type: 'insert', values: [ 'c' ] } ]
  81. expectDiff( '123', 'ab123c', [
  82. { index: 0, type: 'insert', values: [ 'a', 'b', '1', '2', '3', 'c' ] },
  83. { index: 6, type: 'delete', howMany: 3 }
  84. ], false );
  85. } );
  86. it( 'should diff insertion in array of objects', () => {
  87. const o1 = { foo: 1 };
  88. const o2 = { bar: 2 };
  89. expectDiff( [ o1, o2 ], [ o1, o2, { baz: 3 } ], [
  90. { index: 2, type: 'insert', values: [ { baz: 3 } ] }
  91. ] );
  92. } );
  93. it( 'should diff insertion in array of objects with comparator', () => {
  94. expectDiff( [ { text: 'foo' }, { text: 'bar' } ], [ { text: 'foo' }, { text: 'bar' }, { text: 'baz' } ], [
  95. { index: 2, type: 'insert', values: [ { text: 'baz' } ] }
  96. ], true, ( a, b ) => a.text === b.text );
  97. } );
  98. it( 'should diff insertion on the end handle multi-byte unicode properly', () => {
  99. expectDiff( '123🙂', '123🙂x', [ { index: 5, type: 'insert', values: [ 'x' ] } ] );
  100. } );
  101. } );
  102. describe( 'deletion', () => {
  103. it( 'should diff if new text is empty', () => {
  104. expectDiff( '123', '', [ { index: 0, type: 'delete', howMany: 3 } ] );
  105. } );
  106. it( 'should diff if new array is empty', () => {
  107. expectDiff( [ '1', '2', '3' ], [], [ { index: 0, type: 'delete', howMany: 3 } ] );
  108. } );
  109. it( 'should diff deletion on the beginning', () => {
  110. expectDiff( 'abc123', '123', [ { index: 0, type: 'delete', howMany: 3 } ] );
  111. } );
  112. it( 'should diff deletion on the beginning (repetitive substring)', () => {
  113. // Do not check compatibility with 'diffToChanges' as it generates:
  114. // [ { index: 0, type: 'delete', howMany: 2 }, { index: 3, type: 'delete', howMany: 4 } ]
  115. expectDiff( 'ab123c123', '123', [ { index: 0, type: 'delete', howMany: 6 } ], false );
  116. } );
  117. it( 'should diff deletion on the end', () => {
  118. expectDiff( '123abc', '123', [ { index: 3, type: 'delete', howMany: 3 } ] );
  119. } );
  120. it( 'should diff deletion on the end (repetitive substring)', () => {
  121. expectDiff( '123ab123c', '123', [ { index: 3, type: 'delete', howMany: 6 } ] );
  122. } );
  123. it( 'should diff deletion in the middle', () => {
  124. expectDiff( '12abc3', '123', [ { index: 2, type: 'delete', howMany: 3 } ] );
  125. } );
  126. it( 'should diff deletion in the middle (repetitive substring)', () => {
  127. // Do not check compatibility with 'diffToChanges' as it generates:
  128. // [ { index: 2, type: 'delete', howMany: 4 }, { index: 3, type: 'delete', howMany: 2 } ]
  129. expectDiff( '12ab123c3', '123', [ { index: 2, type: 'delete', howMany: 6 } ], false );
  130. } );
  131. it( 'should diff deletion on both boundaries', () => {
  132. // Do not check compatibility with 'diffToChanges' as it generates:
  133. // [ { index: 0, type: 'delete', howMany: 1 }, { index: 3, type: 'delete', howMany: 2 } ]
  134. expectDiff( '12abc3', '2ab', [
  135. { index: 0, type: 'insert', values: [ '2', 'a', 'b' ] },
  136. { index: 3, type: 'delete', howMany: 6 }
  137. ], false );
  138. } );
  139. it( 'should diff deletion of duplicated content', () => {
  140. expectDiff( '123123', '123', [ { index: 3, type: 'delete', howMany: 3 } ] );
  141. } );
  142. it( 'should diff deletion of partially duplicated content', () => {
  143. expectDiff( '12323', '123', [ { index: 3, type: 'delete', howMany: 2 } ] );
  144. } );
  145. it( 'should diff deletion of partially duplicated content 2', () => {
  146. // Do not check compatibility with 'diffToChanges' as it generates:
  147. // [ { index: 1, type: 'delete', howMany: 2 }, { index: 2, type: 'delete', howMany: 1 } ]
  148. expectDiff( '11233', '13', [ { index: 1, type: 'delete', howMany: 3 } ], false );
  149. } );
  150. it( 'should diff deletion in array of objects', () => {
  151. const o1 = { foo: 1 };
  152. const o2 = { bar: 2 };
  153. expectDiff( [ o1, o2 ], [ o2 ], [
  154. { index: 0, type: 'delete', howMany: 1 }
  155. ] );
  156. } );
  157. it( 'should diff insertion in array of objects with comparator', () => {
  158. expectDiff( [ { text: 'foo' }, { text: 'bar' } ], [ { text: 'bar' } ], [
  159. { index: 0, type: 'delete', howMany: 1 }
  160. ], true, ( a, b ) => a.text === b.text );
  161. } );
  162. } );
  163. describe( 'replacement', () => {
  164. it( 'should diff replacement of entire text', () => {
  165. // Do not check compatibility with 'diffToChanges' as it has changes in reveres order ('delete', 'insert') here.
  166. expectDiff( '12345', 'abcd', [
  167. { index: 0, type: 'insert', values: [ 'a', 'b', 'c', 'd' ] },
  168. { index: 4, type: 'delete', howMany: 5 }
  169. ], false );
  170. } );
  171. it( 'should diff replacement on the beginning', () => {
  172. expectDiff( '12345', 'abcd345', [
  173. { index: 0, type: 'insert', values: [ 'a', 'b', 'c', 'd' ] },
  174. { index: 4, type: 'delete', howMany: 2 }
  175. ] );
  176. } );
  177. it( 'should diff replacement on the beginning (repetitive substring)', () => {
  178. // Do not check compatibility with 'diffToChanges' as it has changes in reveres order ('delete', 'insert') here.
  179. expectDiff( '12345', '345345', [
  180. { index: 0, type: 'insert', values: [ '3', '4', '5' ] },
  181. { index: 3, type: 'delete', howMany: 2 }
  182. ], false );
  183. } );
  184. it( 'should diff replacement on the end', () => {
  185. // Do not check compatibility with 'diffToChanges' as it has changes in reveres order ('delete', 'insert') here.
  186. expectDiff( '12345', '12ab', [
  187. { index: 2, type: 'insert', values: [ 'a', 'b' ] },
  188. { index: 4, type: 'delete', howMany: 3 }
  189. ], false );
  190. } );
  191. it( 'should diff replacement on the end (repetitive substring)', () => {
  192. // Do not check compatibility with 'diffToChanges' as it generates:
  193. // [ { index: 3, type: 'insert', values: [ '1', '2', '3' ] }, { index: 7, type: 'delete', howMany: 1 } ]
  194. expectDiff( '12345', '1231234', [
  195. { index: 3, type: 'insert', values: [ '1', '2', '3', '4' ] },
  196. { index: 7, type: 'delete', howMany: 2 }
  197. ], false );
  198. } );
  199. it( 'should diff insertion of duplicated content', () => {
  200. expectDiff( '1234', '123123', [
  201. { index: 3, type: 'insert', values: [ '1', '2', '3' ] },
  202. { index: 6, type: 'delete', howMany: 1 }
  203. ], false );
  204. } );
  205. it( 'should diff insertion of duplicated content', () => {
  206. expectDiff( '1234', '13424', [
  207. { index: 1, type: 'insert', values: [ '3', '4', '2' ] },
  208. { index: 4, type: 'delete', howMany: 2 }
  209. ], false );
  210. } );
  211. it( 'should diff replacement in the middle', () => {
  212. expectDiff( '12345', '12ab5', [
  213. { index: 2, type: 'insert', values: [ 'a', 'b' ] },
  214. { index: 4, type: 'delete', howMany: 2 }
  215. ] );
  216. } );
  217. it( 'should diff replacement in the middle (repetitive substring)', () => {
  218. // Do not check compatibility with 'diffToChanges' as it generates:
  219. // [ { index: 2, type: 'insert', values: [ '1', '2' ] }, { index: 7, type: 'insert', values: [ '5' ] } ]
  220. expectDiff( '12345', '12123455', [
  221. { index: 2, type: 'insert', values: [ '1', '2', '3', '4', '5' ] },
  222. { index: 7, type: 'delete', howMany: 2 }
  223. ], false );
  224. } );
  225. it( 'should diff replacement of duplicated content', () => {
  226. // Do not check compatibility with 'diffToChanges' as it has changes in reveres order ('delete', 'insert') here.
  227. expectDiff( '123123', '123333', [
  228. { index: 3, type: 'insert', values: '33'.split( '' ) },
  229. { index: 5, type: 'delete', howMany: 2 }
  230. ], false );
  231. } );
  232. it( 'should diff replacement in array of objects', () => {
  233. const o1 = { foo: 1 };
  234. const o2 = { bar: 2 };
  235. expectDiff( [ o1, o2 ], [ o1, { baz: 3 } ], [
  236. { index: 1, type: 'insert', values: [ { baz: 3 } ] },
  237. { index: 2, type: 'delete', howMany: 1 }
  238. ] );
  239. } );
  240. it( 'should diff insertion in array of objects with comparator', () => {
  241. expectDiff( [ { text: 'foo' }, { text: 'bar' } ], [ { text: 'foo' }, { text: 'baz' } ], [
  242. { index: 1, type: 'insert', values: [ { text: 'baz' } ] },
  243. { index: 2, type: 'delete', howMany: 1 }
  244. ], true, ( a, b ) => a.text === b.text );
  245. } );
  246. } );
  247. } );
  248. describe( 'changes linear', () => {
  249. it( 'should diff identical texts', () => {
  250. expectDiffLinear( '123', '123', 'eee' );
  251. } );
  252. it( 'should diff identical arrays', () => {
  253. expectDiffLinear( [ '1', '2', '3' ], [ '1', '2', '3' ], 'eee' );
  254. } );
  255. it( 'should diff arrays with custom comparator', () => {
  256. expectDiffLinear( [ 'a', 'b', 'c' ], [ 'A', 'B', 'C' ], 'eee', true, ( a, b ) => a.toLowerCase() === b.toLowerCase() );
  257. } );
  258. describe( 'insertion', () => {
  259. it( 'should diff if old text is empty', () => {
  260. expectDiffLinear( '', '123', 'iii' );
  261. } );
  262. it( 'should diff if old array is empty', () => {
  263. expectDiffLinear( [], [ '1', '2', '3' ], 'iii' );
  264. } );
  265. it( 'should diff insertion on the beginning', () => {
  266. expectDiffLinear( '123', 'abc123', 'iiieee' );
  267. } );
  268. it( 'should diff insertion on the beginning (repetitive substring)', () => {
  269. expectDiffLinear( '123', 'ab123c123', 'iiiiiieee', false );
  270. } );
  271. it( 'should diff insertion on the end', () => {
  272. expectDiffLinear( '123', '123abc', 'eeeiii' );
  273. } );
  274. it( 'should diff insertion on the end (repetitive substring)', () => {
  275. expectDiffLinear( '123', '123ab123c', 'eeeiiiiii' );
  276. } );
  277. it( 'should diff insertion in the middle', () => {
  278. expectDiffLinear( '123', '12abc3', 'eeiiie' );
  279. } );
  280. it( 'should diff insertion in the middle (repetitive substring)', () => {
  281. expectDiffLinear( '123', '12ab123c3', 'eeiiiiiie', false );
  282. } );
  283. it( 'should diff insertion of duplicated content', () => {
  284. expectDiffLinear( '123', '123123', 'eeeiii' );
  285. } );
  286. it( 'should diff insertion of partially duplicated content', () => {
  287. expectDiffLinear( '123', '12323', 'eeeii' );
  288. } );
  289. it( 'should diff insertion on both boundaries', () => {
  290. expectDiffLinear( '123', 'ab123c', 'iiiiiiddd', false );
  291. } );
  292. it( 'should diff insertion in array of objects', () => {
  293. const o1 = { foo: 1 };
  294. const o2 = { bar: 2 };
  295. expectDiffLinear( [ o1, o2 ], [ o1, o2, { baz: 3 } ], 'eei' );
  296. } );
  297. it( 'should diff insertion in array of objects with comparator', () => {
  298. expectDiffLinear( [ { text: 'foo' }, { text: 'bar' } ], [ { text: 'foo' }, { text: 'bar' }, { text: 'baz' } ],
  299. 'eei', true, ( a, b ) => a.text === b.text );
  300. } );
  301. } );
  302. describe( 'deletion', () => {
  303. it( 'should diff if new text is empty', () => {
  304. expectDiffLinear( '123', '', 'ddd' );
  305. } );
  306. it( 'should diff if new array is empty', () => {
  307. expectDiffLinear( [ '1', '2', '3' ], [], 'ddd' );
  308. } );
  309. it( 'should diff deletion on the beginning', () => {
  310. expectDiffLinear( 'abc123', '123', 'dddeee' );
  311. } );
  312. it( 'should diff deletion on the beginning (repetitive substring)', () => {
  313. expectDiffLinear( 'ab123c123', '123', 'ddddddeee', false );
  314. } );
  315. it( 'should diff deletion on the end', () => {
  316. expectDiffLinear( '123abc', '123', 'eeeddd' );
  317. } );
  318. it( 'should diff deletion on the end (repetitive substring)', () => {
  319. expectDiffLinear( '123ab123c', '123', 'eeedddddd' );
  320. } );
  321. it( 'should diff deletion in the middle', () => {
  322. expectDiffLinear( '12abc3', '123', 'eeddde' );
  323. } );
  324. it( 'should diff deletion in the middle (repetitive substring)', () => {
  325. expectDiffLinear( '12ab123c3', '123', 'eedddddde', false );
  326. } );
  327. it( 'should diff deletion on both boundaries', () => {
  328. expectDiffLinear( '12abc3', '2ab', 'iiidddddd', false );
  329. } );
  330. it( 'should diff deletion of duplicated content', () => {
  331. expectDiffLinear( '123123', '123', 'eeeddd' );
  332. } );
  333. it( 'should diff deletion of partially duplicated content', () => {
  334. expectDiffLinear( '12323', '123', 'eeedd' );
  335. } );
  336. it( 'should diff deletion of partially duplicated content 2', () => {
  337. expectDiffLinear( '11233', '13', 'eddde', false );
  338. } );
  339. it( 'should diff deletion in array of objects', () => {
  340. const o1 = { foo: 1 };
  341. const o2 = { bar: 2 };
  342. expectDiffLinear( [ o1, o2 ], [ o2 ], 'de' );
  343. } );
  344. it( 'should diff insertion in array of objects with comparator', () => {
  345. expectDiffLinear( [ { text: 'foo' }, { text: 'bar' } ], [ { text: 'bar' } ], 'de', true, ( a, b ) => a.text === b.text );
  346. } );
  347. } );
  348. describe( 'replacement', () => {
  349. it( 'should diff replacement of entire text', () => {
  350. expectDiffLinear( '12345', 'abcd', 'iiiiddddd', false );
  351. } );
  352. it( 'should diff replacement on the beginning', () => {
  353. expectDiffLinear( '12345', 'abcd345', 'iiiiddeee' );
  354. } );
  355. it( 'should diff replacement on the beginning (repetitive substring)', () => {
  356. expectDiffLinear( '12345', '345345', 'iiiddeee', false );
  357. } );
  358. it( 'should diff replacement on the end', () => {
  359. expectDiffLinear( '12345', '12ab', 'eeiiddd', false );
  360. } );
  361. it( 'should diff replacement on the end (repetitive substring)', () => {
  362. expectDiffLinear( '12345', '1231234', 'eeeiiiidd', false );
  363. } );
  364. it( 'should diff insertion of duplicated content', () => {
  365. expectDiffLinear( '1234', '123123', 'eeeiiid' );
  366. } );
  367. it( 'should diff insertion of duplicated content', () => {
  368. expectDiffLinear( '1234', '13424', 'eiiidde', false );
  369. } );
  370. it( 'should diff replacement in the middle', () => {
  371. expectDiffLinear( '12345', '12ab5', 'eeiidde' );
  372. } );
  373. it( 'should diff replacement in the middle (repetitive substring)', () => {
  374. expectDiffLinear( '12345', '12123455', 'eeiiiiidde', false );
  375. } );
  376. it( 'should diff replacement of duplicated content', () => {
  377. expectDiffLinear( '123123', '123333', 'eeeiidde', false );
  378. } );
  379. it( 'should diff replacement in array of objects', () => {
  380. const o1 = { foo: 1 };
  381. const o2 = { bar: 2 };
  382. expectDiffLinear( [ o1, o2 ], [ o1, { baz: 3 } ], 'eid' );
  383. } );
  384. it( 'should diff insertion in array of objects with comparator', () => {
  385. expectDiffLinear( [ { text: 'foo' }, { text: 'bar' } ], [ { text: 'foo' }, { text: 'baz' } ], 'eid',
  386. true, ( a, b ) => a.text === b.text );
  387. } );
  388. } );
  389. } );
  390. } );
  391. function expectDiff( oldText, newText, expected, checkDiffToChangesCompatibility = true, comparator = null ) {
  392. const result = fastDiff( oldText, newText, comparator );
  393. expect( result ).to.deep.equals( expected, 'fastDiff changes failed' );
  394. if ( checkDiffToChangesCompatibility ) {
  395. expect( result ).to.deep.equals(
  396. diffToChanges( diff( oldText, newText, comparator ), newText ), 'diffToChanges compatibility failed' );
  397. }
  398. }
  399. function expectDiffLinear( oldText, newText, expected, checkDiffCompatibility = true, comparator = null ) {
  400. const actions = { d: 'delete', e: 'equal', i: 'insert' };
  401. const expectedArray = expected.split( '' ).map( item => actions[ item ] );
  402. const result = fastDiff( oldText, newText, comparator, true );
  403. expect( result ).to.deep.equals( expectedArray, 'fastDiff linear result failed' );
  404. if ( checkDiffCompatibility ) {
  405. expect( result ).to.deep.equals( diff( oldText, newText, comparator ), 'diff compatibility failed' );
  406. }
  407. }