splitdelta.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from '../../../src/model/document';
  6. import Position from '../../../src/model/position';
  7. import Element from '../../../src/model/element';
  8. import Text from '../../../src/model/text';
  9. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  10. import MergeDelta from '../../../src/model/delta/mergedelta';
  11. import SplitDelta from '../../../src/model/delta/splitdelta';
  12. import InsertOperation from '../../../src/model/operation/insertoperation';
  13. import MoveOperation from '../../../src/model/operation/moveoperation';
  14. import NoOperation from '../../../src/model/operation/nooperation';
  15. import RemoveOperation from '../../../src/model/operation/removeoperation';
  16. import count from '@ckeditor/ckeditor5-utils/src/count';
  17. describe( 'Batch', () => {
  18. let doc, root, p;
  19. beforeEach( () => {
  20. doc = new Document();
  21. root = doc.createRoot();
  22. p = new Element( 'p', { key: 'value' }, new Text( 'foobar' ) );
  23. root.insertChildren( 0, p );
  24. } );
  25. describe( 'split', () => {
  26. it( 'should split foobar to foo and bar', () => {
  27. doc.batch().split( new Position( root, [ 0, 3 ] ) );
  28. expect( root.maxOffset ).to.equal( 2 );
  29. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  30. expect( root.getChild( 0 ).maxOffset ).to.equal( 3 );
  31. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  32. expect( root.getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
  33. expect( root.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
  34. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  35. expect( root.getChild( 1 ).maxOffset ).to.equal( 3 );
  36. expect( count( root.getChild( 1 ).getAttributes() ) ).to.equal( 1 );
  37. expect( root.getChild( 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
  38. expect( root.getChild( 1 ).getChild( 0 ).data ).to.equal( 'bar' );
  39. } );
  40. it( 'should create an empty paragraph if we split at the end', () => {
  41. doc.batch().split( new Position( root, [ 0, 6 ] ) );
  42. expect( root.maxOffset ).to.equal( 2 );
  43. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  44. expect( root.getChild( 0 ).maxOffset ).to.equal( 6 );
  45. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  46. expect( root.getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
  47. expect( root.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foobar' );
  48. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  49. expect( root.getChild( 1 ).maxOffset ).to.equal( 0 );
  50. expect( count( root.getChild( 1 ).getAttributes() ) ).to.equal( 1 );
  51. expect( root.getChild( 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
  52. } );
  53. it( 'should throw if we try to split a root', () => {
  54. expect( () => {
  55. doc.batch().split( new Position( root, [ 0 ] ) );
  56. } ).to.throw( CKEditorError, /^batch-split-root/ );
  57. } );
  58. it( 'should be chainable', () => {
  59. const batch = doc.batch();
  60. const chain = batch.split( new Position( root, [ 0, 3 ] ) );
  61. expect( chain ).to.equal( batch );
  62. } );
  63. it( 'should add delta to batch and operation to delta before applying operation', () => {
  64. sinon.spy( doc, 'applyOperation' );
  65. const batch = doc.batch().split( new Position( root, [ 0, 3 ] ) );
  66. const correctDeltaMatcher = sinon.match( operation => {
  67. return operation.delta && operation.delta.batch && operation.delta.batch == batch;
  68. } );
  69. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  70. } );
  71. } );
  72. } );
  73. describe( 'SplitDelta', () => {
  74. let splitDelta, doc, root;
  75. beforeEach( () => {
  76. doc = new Document();
  77. root = doc.createRoot();
  78. splitDelta = new SplitDelta();
  79. } );
  80. describe( 'constructor()', () => {
  81. it( 'should create split delta with no operations added', () => {
  82. expect( splitDelta.operations.length ).to.equal( 0 );
  83. } );
  84. } );
  85. describe( 'type', () => {
  86. it( 'should be equal to split', () => {
  87. expect( splitDelta.type ).to.equal( 'split' );
  88. } );
  89. } );
  90. describe( 'position', () => {
  91. it( 'should be null if there are no operations in delta', () => {
  92. expect( splitDelta.position ).to.be.null;
  93. } );
  94. it( 'should be equal to the position where node is split', () => {
  95. splitDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2 ] ), new Element( 'p' ), 0 ) );
  96. splitDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1, 4 ] ), 4, new Position( root, [ 1, 2, 0 ] ), 1 ) );
  97. expect( splitDelta.position.root ).to.equal( root );
  98. expect( splitDelta.position.path ).to.deep.equal( [ 1, 1, 4 ] );
  99. } );
  100. } );
  101. describe( 'getReversed', () => {
  102. it( 'should return empty MergeDelta if there are no operations in delta', () => {
  103. const reversed = splitDelta.getReversed();
  104. expect( reversed ).to.be.instanceof( MergeDelta );
  105. expect( reversed.operations.length ).to.equal( 0 );
  106. } );
  107. it( 'should return correct SplitDelta', () => {
  108. splitDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2 ] ), new Element( 'p' ), 0 ) );
  109. splitDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1, 4 ] ), 4, new Position( root, [ 1, 2, 0 ] ), 1 ) );
  110. const reversed = splitDelta.getReversed();
  111. expect( reversed ).to.be.instanceof( MergeDelta );
  112. expect( reversed.operations.length ).to.equal( 2 );
  113. expect( reversed.operations[ 0 ] ).to.be.instanceof( MoveOperation );
  114. expect( reversed.operations[ 0 ].sourcePosition.path ).to.deep.equal( [ 1, 2, 0 ] );
  115. expect( reversed.operations[ 0 ].howMany ).to.equal( 4 );
  116. expect( reversed.operations[ 0 ].targetPosition.path ).to.deep.equal( [ 1, 1, 4 ] );
  117. expect( reversed.operations[ 1 ] ).to.be.instanceof( RemoveOperation );
  118. expect( reversed.operations[ 1 ].sourcePosition.path ).to.deep.equal( [ 1, 2 ] );
  119. expect( reversed.operations[ 1 ].howMany ).to.equal( 1 );
  120. } );
  121. } );
  122. describe( '_cloneOperation', () => {
  123. it( 'should return null if delta has no operations', () => {
  124. expect( splitDelta._cloneOperation ).to.be.null;
  125. } );
  126. it( 'should return the first operation in the delta, which is InsertOperation or ReinsertOperation', () => {
  127. const p = new Element( 'p' );
  128. const insert = new InsertOperation( new Position( root, [ 1, 2 ] ), p, 0 );
  129. splitDelta.operations.push( insert );
  130. splitDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1, 4 ] ), 4, new Position( root, [ 1, 2, 0 ] ), 1 ) );
  131. expect( splitDelta._cloneOperation ).to.equal( insert );
  132. } );
  133. } );
  134. describe( '_moveOperation', () => {
  135. it( 'should return null if delta has no operations', () => {
  136. expect( splitDelta._moveOperation ).to.be.null;
  137. } );
  138. it( 'should return null if second operation is NoOperation', () => {
  139. const p = new Element( 'p' );
  140. splitDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2 ] ), p, 0 ) );
  141. splitDelta.operations.push( new NoOperation( 1 ) );
  142. expect( splitDelta._moveOperation ).to.be.null;
  143. } );
  144. it( 'should return second operation if it is MoveOperation', () => {
  145. const p = new Element( 'p' );
  146. const move = new MoveOperation( new Position( root, [ 1, 1, 4 ] ), 4, new Position( root, [ 1, 2, 0 ] ), 1 );
  147. splitDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2 ] ), p, 0 ) );
  148. splitDelta.operations.push( move );
  149. expect( splitDelta._moveOperation ).to.equal( move );
  150. } );
  151. } );
  152. it( 'should provide proper className', () => {
  153. expect( SplitDelta.className ).to.equal( 'engine.model.delta.SplitDelta' );
  154. } );
  155. } );