8
0

insertdelta.js 4.0 KB

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