insertdelta.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 Element from '/ckeditor5/engine/model/element.js';
  8. import Position from '/ckeditor5/engine/model/position.js';
  9. import Text from '/ckeditor5/engine/model/text.js';
  10. import InsertOperation from '/ckeditor5/engine/model/operation/insertoperation.js';
  11. import InsertDelta from '/ckeditor5/engine/model/delta/insertdelta.js';
  12. import RemoveDelta from '/ckeditor5/engine/model/delta/removedelta.js';
  13. import RemoveOperation from '/ckeditor5/engine/model/operation/removeoperation.js';
  14. describe( 'Batch', () => {
  15. let doc, root, batch, p, ul, chain;
  16. beforeEach( () => {
  17. doc = new Document();
  18. root = doc.createRoot();
  19. root.insertChildren( 0, new Text( 'abc' ) );
  20. batch = doc.batch();
  21. p = new Element( 'p' );
  22. ul = new Element( 'ul' );
  23. chain = batch.insert( new Position( root, [ 2 ] ), [ p, ul ] );
  24. } );
  25. describe( 'insert', () => {
  26. it( 'should insert given nodes at given position', () => {
  27. expect( root.childCount ).to.equal( 4 );
  28. expect( root.maxOffset ).to.equal( 5 );
  29. expect( root.getChild( 1 ) ).to.equal( p );
  30. expect( root.getChild( 2 ) ).to.equal( ul );
  31. } );
  32. it( 'should be chainable', () => {
  33. expect( chain ).to.equal( batch );
  34. } );
  35. it( 'should add delta to batch and operation to delta before applying operation', () => {
  36. sinon.spy( doc, 'applyOperation' );
  37. batch.insert( new Position( root, [ 2 ] ), [ p, ul ] );
  38. const correctDeltaMatcher = sinon.match( ( operation ) => {
  39. return operation.delta && operation.delta.batch && operation.delta.batch == batch;
  40. } );
  41. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  42. } );
  43. } );
  44. } );
  45. describe( 'InsertDelta', () => {
  46. let insertDelta, doc, root;
  47. beforeEach( () => {
  48. doc = new Document();
  49. root = doc.createRoot();
  50. insertDelta = new InsertDelta();
  51. } );
  52. describe( 'constructor', () => {
  53. it( 'should create insert delta with no operations added', () => {
  54. expect( insertDelta.operations.length ).to.equal( 0 );
  55. } );
  56. } );
  57. describe( 'position', () => {
  58. it( 'should be null if there are no operations in delta', () => {
  59. expect( insertDelta.position ).to.be.null;
  60. } );
  61. it( 'should be equal to the position where node is inserted', () => {
  62. insertDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2, 3 ] ), new Element( 'x' ), 0 ) );
  63. expect( insertDelta.position.root ).to.equal( root );
  64. expect( insertDelta.position.path ).to.deep.equal( [ 1, 2, 3 ] );
  65. } );
  66. } );
  67. describe( 'nodes', () => {
  68. it( 'should be null if there are no operations in delta', () => {
  69. expect( insertDelta.nodes ).to.be.null;
  70. } );
  71. it( 'should be equal to the nodes inserted by the delta', () => {
  72. let elementX = new Element( 'x' );
  73. insertDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2, 3 ] ), elementX, 0 ) );
  74. expect( insertDelta.nodes.length ).to.equal( 1 );
  75. expect( insertDelta.nodes.getNode( 0 ) ).to.equal( elementX );
  76. } );
  77. } );
  78. describe( 'getReversed', () => {
  79. it( 'should return empty RemoveDelta if there are no operations in delta', () => {
  80. let reversed = insertDelta.getReversed();
  81. expect( reversed ).to.be.instanceof( RemoveDelta );
  82. expect( reversed.operations.length ).to.equal( 0 );
  83. } );
  84. it( 'should return correct RemoveDelta', () => {
  85. let position = new Position( root, [ 1, 2, 3 ] );
  86. let elementX = new Element( 'x' );
  87. insertDelta.operations.push( new InsertOperation( position, elementX, 0 ) );
  88. let reversed = insertDelta.getReversed();
  89. expect( reversed ).to.be.instanceof( RemoveDelta );
  90. expect( reversed.operations.length ).to.equal( 1 );
  91. expect( reversed.operations[ 0 ] ).to.be.instanceof( RemoveOperation );
  92. expect( reversed.operations[ 0 ].sourcePosition.isEqual( position ) ).to.be.true;
  93. expect( reversed.operations[ 0 ].howMany ).to.equal( 1 );
  94. } );
  95. } );
  96. it( 'should provide proper className', () => {
  97. expect( InsertDelta.className ).to.equal( 'engine.model.delta.InsertDelta' );
  98. } );
  99. } );