splitdelta.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 Position from '/ckeditor5/engine/model/position.js';
  9. import Element from '/ckeditor5/engine/model/element.js';
  10. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  11. import MergeDelta from '/ckeditor5/engine/model/delta/mergedelta.js';
  12. import SplitDelta from '/ckeditor5/engine/model/delta/splitdelta.js';
  13. import InsertOperation from '/ckeditor5/engine/model/operation/insertoperation.js';
  14. import MoveOperation from '/ckeditor5/engine/model/operation/moveoperation.js';
  15. import RemoveOperation from '/ckeditor5/engine/model/operation/removeoperation.js';
  16. describe( 'Batch', () => {
  17. let doc, root, p;
  18. beforeEach( () => {
  19. doc = new Document();
  20. root = doc.createRoot( '$root', 'root' );
  21. p = new Element( 'p', { key: 'value' }, 'foobar' );
  22. root.insertChildren( 0, p );
  23. } );
  24. describe( 'split', () => {
  25. it( 'should split foobar to foo and bar', () => {
  26. doc.batch().split( new Position( root, [ 0, 3 ] ) );
  27. expect( root.getChildCount() ).to.equal( 2 );
  28. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  29. expect( root.getChild( 0 ).getChildCount() ).to.equal( 3 );
  30. expect( root.getChild( 0 )._attrs.size ).to.equal( 1 );
  31. expect( root.getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
  32. expect( root.getChild( 0 ).getChild( 0 ).character ).to.equal( 'f' );
  33. expect( root.getChild( 0 ).getChild( 1 ).character ).to.equal( 'o' );
  34. expect( root.getChild( 0 ).getChild( 2 ).character ).to.equal( 'o' );
  35. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  36. expect( root.getChild( 1 ).getChildCount() ).to.equal( 3 );
  37. expect( root.getChild( 1 )._attrs.size ).to.equal( 1 );
  38. expect( root.getChild( 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
  39. expect( root.getChild( 1 ).getChild( 0 ).character ).to.equal( 'b' );
  40. expect( root.getChild( 1 ).getChild( 1 ).character ).to.equal( 'a' );
  41. expect( root.getChild( 1 ).getChild( 2 ).character ).to.equal( 'r' );
  42. } );
  43. it( 'should create an empty paragraph if we split at the end', () => {
  44. doc.batch().split( new Position( root, [ 0, 6 ] ) );
  45. expect( root.getChildCount() ).to.equal( 2 );
  46. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  47. expect( root.getChild( 0 ).getChildCount() ).to.equal( 6 );
  48. expect( root.getChild( 0 )._attrs.size ).to.equal( 1 );
  49. expect( root.getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
  50. expect( root.getChild( 0 ).getChild( 0 ).character ).to.equal( 'f' );
  51. expect( root.getChild( 0 ).getChild( 1 ).character ).to.equal( 'o' );
  52. expect( root.getChild( 0 ).getChild( 2 ).character ).to.equal( 'o' );
  53. expect( root.getChild( 0 ).getChild( 3 ).character ).to.equal( 'b' );
  54. expect( root.getChild( 0 ).getChild( 4 ).character ).to.equal( 'a' );
  55. expect( root.getChild( 0 ).getChild( 5 ).character ).to.equal( 'r' );
  56. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  57. expect( root.getChild( 1 ).getChildCount() ).to.equal( 0 );
  58. expect( root.getChild( 1 )._attrs.size ).to.equal( 1 );
  59. expect( root.getChild( 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
  60. } );
  61. it( 'should throw if we try to split a root', () => {
  62. expect( () => {
  63. doc.batch().split( new Position( root, [ 0 ] ) );
  64. } ).to.throw( CKEditorError, /^batch-split-root/ );
  65. } );
  66. it( 'should be chainable', () => {
  67. const batch = doc.batch();
  68. const chain = batch.split( new Position( root, [ 0, 3 ] ) );
  69. expect( chain ).to.equal( batch );
  70. } );
  71. it( 'should add delta to batch and operation to delta before applying operation', () => {
  72. sinon.spy( doc, 'applyOperation' );
  73. const batch = doc.batch().split( new Position( root, [ 0, 3 ] ) );
  74. const correctDeltaMatcher = sinon.match( ( operation ) => {
  75. return operation.delta && operation.delta.batch && operation.delta.batch == batch;
  76. } );
  77. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  78. } );
  79. } );
  80. } );
  81. describe( 'SplitDelta', () => {
  82. let splitDelta, doc, root;
  83. beforeEach( () => {
  84. doc = new Document();
  85. root = doc.createRoot( '$root', 'root' );
  86. splitDelta = new SplitDelta();
  87. } );
  88. describe( 'constructor', () => {
  89. it( 'should create split delta with no operations added', () => {
  90. expect( splitDelta.operations.length ).to.equal( 0 );
  91. } );
  92. } );
  93. describe( 'position', () => {
  94. it( 'should be null if there are no operations in delta', () => {
  95. expect( splitDelta.position ).to.be.null;
  96. } );
  97. it( 'should be equal to the position where node is split', () => {
  98. splitDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2 ] ), new Element( 'p' ), 0 ) );
  99. splitDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1, 4 ] ), 4, new Position( root, [ 1, 2, 0 ] ), 1 ) );
  100. expect( splitDelta.position.root ).to.equal( root );
  101. expect( splitDelta.position.path ).to.deep.equal( [ 1, 1, 4 ] );
  102. } );
  103. } );
  104. describe( 'getReversed', () => {
  105. it( 'should return empty MergeDelta if there are no operations in delta', () => {
  106. let reversed = splitDelta.getReversed();
  107. expect( reversed ).to.be.instanceof( MergeDelta );
  108. expect( reversed.operations.length ).to.equal( 0 );
  109. } );
  110. it( 'should return correct SplitDelta', () => {
  111. splitDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2 ] ), new Element( 'p' ), 0 ) );
  112. splitDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1, 4 ] ), 4, new Position( root, [ 1, 2, 0 ] ), 1 ) );
  113. let reversed = splitDelta.getReversed();
  114. expect( reversed ).to.be.instanceof( MergeDelta );
  115. expect( reversed.operations.length ).to.equal( 2 );
  116. expect( reversed.operations[ 0 ] ).to.be.instanceof( MoveOperation );
  117. expect( reversed.operations[ 0 ].sourcePosition.path ).to.deep.equal( [ 1, 2, 0 ] );
  118. expect( reversed.operations[ 0 ].howMany ).to.equal( 4 );
  119. expect( reversed.operations[ 0 ].targetPosition.path ).to.deep.equal( [ 1, 1, 4 ] );
  120. expect( reversed.operations[ 1 ] ).to.be.instanceof( RemoveOperation );
  121. expect( reversed.operations[ 1 ].sourcePosition.path ).to.deep.equal( [ 1, 2 ] );
  122. expect( reversed.operations[ 1 ].howMany ).to.equal( 1 );
  123. } );
  124. } );
  125. describe( '_cloneOperation', () => {
  126. it( 'should return null if delta has no operations', () => {
  127. expect( splitDelta._cloneOperation ).to.be.null;
  128. } );
  129. it( 'should return the first operation in the delta, which is InsertOperation or ReinsertOperation', () => {
  130. let p = new Element( 'p' );
  131. splitDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2 ] ), p, 0 ) );
  132. splitDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1, 4 ] ), 4, new Position( root, [ 1, 2, 0 ] ), 1 ) );
  133. expect( splitDelta._cloneOperation ).to.be.instanceof( InsertOperation );
  134. expect( splitDelta._cloneOperation.nodeList.get( 0 ) ).to.equal( p );
  135. expect( splitDelta._cloneOperation.position.path ).to.deep.equal( [ 1, 2 ] );
  136. } );
  137. } );
  138. it( 'should provide proper className', () => {
  139. expect( SplitDelta.className ).to.equal( 'engine.model.delta.SplitDelta' );
  140. } );
  141. } );