transform.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import transformations from '../../../../src/model/delta/basic-transformations';
  6. /*jshint unused: false*/
  7. import { transformDeltaSets } from '../../../../src/model/delta/transform';
  8. import Document from '../../../../src/model/document';
  9. import Element from '../../../../src/model/element';
  10. import Text from '../../../../src/model/text';
  11. import Position from '../../../../src/model/position';
  12. import Delta from '../../../../src/model/delta/delta';
  13. import InsertDelta from '../../../../src/model/delta/insertdelta';
  14. import RemoveDelta from '../../../../src/model/delta/removedelta';
  15. import SplitDelta from '../../../../src/model/delta/splitdelta';
  16. import NoOperation from '../../../../src/model/operation/nooperation';
  17. import MoveOperation from '../../../../src/model/operation/moveoperation';
  18. import RemoveOperation from '../../../../src/model/operation/removeoperation';
  19. import InsertOperation from '../../../../src/model/operation/insertoperation';
  20. import {
  21. expectDelta,
  22. getInsertDelta,
  23. getSplitDelta,
  24. getRemoveDelta
  25. } from '../../../../tests/model/delta/transform/_utils/utils';
  26. describe( 'transform', () => {
  27. let doc, root, gy, baseVersion;
  28. beforeEach( () => {
  29. doc = new Document();
  30. root = doc.createRoot();
  31. root.appendChildren( new Element( 'p', null, new Text( 'foobar' ) ) );
  32. gy = doc.graveyard;
  33. baseVersion = doc.version;
  34. } );
  35. describe( 'transformDeltaSets', () => {
  36. it( 'should transform two deltas', () => {
  37. const insertDelta = getInsertDelta( new Position( root, [ 0, 4 ] ), new Text( 'xxx' ), baseVersion );
  38. const removeDelta = getRemoveDelta( new Position( root, [ 0, 0 ] ), 2, baseVersion );
  39. const { deltasA, deltasB } = transformDeltaSets( [ insertDelta ], [ removeDelta ] );
  40. expect( deltasA.length ).to.equal( 1 );
  41. expect( deltasB.length ).to.equal( 1 );
  42. expectDelta( deltasA[ 0 ], {
  43. type: InsertDelta,
  44. operations: [
  45. {
  46. type: InsertOperation,
  47. position: new Position( root, [ 0, 2 ] ),
  48. baseVersion: 1
  49. }
  50. ]
  51. } );
  52. expectDelta( deltasB[ 0 ], {
  53. type: RemoveDelta,
  54. operations: [
  55. {
  56. type: RemoveOperation,
  57. sourcePosition: new Position( root, [ 0, 0 ] ),
  58. howMany: 2,
  59. baseVersion: 1
  60. }
  61. ]
  62. } );
  63. } );
  64. it( 'should transform two deltas - reverse', () => {
  65. const insertDelta = getInsertDelta( new Position( root, [ 0, 4 ] ), new Text( 'xxx' ), baseVersion );
  66. const removeDelta = getRemoveDelta( new Position( root, [ 0, 0 ] ), 2, baseVersion );
  67. const { deltasA, deltasB } = transformDeltaSets( [ removeDelta ], [ insertDelta ] );
  68. expect( deltasA.length ).to.equal( 1 );
  69. expect( deltasB.length ).to.equal( 1 );
  70. expectDelta( deltasA[ 0 ], {
  71. type: RemoveDelta,
  72. operations: [
  73. {
  74. type: RemoveOperation,
  75. sourcePosition: new Position( root, [ 0, 0 ] ),
  76. howMany: 2,
  77. baseVersion: 1
  78. }
  79. ]
  80. } );
  81. expectDelta( deltasB[ 0 ], {
  82. type: InsertDelta,
  83. operations: [
  84. {
  85. type: InsertOperation,
  86. position: new Position( root, [ 0, 2 ] ),
  87. baseVersion: 1
  88. }
  89. ]
  90. } );
  91. } );
  92. it( 'should transform two arrays of deltas', () => {
  93. const splitDelta = getSplitDelta( new Position( root, [ 0, 3 ] ), new Element( 'p' ), 3, baseVersion );
  94. const insertDeltaX = getInsertDelta( new Position( root, [ 0, 3 ] ), new Text( 'xxx' ), baseVersion + 2 );
  95. const removeDelta = getRemoveDelta( new Position( root, [ 0, 2 ] ), 2, baseVersion );
  96. const insertDeltaY = getInsertDelta( new Position( root, [ 0, 4 ] ), new Text( 'yyy' ), baseVersion + 1 );
  97. const { deltasA, deltasB } = transformDeltaSets( [ splitDelta, insertDeltaX ], [ removeDelta, insertDeltaY ], true );
  98. expect( deltasA.length ).to.equal( 3 );
  99. expect( deltasB.length ).to.equal( 2 );
  100. expectDelta( deltasA[ 0 ], {
  101. type: SplitDelta,
  102. operations: [
  103. {
  104. type: InsertOperation,
  105. position: new Position( root, [ 1 ] ),
  106. baseVersion: 2
  107. },
  108. {
  109. type: MoveOperation,
  110. sourcePosition: new Position( root, [ 0, 2 ] ),
  111. howMany: 5,
  112. targetPosition: new Position( root, [ 1, 0 ] ),
  113. baseVersion: 3
  114. }
  115. ]
  116. } );
  117. expectDelta( deltasA[ 1 ], {
  118. type: InsertDelta,
  119. operations: [
  120. {
  121. type: InsertOperation,
  122. position: new Position( root, [ 0, 2 ] ),
  123. baseVersion: 4
  124. }
  125. ]
  126. } );
  127. expectDelta( deltasA[ 2 ], {
  128. type: Delta,
  129. operations: [
  130. {
  131. type: NoOperation,
  132. baseVersion: 5
  133. }
  134. ]
  135. } );
  136. expectDelta( deltasB[ 0 ], {
  137. type: RemoveDelta,
  138. operations: [
  139. {
  140. type: RemoveOperation,
  141. sourcePosition: new Position( root, [ 1, 0 ] ),
  142. howMany: 1,
  143. baseVersion: 3
  144. },
  145. {
  146. type: RemoveOperation,
  147. sourcePosition: new Position( root, [ 0, 2 ] ),
  148. howMany: 1,
  149. baseVersion: 4
  150. }
  151. ]
  152. } );
  153. expectDelta( deltasB[ 1 ], {
  154. type: InsertDelta,
  155. operations: [
  156. {
  157. type: InsertOperation,
  158. position: new Position( root, [ 1, 2 ] ),
  159. baseVersion: 5
  160. }
  161. ]
  162. } );
  163. } );
  164. it( 'should transform two arrays of deltas - reverse', () => {
  165. const splitDelta = getSplitDelta( new Position( root, [ 0, 3 ] ), new Element( 'p' ), 3, baseVersion );
  166. const insertDeltaX = getInsertDelta( new Position( root, [ 0, 3 ] ), new Text( 'xxx' ), baseVersion + 2 );
  167. const removeDelta = getRemoveDelta( new Position( root, [ 0, 2 ] ), 2, baseVersion );
  168. const insertDeltaY = getInsertDelta( new Position( root, [ 0, 4 ] ), new Text( 'yyy' ), baseVersion + 1 );
  169. const { deltasA, deltasB } = transformDeltaSets( [ removeDelta, insertDeltaY ], [ splitDelta, insertDeltaX ], true );
  170. expect( deltasA.length ).to.equal( 2 );
  171. expect( deltasB.length ).to.equal( 3 );
  172. expectDelta( deltasA[ 0 ], {
  173. type: RemoveDelta,
  174. operations: [
  175. {
  176. type: RemoveOperation,
  177. sourcePosition: new Position( root, [ 1, 0 ] ),
  178. howMany: 1,
  179. baseVersion: 3
  180. },
  181. {
  182. type: RemoveOperation,
  183. sourcePosition: new Position( root, [ 0, 2 ] ),
  184. howMany: 1,
  185. baseVersion: 4
  186. }
  187. ]
  188. } );
  189. expectDelta( deltasA[ 1 ], {
  190. type: InsertDelta,
  191. operations: [
  192. {
  193. type: InsertOperation,
  194. position: new Position( root, [ 1, 2 ] ),
  195. baseVersion: 5
  196. }
  197. ]
  198. } );
  199. expectDelta( deltasB[ 0 ], {
  200. type: SplitDelta,
  201. operations: [
  202. {
  203. type: InsertOperation,
  204. position: new Position( root, [ 1 ] ),
  205. baseVersion: 2
  206. },
  207. {
  208. type: MoveOperation,
  209. sourcePosition: new Position( root, [ 0, 2 ] ),
  210. howMany: 5,
  211. targetPosition: new Position( root, [ 1, 0 ] ),
  212. baseVersion: 3
  213. }
  214. ]
  215. } );
  216. expectDelta( deltasB[ 1 ], {
  217. type: InsertDelta,
  218. operations: [
  219. {
  220. type: InsertOperation,
  221. position: new Position( root, [ 0, 2 ] ),
  222. baseVersion: 4
  223. }
  224. ]
  225. } );
  226. expectDelta( deltasB[ 2 ], {
  227. type: Delta,
  228. operations: [
  229. {
  230. type: NoOperation,
  231. baseVersion: 5
  232. }
  233. ]
  234. } );
  235. } );
  236. it( 'importance flag - first set is important', () => {
  237. const insertDeltaA = getInsertDelta( new Position( root, [ 0, 4 ] ), new Text( 'xxx' ), baseVersion );
  238. const insertDeltaB = getInsertDelta( new Position( root, [ 0, 4 ] ), new Text( 'yyy' ), baseVersion );
  239. let { deltasA, deltasB } = transformDeltaSets( [ insertDeltaA ], [ insertDeltaB ], true );
  240. expectDelta( deltasA[ 0 ], {
  241. type: InsertDelta,
  242. operations: [
  243. {
  244. type: InsertOperation,
  245. position: new Position( root, [ 0, 4 ] ),
  246. baseVersion: 1
  247. }
  248. ]
  249. } );
  250. expectDelta( deltasB[ 0 ], {
  251. type: InsertDelta,
  252. operations: [
  253. {
  254. type: InsertOperation,
  255. position: new Position( root, [ 0, 7 ] ),
  256. baseVersion: 1
  257. }
  258. ]
  259. } );
  260. } );
  261. it( 'importance flag - second set is important', () => {
  262. const insertDeltaA = getInsertDelta( new Position( root, [ 0, 4 ] ), new Text( 'xxx' ), baseVersion );
  263. const insertDeltaB = getInsertDelta( new Position( root, [ 0, 4 ] ), new Text( 'yyy' ), baseVersion );
  264. let { deltasA, deltasB } = transformDeltaSets( [ insertDeltaA ], [ insertDeltaB ], false );
  265. expectDelta( deltasA[ 0 ], {
  266. type: InsertDelta,
  267. operations: [
  268. {
  269. type: InsertOperation,
  270. position: new Position( root, [ 0, 7 ] ),
  271. baseVersion: 1
  272. }
  273. ]
  274. } );
  275. expectDelta( deltasB[ 0 ], {
  276. type: InsertDelta,
  277. operations: [
  278. {
  279. type: InsertOperation,
  280. position: new Position( root, [ 0, 4 ] ),
  281. baseVersion: 1
  282. }
  283. ]
  284. } );
  285. } );
  286. it( 'should not modify original deltas or arrays', () => {
  287. const insertDeltaA = getInsertDelta( new Position( root, [ 0, 0 ] ), new Text( 'x' ), baseVersion );
  288. const insertDeltaB = getInsertDelta( new Position( root, [ 1, 0 ] ), new Text( 'y' ), baseVersion );
  289. const originalDeltasA = [ insertDeltaA ];
  290. const originalDeltasB = [ insertDeltaB ];
  291. let { deltasA, deltasB } = transformDeltaSets( originalDeltasA, originalDeltasB, false );
  292. expect( deltasA ).to.not.equal( originalDeltasA );
  293. expect( deltasB ).to.not.equal( originalDeltasB );
  294. expect( deltasA[ 0 ] ).to.not.equal( originalDeltasA[ 0 ] );
  295. expect( deltasB[ 0 ] ).to.not.equal( originalDeltasB[ 0 ] );
  296. } );
  297. } );
  298. } );