splitdelta.js 11 KB

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