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