movedelta.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 { getNodesAndText } from '/tests/engine/model/_utils/utils.js';
  7. import Document from '/ckeditor5/engine/model/document.js';
  8. import Position from '/ckeditor5/engine/model/position.js';
  9. import Range from '/ckeditor5/engine/model/range.js';
  10. import Element from '/ckeditor5/engine/model/element.js';
  11. import Text from '/ckeditor5/engine/model/text.js';
  12. import MoveDelta from '/ckeditor5/engine/model/delta/movedelta.js';
  13. import MoveOperation from '/ckeditor5/engine/model/operation/moveoperation.js';
  14. describe( 'Batch', () => {
  15. let doc, root, div, p, batch, chain;
  16. beforeEach( () => {
  17. doc = new Document();
  18. root = doc.createRoot();
  19. div = new Element( 'div', [], new Text( 'foobar' ) );
  20. p = new Element( 'p', [], new Text( 'abcxyz' ) );
  21. div.insertChildren( 0, [ new Element( 'p', [], new Text( 'gggg' ) ) ] );
  22. div.insertChildren( 2, [ new Element( 'p', [], new Text( 'hhhh' ) ) ] );
  23. root.insertChildren( 0, [ div, p ] );
  24. batch = doc.batch();
  25. } );
  26. describe( 'move', () => {
  27. it( 'should move specified node', () => {
  28. batch.move( div, new Position( root, [ 2 ] ) );
  29. expect( root.maxOffset ).to.equal( 2 );
  30. expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'abcxyz' );
  31. expect( getNodesAndText( Range.createIn( root.getChild( 1 ) ) ) ).to.equal( 'PggggPfoobarPhhhhP' );
  32. } );
  33. it( 'should move flat range of nodes', () => {
  34. let range = new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 0, 7 ] ) );
  35. batch.move( range, new Position( root, [ 1, 3 ] ) );
  36. expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'PggggPfoPhhhhP' );
  37. expect( getNodesAndText( Range.createIn( root.getChild( 1 ) ) ) ).to.equal( 'abcobarxyz' );
  38. } );
  39. it( 'should throw if given range is not flat', () => {
  40. let notFlatRange = new Range( new Position( root, [ 0, 2, 2 ] ), new Position( root, [ 0, 6 ] ) );
  41. expect( () => {
  42. doc.batch().move( notFlatRange, new Position( root, [ 1, 3 ] ) );
  43. } ).to.throwCKEditorError( /^batch-move-range-not-flat/ );
  44. } );
  45. it( 'should be chainable', () => {
  46. chain = batch.move( div, new Position( root, [ 1, 3 ] ) );
  47. expect( chain ).to.equal( batch );
  48. } );
  49. it( 'should add delta to batch and operation to delta before applying operation', () => {
  50. sinon.spy( doc, 'applyOperation' );
  51. batch.move( div, new Position( root, [ 2 ] ) );
  52. const correctDeltaMatcher = sinon.match( ( operation ) => {
  53. return operation.delta && operation.delta.batch && operation.delta.batch == batch;
  54. } );
  55. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  56. } );
  57. } );
  58. } );
  59. describe( 'MoveDelta', () => {
  60. let moveDelta, doc, root;
  61. beforeEach( () => {
  62. doc = new Document();
  63. root = doc.createRoot();
  64. moveDelta = new MoveDelta();
  65. } );
  66. describe( 'constructor', () => {
  67. it( 'should create move delta with no operations added', () => {
  68. expect( moveDelta.operations.length ).to.equal( 0 );
  69. } );
  70. } );
  71. describe( 'sourcePosition', () => {
  72. it( 'should be null if there are no operations in delta', () => {
  73. expect( moveDelta.sourcePosition ).to.be.null;
  74. } );
  75. it( 'should be equal to the position where node is inserted', () => {
  76. moveDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), 2, new Position( root, [ 2, 2 ] ), 0 ) );
  77. expect( moveDelta.sourcePosition.root ).to.equal( root );
  78. expect( moveDelta.sourcePosition.path ).to.deep.equal( [ 1, 1 ] );
  79. } );
  80. } );
  81. describe( 'howMany', () => {
  82. it( 'should be null if there are no operations in delta', () => {
  83. expect( moveDelta.howMany ).to.be.null;
  84. } );
  85. it( 'should be equal to the position where node is inserted', () => {
  86. moveDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), 2, new Position( root, [ 2, 2 ] ), 0 ) );
  87. expect( moveDelta.howMany ).to.equal( 2 );
  88. } );
  89. } );
  90. describe( 'targetPosition', () => {
  91. it( 'should be null if there are no operations in delta', () => {
  92. expect( moveDelta.targetPosition ).to.be.null;
  93. } );
  94. it( 'should be equal to the move operation\'s target position', () => {
  95. moveDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), 2, new Position( root, [ 2, 2 ] ), 0 ) );
  96. expect( moveDelta.targetPosition.root ).to.equal( root );
  97. expect( moveDelta.targetPosition.path ).to.deep.equal( [ 2, 2 ] );
  98. } );
  99. } );
  100. describe( 'getReversed', () => {
  101. it( 'should return empty MoveDelta if there are no operations in delta', () => {
  102. let reversed = moveDelta.getReversed();
  103. expect( reversed ).to.be.instanceof( MoveDelta );
  104. expect( reversed.operations.length ).to.equal( 0 );
  105. } );
  106. it( 'should return correct MoveDelta', () => {
  107. moveDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), 2, new Position( root, [ 2, 2 ] ), 0 ) );
  108. let reversed = moveDelta.getReversed();
  109. expect( reversed ).to.be.instanceof( MoveDelta );
  110. expect( reversed.operations.length ).to.equal( 1 );
  111. expect( reversed.operations[ 0 ] ).to.be.instanceof( MoveOperation );
  112. expect( reversed.operations[ 0 ].sourcePosition.path ).to.deep.equal( [ 2, 2 ] );
  113. expect( reversed.operations[ 0 ].howMany ).to.equal( 2 );
  114. expect( reversed.operations[ 0 ].targetPosition.path ).to.deep.equal( [ 1, 1 ] );
  115. } );
  116. } );
  117. it( 'should provide proper className', () => {
  118. expect( MoveDelta.className ).to.equal( 'engine.model.delta.MoveDelta' );
  119. } );
  120. } );