splitdelta.js 6.5 KB

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