splitdelta.js 10.0 KB

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