splitdelta.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model, operation */
  6. import transformations from '/ckeditor5/engine/model/delta/basic-transformations.js';
  7. /*jshint unused: false*/
  8. import transform from '/ckeditor5/engine/model/delta/transform.js';
  9. import Element from '/ckeditor5/engine/model/element.js';
  10. import Position from '/ckeditor5/engine/model/position.js';
  11. import Range from '/ckeditor5/engine/model/range.js';
  12. import Delta from '/ckeditor5/engine/model/delta/delta.js';
  13. import SplitDelta from '/ckeditor5/engine/model/delta/splitdelta.js';
  14. import InsertOperation from '/ckeditor5/engine/model/operation/insertoperation.js';
  15. import ReinsertOperation from '/ckeditor5/engine/model/operation/reinsertoperation.js';
  16. import MoveOperation from '/ckeditor5/engine/model/operation/moveoperation.js';
  17. import NoOperation from '/ckeditor5/engine/model/operation/nooperation.js';
  18. import { getNodesAndText, jsonParseStringify } from '/tests/engine/model/_utils/utils.js';
  19. import {
  20. applyDelta,
  21. expectDelta,
  22. getFilledDocument,
  23. getSplitDelta,
  24. getWrapDelta,
  25. getUnwrapDelta
  26. } from '/tests/engine/model/delta/transform/_utils/utils.js';
  27. describe( 'transform', () => {
  28. let doc, root, gy, baseVersion;
  29. beforeEach( () => {
  30. doc = getFilledDocument();
  31. root = doc.getRoot();
  32. gy = doc.graveyard;
  33. baseVersion = doc.version;
  34. } );
  35. describe( 'SplitDelta by', () => {
  36. let splitDelta, splitPosition;
  37. beforeEach( () => {
  38. splitPosition = new Position( root, [ 3, 3, 3, 3 ] );
  39. splitDelta = getSplitDelta( splitPosition, new Element( 'p' ), 9, baseVersion );
  40. } );
  41. describe( 'SplitDelta', () => {
  42. it( 'split in same parent and offset', () => {
  43. let splitDeltaB = getSplitDelta( splitPosition, new Element( 'p' ), 9, baseVersion );
  44. let transformed = transform( splitDelta, splitDeltaB );
  45. baseVersion = splitDeltaB.operations.length;
  46. expect( transformed.length ).to.equal( 1 );
  47. expectDelta( transformed[ 0 ], {
  48. type: Delta,
  49. operations: [
  50. {
  51. type: NoOperation,
  52. baseVersion: baseVersion
  53. }
  54. ]
  55. } );
  56. // Test if deltas do what they should after applying transformed delta.
  57. applyDelta( splitDeltaB, doc );
  58. applyDelta( transformed[ 0 ], doc );
  59. let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 0 ] ), 5 ) );
  60. // Incoming split delta is discarded. Only one new element is created after applying both split deltas.
  61. // There are no empty P elements.
  62. expect( nodesAndText ).to.equal( 'XXXXXabcdXPabcPPfoobarxyzP' );
  63. } );
  64. it( 'split in same parent, incoming delta splits closer', () => {
  65. let splitDeltaB = getSplitDelta( new Position( root, [ 3, 3, 3, 5 ] ), new Element( 'p' ), 7, baseVersion );
  66. let transformed = transform( splitDelta, splitDeltaB );
  67. baseVersion = splitDeltaB.operations.length;
  68. expect( transformed.length ).to.equal( 1 );
  69. expectDelta( transformed[ 0 ], {
  70. type: SplitDelta,
  71. operations: [
  72. {
  73. type: InsertOperation,
  74. position: new Position( root, [ 3, 3, 4 ] ),
  75. baseVersion: baseVersion
  76. },
  77. {
  78. type: MoveOperation,
  79. sourcePosition: new Position( root, [ 3, 3, 3, 3 ] ),
  80. howMany: 2,
  81. targetPosition: new Position( root, [ 3, 3, 4, 0 ] ),
  82. baseVersion: baseVersion + 1
  83. }
  84. ]
  85. } );
  86. // Test if deltas do what they should after applying transformed delta.
  87. applyDelta( splitDeltaB, doc );
  88. applyDelta( transformed[ 0 ], doc );
  89. let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 0 ] ), 6 ) );
  90. // P element is correctly split, there are three P elements, letters in P elements are in correct order.
  91. expect( nodesAndText ).to.equal( 'XXXXXabcdXPabcPPfoPPobarxyzP' );
  92. } );
  93. it( 'split in same parent, incoming delta splits closer, split deltas have reinsert operations', () => {
  94. let reOp = new ReinsertOperation(
  95. new Position( gy, [ 1 ] ),
  96. 1,
  97. Position.createFromPosition( splitDelta.operations[ 0 ].position ),
  98. splitDelta.operations[ 0 ].baseVersion
  99. );
  100. splitDelta.operations[ 0 ] = reOp;
  101. let splitDeltaB = getSplitDelta( new Position( root, [ 3, 3, 3, 5 ] ), new Element( 'p' ), 7, baseVersion );
  102. reOp = new ReinsertOperation(
  103. new Position( gy, [ 0 ] ),
  104. 1,
  105. Position.createFromPosition( splitDeltaB.operations[ 0 ].position ),
  106. splitDeltaB.operations[ 0 ].baseVersion
  107. );
  108. splitDeltaB.operations[ 0 ] = reOp;
  109. let transformed = transform( splitDelta, splitDeltaB );
  110. baseVersion = splitDeltaB.operations.length;
  111. expect( transformed.length ).to.equal( 1 );
  112. expectDelta( transformed[ 0 ], {
  113. type: SplitDelta,
  114. operations: [
  115. {
  116. type: ReinsertOperation,
  117. sourcePosition: new Position( gy, [ 0 ] ),
  118. howMany: 1,
  119. targetPosition: new Position( root, [ 3, 3, 4 ] ),
  120. baseVersion: baseVersion
  121. },
  122. {
  123. type: MoveOperation,
  124. sourcePosition: new Position( root, [ 3, 3, 3, 3 ] ),
  125. howMany: 2,
  126. targetPosition: new Position( root, [ 3, 3, 4, 0 ] ),
  127. baseVersion: baseVersion + 1
  128. }
  129. ]
  130. } );
  131. } );
  132. it( 'split in same parent, incoming delta splits further', () => {
  133. let splitDeltaB = getSplitDelta( new Position( root, [ 3, 3, 3, 1 ] ), new Element( 'p' ), 11, baseVersion );
  134. let transformed = transform( splitDelta, splitDeltaB );
  135. baseVersion = splitDeltaB.operations.length;
  136. expect( transformed.length ).to.equal( 1 );
  137. expectDelta( transformed[ 0 ], {
  138. type: SplitDelta,
  139. operations: [
  140. {
  141. type: InsertOperation,
  142. position: new Position( root, [ 3, 3, 5 ] ),
  143. baseVersion: baseVersion
  144. },
  145. {
  146. type: MoveOperation,
  147. sourcePosition: new Position( root, [ 3, 3, 4, 2 ] ),
  148. howMany: 9,
  149. targetPosition: new Position( root, [ 3, 3, 5, 0 ] ),
  150. baseVersion: baseVersion + 1
  151. }
  152. ]
  153. } );
  154. // Test if deltas do what they should after applying transformed delta.
  155. applyDelta( splitDeltaB, doc );
  156. applyDelta( transformed[ 0 ], doc );
  157. let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 0 ] ), 6 ) );
  158. // P element is correctly split, there are three P elements, letters in P elements are in correct order.
  159. expect( nodesAndText ).to.equal( 'XXXXXabcdXPaPPbcPPfoobarxyzP' );
  160. } );
  161. it( 'split in same parent, incoming delta splits further, split deltas have reinsert operations', () => {
  162. let reOp = new ReinsertOperation(
  163. new Position( gy, [ 1 ] ),
  164. 1,
  165. Position.createFromPosition( splitDelta.operations[ 0 ].position ),
  166. splitDelta.operations[ 0 ].baseVersion
  167. );
  168. splitDelta.operations[ 0 ] = reOp;
  169. let splitDeltaB = getSplitDelta( new Position( root, [ 3, 3, 3, 1 ] ), new Element( 'p' ), 11, baseVersion );
  170. reOp = new ReinsertOperation(
  171. new Position( gy, [ 0 ] ),
  172. 1,
  173. Position.createFromPosition( splitDeltaB.operations[ 0 ].position ),
  174. splitDeltaB.operations[ 0 ].baseVersion
  175. );
  176. splitDeltaB.operations[ 0 ] = reOp;
  177. let transformed = transform( splitDelta, splitDeltaB );
  178. baseVersion = splitDeltaB.operations.length;
  179. expect( transformed.length ).to.equal( 1 );
  180. expectDelta( transformed[ 0 ], {
  181. type: SplitDelta,
  182. operations: [
  183. {
  184. type: ReinsertOperation,
  185. sourcePosition: new Position( gy, [ 0 ] ),
  186. howMany: 1,
  187. targetPosition: new Position( root, [ 3, 3, 5 ] ),
  188. baseVersion: baseVersion
  189. },
  190. {
  191. type: MoveOperation,
  192. sourcePosition: new Position( root, [ 3, 3, 4, 2 ] ),
  193. howMany: 9,
  194. targetPosition: new Position( root, [ 3, 3, 5, 0 ] ),
  195. baseVersion: baseVersion + 1
  196. }
  197. ]
  198. } );
  199. } );
  200. it( 'split in split parent', () => {
  201. let splitDeltaB = getSplitDelta( new Position( root, [ 3, 3, 3 ] ), new Element( 'div' ), 1, baseVersion );
  202. let transformed = transform( splitDelta, splitDeltaB );
  203. baseVersion = splitDeltaB.operations.length;
  204. expect( transformed.length ).to.equal( 1 );
  205. expectDelta( transformed[ 0 ], {
  206. type: SplitDelta,
  207. operations: [
  208. {
  209. type: InsertOperation,
  210. position: new Position( root, [ 3, 4, 1 ] ),
  211. baseVersion: baseVersion
  212. },
  213. {
  214. type: MoveOperation,
  215. sourcePosition: new Position( root, [ 3, 4, 0, 3 ] ),
  216. howMany: 9,
  217. targetPosition: new Position( root, [ 3, 4, 1, 0 ] ),
  218. baseVersion: baseVersion + 1
  219. }
  220. ]
  221. } );
  222. // Test if deltas do what they should after applying transformed delta.
  223. applyDelta( splitDeltaB, doc );
  224. applyDelta( transformed[ 0 ], doc );
  225. let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3 ] ), 2 ) );
  226. // DIV and P elements are correctly split.
  227. expect( nodesAndText ).to.equal( 'DIVXXXXXabcdXDIVDIVPabcPPfoobarxyzPDIV' );
  228. } );
  229. } );
  230. describe( 'UnwrapDelta', () => {
  231. it( 'split position directly in unwrapped node', () => {
  232. let unwrapDelta = getUnwrapDelta( new Position( root, [ 3, 3, 3 ] ), 12, baseVersion );
  233. let transformed = transform( splitDelta, unwrapDelta );
  234. baseVersion = unwrapDelta.operations.length;
  235. expect( transformed.length ).to.equal( 1 );
  236. expectDelta( transformed[ 0 ], {
  237. type: Delta,
  238. operations: [
  239. {
  240. type: NoOperation,
  241. baseVersion: baseVersion
  242. }
  243. ]
  244. } );
  245. // Test if deltas do what they should after applying transformed delta.
  246. applyDelta( unwrapDelta, doc );
  247. applyDelta( transformed[ 0 ], doc );
  248. let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3 ] ), 1 ) );
  249. // UnwrapDelta is applied. SplitDelta is discarded.
  250. expect( nodesAndText ).to.equal( 'DIVXXXXXabcdXabcfoobarxyzDIV' );
  251. } );
  252. it( 'split position indirectly in unwrapped node', () => {
  253. let unwrapDelta = getUnwrapDelta( new Position( root, [ 3, 3 ] ), 4, baseVersion );
  254. let transformed = transform( splitDelta, unwrapDelta );
  255. expect( transformed.length ).to.equal( 1 );
  256. baseVersion = unwrapDelta.operations.length;
  257. expectDelta( transformed[ 0 ], {
  258. type: SplitDelta,
  259. operations: [
  260. {
  261. type: InsertOperation,
  262. position: new Position( root, [ 3, 7 ] ),
  263. baseVersion: baseVersion
  264. },
  265. {
  266. type: MoveOperation,
  267. sourcePosition: new Position( root, [ 3, 6, 3 ] ),
  268. howMany: 9,
  269. targetPosition: new Position( root, [ 3, 7, 0 ] ),
  270. baseVersion: baseVersion + 1
  271. }
  272. ]
  273. } );
  274. // Test if deltas do what they should after applying transformed delta.
  275. applyDelta( unwrapDelta, doc );
  276. applyDelta( transformed[ 0 ], doc );
  277. let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3 ] ), 1 ) );
  278. // UnwrapDelta and SplitDelta are correctly applied.
  279. expect( nodesAndText ).to.equal( 'DIVXXXXXaXXXXXXabcdXPabcPPfoobarxyzPDIV' );
  280. } );
  281. } );
  282. describe( 'WrapDelta', () => {
  283. it( 'split position is between wrapped nodes', () => {
  284. let wrapRange = new Range( new Position( root, [ 3, 3, 3, 1 ] ), new Position( root, [ 3, 3, 3, 5 ] ) );
  285. let wrapElement = new Element( 'E' );
  286. let wrapDelta = getWrapDelta( wrapRange, wrapElement, baseVersion );
  287. let transformed = transform( splitDelta, wrapDelta );
  288. baseVersion = wrapDelta.operations.length;
  289. expect( transformed.length ).to.equal( 1 );
  290. expectDelta( transformed[ 0 ], {
  291. type: Delta,
  292. operations: [
  293. {
  294. type: NoOperation,
  295. baseVersion: baseVersion
  296. }
  297. ]
  298. } );
  299. // Test if deltas do what they should after applying transformed delta.
  300. applyDelta( wrapDelta, doc );
  301. applyDelta( transformed[ 0 ], doc );
  302. let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 3 ] ), 1 ) );
  303. // WrapDelta is applied. SplitDelta is discarded.
  304. expect( nodesAndText ).to.equal( 'PaEbcfoEobarxyzP' );
  305. } );
  306. it( 'split position is before wrapped nodes', () => {
  307. let wrapRange = new Range( new Position( root, [ 3, 3, 3, 5 ] ), new Position( root, [ 3, 3, 3, 7 ] ) );
  308. let wrapElement = new Element( 'E' );
  309. let wrapDelta = getWrapDelta( wrapRange, wrapElement, baseVersion );
  310. let transformed = transform( splitDelta, wrapDelta );
  311. expect( transformed.length ).to.equal( 1 );
  312. baseVersion = wrapDelta.operations.length;
  313. expectDelta( transformed[ 0 ], {
  314. type: SplitDelta,
  315. operations: [
  316. {
  317. type: InsertOperation,
  318. position: new Position( root, [ 3, 3, 4 ] ),
  319. baseVersion: baseVersion
  320. },
  321. {
  322. type: MoveOperation,
  323. sourcePosition: new Position( root, [ 3, 3, 3, 3 ] ),
  324. howMany: 8,
  325. targetPosition: new Position( root, [ 3, 3, 4, 0 ] ),
  326. baseVersion: baseVersion + 1
  327. }
  328. ]
  329. } );
  330. // Test if deltas do what they should after applying transformed delta.
  331. applyDelta( wrapDelta, doc );
  332. applyDelta( transformed[ 0 ], doc );
  333. let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 3 ] ), 2 ) );
  334. // WrapDelta and SplitDelta are correctly applied.
  335. expect( nodesAndText ).to.equal( 'PabcPPfoEobEarxyzP' );
  336. } );
  337. it( 'split position is inside wrapped node', () => {
  338. let wrapRange = new Range( new Position( root, [ 3, 3, 2 ] ), new Position( root, [ 3, 3, 4 ] ) );
  339. let wrapElement = new Element( 'E' );
  340. let wrapDelta = getWrapDelta( wrapRange, wrapElement, baseVersion );
  341. let transformed = transform( splitDelta, wrapDelta );
  342. expect( transformed.length ).to.equal( 1 );
  343. baseVersion = wrapDelta.operations.length;
  344. expectDelta( transformed[ 0 ], {
  345. type: SplitDelta,
  346. operations: [
  347. {
  348. type: InsertOperation,
  349. position: new Position( root, [ 3, 3, 2, 2 ] ),
  350. baseVersion: baseVersion
  351. },
  352. {
  353. type: MoveOperation,
  354. sourcePosition: new Position( root, [ 3, 3, 2, 1, 3 ] ),
  355. howMany: 9,
  356. targetPosition: new Position( root, [ 3, 3, 2, 2, 0 ] ),
  357. baseVersion: baseVersion + 1
  358. }
  359. ]
  360. } );
  361. // Test if deltas do what they should after applying transformed delta.
  362. applyDelta( wrapDelta, doc );
  363. applyDelta( transformed[ 0 ], doc );
  364. let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 2 ] ), 1 ) );
  365. // WrapDelta and SplitDelta are correctly applied.
  366. expect( nodesAndText ).to.equal( 'EXabcdXPabcPPfoobarxyzPE' );
  367. } );
  368. } );
  369. } );
  370. } );