8
0

movedelta.js 5.6 KB

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