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