insertdelta.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 Element from '../../../src/model/element';
  7. import DocumentFragment from '../../../src/model/documentfragment';
  8. import Text from '../../../src/model/text';
  9. import Position from '../../../src/model/position';
  10. import Range from '../../../src/model/range';
  11. import InsertOperation from '../../../src/model/operation/insertoperation';
  12. import MarkerOperation from '../../../src/model/operation/markeroperation';
  13. import InsertDelta from '../../../src/model/delta/insertdelta';
  14. import RemoveDelta from '../../../src/model/delta/removedelta';
  15. import RemoveOperation from '../../../src/model/operation/removeoperation';
  16. import { stringify } from '../../../src/dev-utils/model';
  17. describe( 'Batch', () => {
  18. let doc, root, batch, p, ul, chain;
  19. beforeEach( () => {
  20. doc = new Document();
  21. root = doc.createRoot();
  22. root.insertChildren( 0, new Text( 'abc' ) );
  23. batch = doc.batch();
  24. p = new Element( 'p' );
  25. ul = new Element( 'ul' );
  26. chain = batch.insert( new Position( root, [ 2 ] ), [ p, ul ] );
  27. } );
  28. describe( 'insert', () => {
  29. it( 'should insert given nodes at given position', () => {
  30. expect( root.childCount ).to.equal( 4 );
  31. expect( root.maxOffset ).to.equal( 5 );
  32. expect( root.getChild( 1 ) ).to.equal( p );
  33. expect( root.getChild( 2 ) ).to.equal( ul );
  34. } );
  35. it( 'should be chainable', () => {
  36. expect( chain ).to.equal( batch );
  37. } );
  38. it( 'should add delta to batch and operation to delta before applying operation', () => {
  39. sinon.spy( doc, 'applyOperation' );
  40. batch.insert( new Position( root, [ 2 ] ), [ p, ul ] );
  41. const correctDeltaMatcher = sinon.match( operation => {
  42. return operation.delta && operation.delta.batch && operation.delta.batch == batch;
  43. } );
  44. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  45. } );
  46. it( 'should transfer markers from given DocumentFragment', () => {
  47. const documentFragment = new DocumentFragment( [ new Element( 'li', null, [ new Text( 'foo bar' ) ] ) ] );
  48. const marker = new Range( new Position( documentFragment, [ 0, 1 ] ), new Position( documentFragment, [ 0, 5 ] ) );
  49. documentFragment.markers.set( 'marker', marker );
  50. batch.insert( new Position( root, [ 3, 0 ] ), documentFragment );
  51. expect( Array.from( doc.markers ).length ).to.equal( 1 );
  52. expect( stringify( root, doc.markers.get( 'marker' ).getRange() ) ).to.equal( 'ab<p></p><ul><li>f[oo b]ar</li></ul>c' );
  53. } );
  54. it( 'should set each marker as separate operation', () => {
  55. sinon.spy( doc, 'applyOperation' );
  56. const documentFragment = new DocumentFragment( [ new Element( 'li', null, [ new Text( 'foo bar' ) ] ) ] );
  57. const marker1 = new Range( new Position( documentFragment, [ 0, 1 ] ), new Position( documentFragment, [ 0, 2 ] ) );
  58. const marker2 = new Range( new Position( documentFragment, [ 0, 5 ] ), new Position( documentFragment, [ 0, 6 ] ) );
  59. documentFragment.markers.set( 'marker1', marker1 );
  60. documentFragment.markers.set( 'marker2', marker2 );
  61. batch.insert( new Position( root, [ 3, 0 ] ), documentFragment );
  62. expect( doc.applyOperation.calledThrice );
  63. expect( doc.applyOperation.firstCall.calledWith( sinon.match( operation => operation instanceof InsertOperation ) ) );
  64. expect( doc.applyOperation.secondCall.calledWith( sinon.match( operation => operation instanceof MarkerOperation ) ) );
  65. expect( doc.applyOperation.thirdCall.calledWith( sinon.match( operation => operation instanceof MarkerOperation ) ) );
  66. } );
  67. it( 'should not create a delta and an operation if no nodes were inserted', () => {
  68. sinon.spy( doc, 'applyOperation' );
  69. batch = doc.batch();
  70. batch.insert( new Position( root, [ 0 ] ), [] );
  71. expect( batch.deltas.length ).to.equal( 0 );
  72. expect( doc.applyOperation.called ).to.be.false;
  73. } );
  74. } );
  75. } );
  76. describe( 'InsertDelta', () => {
  77. let insertDelta, doc, root;
  78. beforeEach( () => {
  79. doc = new Document();
  80. root = doc.createRoot();
  81. insertDelta = new InsertDelta();
  82. } );
  83. describe( 'constructor()', () => {
  84. it( 'should create insert delta with no operations added', () => {
  85. expect( insertDelta.operations.length ).to.equal( 0 );
  86. } );
  87. } );
  88. describe( 'type', () => {
  89. it( 'should be equal to insert', () => {
  90. expect( insertDelta.type ).to.equal( 'insert' );
  91. } );
  92. } );
  93. describe( 'position', () => {
  94. it( 'should be null if there are no operations in delta', () => {
  95. expect( insertDelta.position ).to.be.null;
  96. } );
  97. it( 'should be equal to the position where node is inserted', () => {
  98. insertDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2, 3 ] ), new Element( 'x' ), 0 ) );
  99. expect( insertDelta.position.root ).to.equal( root );
  100. expect( insertDelta.position.path ).to.deep.equal( [ 1, 2, 3 ] );
  101. } );
  102. } );
  103. describe( 'nodes', () => {
  104. it( 'should be null if there are no operations in delta', () => {
  105. expect( insertDelta.nodes ).to.be.null;
  106. } );
  107. it( 'should be equal to the nodes inserted by the delta', () => {
  108. const elementX = new Element( 'x' );
  109. insertDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2, 3 ] ), elementX, 0 ) );
  110. expect( insertDelta.nodes.length ).to.equal( 1 );
  111. expect( insertDelta.nodes.getNode( 0 ) ).to.equal( elementX );
  112. } );
  113. } );
  114. describe( 'getReversed', () => {
  115. it( 'should return empty RemoveDelta if there are no operations in delta', () => {
  116. const reversed = insertDelta.getReversed();
  117. expect( reversed ).to.be.instanceof( RemoveDelta );
  118. expect( reversed.operations.length ).to.equal( 0 );
  119. } );
  120. it( 'should return correct RemoveDelta', () => {
  121. const position = new Position( root, [ 1, 2, 3 ] );
  122. const elementX = new Element( 'x' );
  123. insertDelta.operations.push( new InsertOperation( position, elementX, 0 ) );
  124. const reversed = insertDelta.getReversed();
  125. expect( reversed ).to.be.instanceof( RemoveDelta );
  126. expect( reversed.operations.length ).to.equal( 1 );
  127. expect( reversed.operations[ 0 ] ).to.be.instanceof( RemoveOperation );
  128. expect( reversed.operations[ 0 ].sourcePosition.isEqual( position ) ).to.be.true;
  129. expect( reversed.operations[ 0 ].howMany ).to.equal( 1 );
  130. } );
  131. } );
  132. it( 'should provide proper className', () => {
  133. expect( InsertDelta.className ).to.equal( 'engine.model.delta.InsertDelta' );
  134. } );
  135. } );