splitdelta.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel, operation */
  6. 'use strict';
  7. import transformations from '/ckeditor5/engine/treemodel/delta/basic-transformations.js';
  8. /*jshint unused: false*/
  9. import transform from '/ckeditor5/engine/treemodel/delta/transform.js';
  10. import Element from '/ckeditor5/engine/treemodel/element.js';
  11. import Position from '/ckeditor5/engine/treemodel/position.js';
  12. import Range from '/ckeditor5/engine/treemodel/range.js';
  13. import Delta from '/ckeditor5/engine/treemodel/delta/delta.js';
  14. import SplitDelta from '/ckeditor5/engine/treemodel/delta/splitdelta.js';
  15. import InsertOperation from '/ckeditor5/engine/treemodel/operation/insertoperation.js';
  16. import MoveOperation from '/ckeditor5/engine/treemodel/operation/moveoperation.js';
  17. import { getNodesAndText, jsonParseStringify } from '/tests/engine/treemodel/_utils/utils.js';
  18. import {
  19. applyDelta,
  20. expectDelta,
  21. getFilledDocument,
  22. getSplitDelta,
  23. getWrapDelta,
  24. getUnwrapDelta
  25. } from '/tests/engine/treemodel/delta/transform/_utils/utils.js';
  26. describe( 'transform', () => {
  27. let doc, root, gy, baseVersion;
  28. beforeEach( () => {
  29. doc = getFilledDocument();
  30. root = doc.getRoot( 'root' );
  31. gy = doc.graveyard;
  32. baseVersion = doc.version;
  33. } );
  34. describe( 'SplitDelta by', () => {
  35. let splitDelta, splitPosition;
  36. beforeEach( () => {
  37. splitPosition = new Position( root, [ 3, 3, 3, 3 ] );
  38. splitDelta = getSplitDelta( splitPosition, new Element( 'p' ), 9, baseVersion );
  39. } );
  40. describe( 'SplitDelta', () => {
  41. it( 'split in same parent and offset', () => {
  42. let splitDeltaB = getSplitDelta( splitPosition, new Element( 'p' ), 9, baseVersion );
  43. let transformed = transform( splitDelta, splitDeltaB );
  44. expect( transformed.length ).to.equal( 1 );
  45. expectDelta( transformed[ 0 ], {
  46. type: Delta,
  47. operations: []
  48. } );
  49. // Test if deltas do what they should after applying transformed delta.
  50. applyDelta( splitDeltaB, doc );
  51. applyDelta( transformed[ 0 ], doc );
  52. let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 0 ] ), 5 ) );
  53. // Incoming split delta is discarded. Only one new element is created after applying both split deltas.
  54. // There are no empty P elements.
  55. expect( nodesAndText ).to.equal( 'XXXXXabcdXPabcPPfoobarxyzP' );
  56. } );
  57. it( 'split in same parent, incoming delta splits closer', () => {
  58. let splitDeltaB = getSplitDelta( new Position( root, [ 3, 3, 3, 5 ] ), new Element( 'p' ), 7, baseVersion );
  59. let transformed = transform( splitDelta, splitDeltaB );
  60. baseVersion = splitDeltaB.operations.length;
  61. expect( transformed.length ).to.equal( 1 );
  62. expectDelta( transformed[ 0 ], {
  63. type: SplitDelta,
  64. operations: [
  65. {
  66. type: InsertOperation,
  67. position: new Position( root, [ 3, 3, 4 ] ),
  68. baseVersion: baseVersion
  69. },
  70. {
  71. type: MoveOperation,
  72. sourcePosition: new Position( root, [ 3, 3, 3, 3 ] ),
  73. howMany: 2,
  74. targetPosition: new Position( root, [ 3, 3, 4, 0 ] ),
  75. baseVersion: baseVersion + 1
  76. }
  77. ]
  78. } );
  79. // Test if deltas do what they should after applying transformed delta.
  80. applyDelta( splitDeltaB, doc );
  81. applyDelta( transformed[ 0 ], doc );
  82. let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 0 ] ), 6 ) );
  83. // P element is correctly split, there are three P elements, letters in P elements are in correct order.
  84. expect( nodesAndText ).to.equal( 'XXXXXabcdXPabcPPfoPPobarxyzP' );
  85. } );
  86. it( 'split in same parent, incoming delta splits further', () => {
  87. let splitDeltaB = getSplitDelta( new Position( root, [ 3, 3, 3, 1 ] ), new Element( 'p' ), 11, baseVersion );
  88. let transformed = transform( splitDelta, splitDeltaB );
  89. baseVersion = splitDeltaB.operations.length;
  90. expect( transformed.length ).to.equal( 1 );
  91. expectDelta( transformed[ 0 ], {
  92. type: SplitDelta,
  93. operations: [
  94. {
  95. type: InsertOperation,
  96. position: new Position( root, [ 3, 3, 5 ] ),
  97. baseVersion: baseVersion
  98. },
  99. {
  100. type: MoveOperation,
  101. sourcePosition: new Position( root, [ 3, 3, 4, 2 ] ),
  102. howMany: 9,
  103. targetPosition: new Position( root, [ 3, 3, 5, 0 ] ),
  104. baseVersion: baseVersion + 1
  105. }
  106. ]
  107. } );
  108. // Test if deltas do what they should after applying transformed delta.
  109. applyDelta( splitDeltaB, doc );
  110. applyDelta( transformed[ 0 ], doc );
  111. let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 0 ] ), 6 ) );
  112. // P element is correctly split, there are three P elements, letters in P elements are in correct order.
  113. expect( nodesAndText ).to.equal( 'XXXXXabcdXPaPPbcPPfoobarxyzP' );
  114. } );
  115. it( 'split in split parent', () => {
  116. let splitDeltaB = getSplitDelta( new Position( root, [ 3, 3, 3 ] ), new Element( 'div' ), 1, baseVersion );
  117. let transformed = transform( splitDelta, splitDeltaB );
  118. baseVersion = splitDeltaB.operations.length;
  119. expect( transformed.length ).to.equal( 1 );
  120. expectDelta( transformed[ 0 ], {
  121. type: SplitDelta,
  122. operations: [
  123. {
  124. type: InsertOperation,
  125. position: new Position( root, [ 3, 4, 1 ] ),
  126. baseVersion: baseVersion
  127. },
  128. {
  129. type: MoveOperation,
  130. sourcePosition: new Position( root, [ 3, 4, 0, 3 ] ),
  131. howMany: 9,
  132. targetPosition: new Position( root, [ 3, 4, 1, 0 ] ),
  133. baseVersion: baseVersion + 1
  134. }
  135. ]
  136. } );
  137. // Test if deltas do what they should after applying transformed delta.
  138. applyDelta( splitDeltaB, doc );
  139. applyDelta( transformed[ 0 ], doc );
  140. let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3 ] ), 2 ) );
  141. // DIV and P elements are correctly split.
  142. expect( nodesAndText ).to.equal( 'DIVXXXXXabcdXDIVDIVPabcPPfoobarxyzPDIV' );
  143. } );
  144. } );
  145. describe( 'UnwrapDelta', () => {
  146. it( 'split position directly in unwrapped node', () => {
  147. let unwrapDelta = getUnwrapDelta( new Position( root, [ 3, 3, 3 ] ), 12, baseVersion );
  148. let transformed = transform( splitDelta, unwrapDelta );
  149. expect( transformed.length ).to.equal( 1 );
  150. expectDelta( transformed[ 0 ], {
  151. type: Delta,
  152. operations: []
  153. } );
  154. // Test if deltas do what they should after applying transformed delta.
  155. applyDelta( unwrapDelta, doc );
  156. applyDelta( transformed[ 0 ], doc );
  157. let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3 ] ), 1 ) );
  158. // UnwrapDelta is applied. SplitDelta is discarded.
  159. expect( nodesAndText ).to.equal( 'DIVXXXXXabcdXabcfoobarxyzDIV' );
  160. } );
  161. it( 'split position indirectly in unwrapped node', () => {
  162. let unwrapDelta = getUnwrapDelta( new Position( root, [ 3, 3 ] ), 4, baseVersion );
  163. let transformed = transform( splitDelta, unwrapDelta );
  164. expect( transformed.length ).to.equal( 1 );
  165. baseVersion = unwrapDelta.operations.length;
  166. expectDelta( transformed[ 0 ], {
  167. type: SplitDelta,
  168. operations: [
  169. {
  170. type: InsertOperation,
  171. position: new Position( root, [ 3, 7 ] ),
  172. baseVersion: baseVersion
  173. },
  174. {
  175. type: MoveOperation,
  176. sourcePosition: new Position( root, [ 3, 6, 3 ] ),
  177. howMany: 9,
  178. targetPosition: new Position( root, [ 3, 7, 0 ] ),
  179. baseVersion: baseVersion + 1
  180. }
  181. ]
  182. } );
  183. // Test if deltas do what they should after applying transformed delta.
  184. applyDelta( unwrapDelta, doc );
  185. applyDelta( transformed[ 0 ], doc );
  186. let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3 ] ), 1 ) );
  187. // UnwrapDelta and SplitDelta are correctly applied.
  188. expect( nodesAndText ).to.equal( 'DIVXXXXXaXXXXXXabcdXPabcPPfoobarxyzPDIV' );
  189. } );
  190. } );
  191. describe( 'WrapDelta', () => {
  192. it( 'split position is between wrapped nodes', () => {
  193. let wrapRange = new Range( new Position( root, [ 3, 3, 3, 1 ] ), new Position( root, [ 3, 3, 3, 5 ] ) );
  194. let wrapElement = new Element( 'E' );
  195. let wrapDelta = getWrapDelta( wrapRange, wrapElement, baseVersion );
  196. let transformed = transform( splitDelta, wrapDelta );
  197. expect( transformed.length ).to.equal( 1 );
  198. expectDelta( transformed[ 0 ], {
  199. type: Delta,
  200. operations: []
  201. } );
  202. // Test if deltas do what they should after applying transformed delta.
  203. applyDelta( wrapDelta, doc );
  204. applyDelta( transformed[ 0 ], doc );
  205. let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 3 ] ), 1 ) );
  206. // WrapDelta is applied. SplitDelta is discarded.
  207. expect( nodesAndText ).to.equal( 'PaEbcfoEobarxyzP' );
  208. } );
  209. it( 'split position is before wrapped nodes', () => {
  210. let wrapRange = new Range( new Position( root, [ 3, 3, 3, 5 ] ), new Position( root, [ 3, 3, 3, 7 ] ) );
  211. let wrapElement = new Element( 'E' );
  212. let wrapDelta = getWrapDelta( wrapRange, wrapElement, baseVersion );
  213. let transformed = transform( splitDelta, wrapDelta );
  214. expect( transformed.length ).to.equal( 1 );
  215. baseVersion = wrapDelta.operations.length;
  216. expectDelta( transformed[ 0 ], {
  217. type: SplitDelta,
  218. operations: [
  219. {
  220. type: InsertOperation,
  221. position: new Position( root, [ 3, 3, 4 ] ),
  222. baseVersion: baseVersion
  223. },
  224. {
  225. type: MoveOperation,
  226. sourcePosition: new Position( root, [ 3, 3, 3, 3 ] ),
  227. howMany: 8,
  228. targetPosition: new Position( root, [ 3, 3, 4, 0 ] ),
  229. baseVersion: baseVersion + 1
  230. }
  231. ]
  232. } );
  233. // Test if deltas do what they should after applying transformed delta.
  234. applyDelta( wrapDelta, doc );
  235. applyDelta( transformed[ 0 ], doc );
  236. let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 3 ] ), 2 ) );
  237. // WrapDelta and SplitDelta are correctly applied.
  238. expect( nodesAndText ).to.equal( 'PabcPPfoEobEarxyzP' );
  239. } );
  240. it( 'split position is inside wrapped node', () => {
  241. let wrapRange = new Range( new Position( root, [ 3, 3, 2 ] ), new Position( root, [ 3, 3, 4 ] ) );
  242. let wrapElement = new Element( 'E' );
  243. let wrapDelta = getWrapDelta( wrapRange, wrapElement, baseVersion );
  244. let transformed = transform( splitDelta, wrapDelta );
  245. expect( transformed.length ).to.equal( 1 );
  246. baseVersion = wrapDelta.operations.length;
  247. expectDelta( transformed[ 0 ], {
  248. type: SplitDelta,
  249. operations: [
  250. {
  251. type: InsertOperation,
  252. position: new Position( root, [ 3, 3, 2, 2 ] ),
  253. baseVersion: baseVersion
  254. },
  255. {
  256. type: MoveOperation,
  257. sourcePosition: new Position( root, [ 3, 3, 2, 1, 3 ] ),
  258. howMany: 9,
  259. targetPosition: new Position( root, [ 3, 3, 2, 2, 0 ] ),
  260. baseVersion: baseVersion + 1
  261. }
  262. ]
  263. } );
  264. // Test if deltas do what they should after applying transformed delta.
  265. applyDelta( wrapDelta, doc );
  266. applyDelta( transformed[ 0 ], doc );
  267. let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 2 ] ), 1 ) );
  268. // WrapDelta and SplitDelta are correctly applied.
  269. expect( nodesAndText ).to.equal( 'EXabcdXPabcPPfoobarxyzPE' );
  270. } );
  271. } );
  272. } );
  273. } );