8
0

splitdelta.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 CKEditorError from '/ckeditor5/utils/ckeditorerror.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. describe( 'Batch', () => {
  16. let doc, root, p;
  17. beforeEach( () => {
  18. doc = new Document();
  19. root = doc.createRoot();
  20. p = new Element( 'p', { key: 'value' }, 'foobar' );
  21. root.insertChildren( 0, p );
  22. } );
  23. describe( 'split', () => {
  24. it( 'should split foobar to foo and bar', () => {
  25. doc.batch().split( new Position( root, [ 0, 3 ] ) );
  26. expect( root.getChildCount() ).to.equal( 2 );
  27. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  28. expect( root.getChild( 0 ).getChildCount() ).to.equal( 3 );
  29. expect( root.getChild( 0 )._attrs.size ).to.equal( 1 );
  30. expect( root.getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
  31. expect( root.getChild( 0 ).getChild( 0 ).character ).to.equal( 'f' );
  32. expect( root.getChild( 0 ).getChild( 1 ).character ).to.equal( 'o' );
  33. expect( root.getChild( 0 ).getChild( 2 ).character ).to.equal( 'o' );
  34. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  35. expect( root.getChild( 1 ).getChildCount() ).to.equal( 3 );
  36. expect( root.getChild( 1 )._attrs.size ).to.equal( 1 );
  37. expect( root.getChild( 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
  38. expect( root.getChild( 1 ).getChild( 0 ).character ).to.equal( 'b' );
  39. expect( root.getChild( 1 ).getChild( 1 ).character ).to.equal( 'a' );
  40. expect( root.getChild( 1 ).getChild( 2 ).character ).to.equal( 'r' );
  41. } );
  42. it( 'should create an empty paragraph if we split at the end', () => {
  43. doc.batch().split( new Position( root, [ 0, 6 ] ) );
  44. expect( root.getChildCount() ).to.equal( 2 );
  45. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  46. expect( root.getChild( 0 ).getChildCount() ).to.equal( 6 );
  47. expect( root.getChild( 0 )._attrs.size ).to.equal( 1 );
  48. expect( root.getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
  49. expect( root.getChild( 0 ).getChild( 0 ).character ).to.equal( 'f' );
  50. expect( root.getChild( 0 ).getChild( 1 ).character ).to.equal( 'o' );
  51. expect( root.getChild( 0 ).getChild( 2 ).character ).to.equal( 'o' );
  52. expect( root.getChild( 0 ).getChild( 3 ).character ).to.equal( 'b' );
  53. expect( root.getChild( 0 ).getChild( 4 ).character ).to.equal( 'a' );
  54. expect( root.getChild( 0 ).getChild( 5 ).character ).to.equal( 'r' );
  55. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  56. expect( root.getChild( 1 ).getChildCount() ).to.equal( 0 );
  57. expect( root.getChild( 1 )._attrs.size ).to.equal( 1 );
  58. expect( root.getChild( 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
  59. } );
  60. it( 'should throw if we try to split a root', () => {
  61. expect( () => {
  62. doc.batch().split( new Position( root, [ 0 ] ) );
  63. } ).to.throw( CKEditorError, /^batch-split-root/ );
  64. } );
  65. it( 'should be chainable', () => {
  66. const batch = doc.batch();
  67. const chain = batch.split( new Position( root, [ 0, 3 ] ) );
  68. expect( chain ).to.equal( batch );
  69. } );
  70. it( 'should add delta to batch and operation to delta before applying operation', () => {
  71. sinon.spy( doc, 'applyOperation' );
  72. const batch = doc.batch().split( new Position( root, [ 0, 3 ] ) );
  73. const correctDeltaMatcher = sinon.match( ( operation ) => {
  74. return operation.delta && operation.delta.batch && operation.delta.batch == batch;
  75. } );
  76. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  77. } );
  78. } );
  79. } );
  80. describe( 'SplitDelta', () => {
  81. let splitDelta, doc, root;
  82. beforeEach( () => {
  83. doc = new Document();
  84. root = doc.createRoot();
  85. splitDelta = new SplitDelta();
  86. } );
  87. describe( 'constructor', () => {
  88. it( 'should create split delta with no operations added', () => {
  89. expect( splitDelta.operations.length ).to.equal( 0 );
  90. } );
  91. } );
  92. describe( 'position', () => {
  93. it( 'should be null if there are no operations in delta', () => {
  94. expect( splitDelta.position ).to.be.null;
  95. } );
  96. it( 'should be equal to the position where node is split', () => {
  97. splitDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2 ] ), new Element( 'p' ), 0 ) );
  98. splitDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1, 4 ] ), 4, new Position( root, [ 1, 2, 0 ] ), 1 ) );
  99. expect( splitDelta.position.root ).to.equal( root );
  100. expect( splitDelta.position.path ).to.deep.equal( [ 1, 1, 4 ] );
  101. } );
  102. } );
  103. describe( 'getReversed', () => {
  104. it( 'should return empty MergeDelta if there are no operations in delta', () => {
  105. let reversed = splitDelta.getReversed();
  106. expect( reversed ).to.be.instanceof( MergeDelta );
  107. expect( reversed.operations.length ).to.equal( 0 );
  108. } );
  109. it( 'should return correct SplitDelta', () => {
  110. splitDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2 ] ), new Element( 'p' ), 0 ) );
  111. splitDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1, 4 ] ), 4, new Position( root, [ 1, 2, 0 ] ), 1 ) );
  112. let reversed = splitDelta.getReversed();
  113. expect( reversed ).to.be.instanceof( MergeDelta );
  114. expect( reversed.operations.length ).to.equal( 2 );
  115. expect( reversed.operations[ 0 ] ).to.be.instanceof( MoveOperation );
  116. expect( reversed.operations[ 0 ].sourcePosition.path ).to.deep.equal( [ 1, 2, 0 ] );
  117. expect( reversed.operations[ 0 ].howMany ).to.equal( 4 );
  118. expect( reversed.operations[ 0 ].targetPosition.path ).to.deep.equal( [ 1, 1, 4 ] );
  119. expect( reversed.operations[ 1 ] ).to.be.instanceof( RemoveOperation );
  120. expect( reversed.operations[ 1 ].sourcePosition.path ).to.deep.equal( [ 1, 2 ] );
  121. expect( reversed.operations[ 1 ].howMany ).to.equal( 1 );
  122. } );
  123. } );
  124. describe( '_cloneOperation', () => {
  125. it( 'should return null if delta has no operations', () => {
  126. expect( splitDelta._cloneOperation ).to.be.null;
  127. } );
  128. it( 'should return the first operation in the delta, which is InsertOperation or ReinsertOperation', () => {
  129. let p = new Element( 'p' );
  130. splitDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2 ] ), p, 0 ) );
  131. splitDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1, 4 ] ), 4, new Position( root, [ 1, 2, 0 ] ), 1 ) );
  132. expect( splitDelta._cloneOperation ).to.be.instanceof( InsertOperation );
  133. expect( splitDelta._cloneOperation.nodeList.get( 0 ) ).to.equal( p );
  134. expect( splitDelta._cloneOperation.position.path ).to.deep.equal( [ 1, 2 ] );
  135. } );
  136. } );
  137. it( 'should provide proper className', () => {
  138. expect( SplitDelta.className ).to.equal( 'engine.model.delta.SplitDelta' );
  139. } );
  140. } );