splitdelta.js 15 KB

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