8
0

splitdelta.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  11. import MergeDelta from '/ckeditor5/engine/model/delta/mergedelta.js';
  12. import SplitDelta from '/ckeditor5/engine/model/delta/splitdelta.js';
  13. import InsertOperation from '/ckeditor5/engine/model/operation/insertoperation.js';
  14. import MoveOperation from '/ckeditor5/engine/model/operation/moveoperation.js';
  15. import RemoveOperation from '/ckeditor5/engine/model/operation/removeoperation.js';
  16. import count from '/ckeditor5/utils/count.js';
  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( 'position', () => {
  86. it( 'should be null if there are no operations in delta', () => {
  87. expect( splitDelta.position ).to.be.null;
  88. } );
  89. it( 'should be equal to the position where node is split', () => {
  90. splitDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2 ] ), new Element( 'p' ), 0 ) );
  91. splitDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1, 4 ] ), 4, new Position( root, [ 1, 2, 0 ] ), 1 ) );
  92. expect( splitDelta.position.root ).to.equal( root );
  93. expect( splitDelta.position.path ).to.deep.equal( [ 1, 1, 4 ] );
  94. } );
  95. } );
  96. describe( 'getReversed', () => {
  97. it( 'should return empty MergeDelta if there are no operations in delta', () => {
  98. let reversed = splitDelta.getReversed();
  99. expect( reversed ).to.be.instanceof( MergeDelta );
  100. expect( reversed.operations.length ).to.equal( 0 );
  101. } );
  102. it( 'should return correct SplitDelta', () => {
  103. splitDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2 ] ), new Element( 'p' ), 0 ) );
  104. splitDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1, 4 ] ), 4, new Position( root, [ 1, 2, 0 ] ), 1 ) );
  105. let reversed = splitDelta.getReversed();
  106. expect( reversed ).to.be.instanceof( MergeDelta );
  107. expect( reversed.operations.length ).to.equal( 2 );
  108. expect( reversed.operations[ 0 ] ).to.be.instanceof( MoveOperation );
  109. expect( reversed.operations[ 0 ].sourcePosition.path ).to.deep.equal( [ 1, 2, 0 ] );
  110. expect( reversed.operations[ 0 ].howMany ).to.equal( 4 );
  111. expect( reversed.operations[ 0 ].targetPosition.path ).to.deep.equal( [ 1, 1, 4 ] );
  112. expect( reversed.operations[ 1 ] ).to.be.instanceof( RemoveOperation );
  113. expect( reversed.operations[ 1 ].sourcePosition.path ).to.deep.equal( [ 1, 2 ] );
  114. expect( reversed.operations[ 1 ].howMany ).to.equal( 1 );
  115. } );
  116. } );
  117. describe( '_cloneOperation', () => {
  118. it( 'should return null if delta has no operations', () => {
  119. expect( splitDelta._cloneOperation ).to.be.null;
  120. } );
  121. it( 'should return the first operation in the delta, which is InsertOperation or ReinsertOperation', () => {
  122. let p = new Element( 'p' );
  123. splitDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2 ] ), p, 0 ) );
  124. splitDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1, 4 ] ), 4, new Position( root, [ 1, 2, 0 ] ), 1 ) );
  125. expect( splitDelta._cloneOperation ).to.be.instanceof( InsertOperation );
  126. expect( splitDelta._cloneOperation.nodes.getNode( 0 ) ).to.equal( p );
  127. expect( splitDelta._cloneOperation.position.path ).to.deep.equal( [ 1, 2 ] );
  128. } );
  129. } );
  130. it( 'should provide proper className', () => {
  131. expect( SplitDelta.className ).to.equal( 'engine.model.delta.SplitDelta' );
  132. } );
  133. } );