wrapdelta.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 Document from '/ckeditor5/engine/model/document.js';
  7. import Position from '/ckeditor5/engine/model/position.js';
  8. import Range from '/ckeditor5/engine/model/range.js';
  9. import Element from '/ckeditor5/engine/model/element.js';
  10. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  11. import WrapDelta from '/ckeditor5/engine/model/delta/wrapdelta.js';
  12. import UnwrapDelta from '/ckeditor5/engine/model/delta/unwrapdelta.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, range;
  18. beforeEach( () => {
  19. doc = new Document();
  20. root = doc.createRoot();
  21. root.insertChildren( 0, 'foobar' );
  22. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
  23. } );
  24. describe( 'wrap', () => {
  25. it( 'should wrap flat range with given element', () => {
  26. let p = new Element( 'p' );
  27. doc.batch().wrap( range, p );
  28. expect( root.getChildCount() ).to.equal( 5 );
  29. expect( root.getChild( 0 ).character ).to.equal( 'f' );
  30. expect( root.getChild( 1 ).character ).to.equal( 'o' );
  31. expect( root.getChild( 2 ) ).to.equal( p );
  32. expect( p.getChild( 0 ).character ).to.equal( 'o' );
  33. expect( p.getChild( 1 ).character ).to.equal( 'b' );
  34. expect( root.getChild( 3 ).character ).to.equal( 'a' );
  35. expect( root.getChild( 4 ).character ).to.equal( 'r' );
  36. } );
  37. it( 'should wrap flat range with an element of given name', () => {
  38. doc.batch().wrap( range, 'p' );
  39. expect( root.getChildCount() ).to.equal( 5 );
  40. expect( root.getChild( 0 ).character ).to.equal( 'f' );
  41. expect( root.getChild( 1 ).character ).to.equal( 'o' );
  42. expect( root.getChild( 2 ).name ).to.equal( 'p' );
  43. expect( root.getChild( 2 ).getChild( 0 ).character ).to.equal( 'o' );
  44. expect( root.getChild( 2 ).getChild( 1 ).character ).to.equal( 'b' );
  45. expect( root.getChild( 3 ).character ).to.equal( 'a' );
  46. expect( root.getChild( 4 ).character ).to.equal( 'r' );
  47. } );
  48. it( 'should throw if range to wrap is not flat', () => {
  49. root.insertChildren( 6, [ new Element( 'p', [], 'xyz' ) ] );
  50. let notFlatRange = new Range( new Position( root, [ 3 ] ), new Position( root, [ 6, 2 ] ) );
  51. expect( () => {
  52. doc.batch().wrap( notFlatRange, 'p' );
  53. } ).to.throw( CKEditorError, /^batch-wrap-range-not-flat/ );
  54. } );
  55. it( 'should throw if element to wrap with has children', () => {
  56. let p = new Element( 'p', [], 'a' );
  57. expect( () => {
  58. doc.batch().wrap( range, p );
  59. } ).to.throw( CKEditorError, /^batch-wrap-element-not-empty/ );
  60. } );
  61. it( 'should throw if element to wrap with has children', () => {
  62. let p = new Element( 'p' );
  63. root.insertChildren( 0, p );
  64. expect( () => {
  65. doc.batch().wrap( range, p );
  66. } ).to.throw( CKEditorError, /^batch-wrap-element-attached/ );
  67. } );
  68. it( 'should be chainable', () => {
  69. const batch = doc.batch();
  70. const chain = batch.wrap( range, 'p' );
  71. expect( chain ).to.equal( batch );
  72. } );
  73. it( 'should add delta to batch and operation to delta before applying operation', () => {
  74. sinon.spy( doc, 'applyOperation' );
  75. const batch = doc.batch().wrap( range, 'p' );
  76. const correctDeltaMatcher = sinon.match( ( operation ) => {
  77. return operation.delta && operation.delta.batch && operation.delta.batch == batch;
  78. } );
  79. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  80. } );
  81. } );
  82. } );
  83. describe( 'WrapDelta', () => {
  84. let wrapDelta, doc, root;
  85. beforeEach( () => {
  86. doc = new Document();
  87. root = doc.createRoot();
  88. wrapDelta = new WrapDelta();
  89. } );
  90. describe( 'constructor', () => {
  91. it( 'should create wrap delta with no operations added', () => {
  92. expect( wrapDelta.operations.length ).to.equal( 0 );
  93. } );
  94. } );
  95. describe( 'range', () => {
  96. it( 'should be equal to null if there are no operations in delta', () => {
  97. expect( wrapDelta.range ).to.be.null;
  98. } );
  99. it( 'should be equal to wrapped range', () => {
  100. wrapDelta.operations.push( new InsertOperation( new Position( root, [ 1, 6 ] ), 1 ) );
  101. wrapDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), 5, new Position( root, [ 1, 6, 0 ] ) ) );
  102. expect( wrapDelta.range.start.isEqual( new Position( root, [ 1, 1 ] ) ) ).to.be.true;
  103. expect( wrapDelta.range.end.isEqual( new Position( root, [ 1, 6 ] ) ) ).to.be.true;
  104. } );
  105. } );
  106. describe( 'howMany', () => {
  107. it( 'should be equal to 0 if there are no operations in delta', () => {
  108. expect( wrapDelta.howMany ).to.equal( 0 );
  109. } );
  110. it( 'should be equal to the number of wrapped elements', () => {
  111. let howMany = 5;
  112. wrapDelta.operations.push( new InsertOperation( new Position( root, [ 1, 6 ] ), 1 ) );
  113. wrapDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), howMany, new Position( root, [ 1, 6, 0 ] ) ) );
  114. expect( wrapDelta.howMany ).to.equal( 5 );
  115. } );
  116. } );
  117. describe( 'getReversed', () => {
  118. it( 'should return empty UnwrapDelta if there are no operations in delta', () => {
  119. let reversed = wrapDelta.getReversed();
  120. expect( reversed ).to.be.instanceof( UnwrapDelta );
  121. expect( reversed.operations.length ).to.equal( 0 );
  122. } );
  123. it( 'should return correct UnwrapDelta', () => {
  124. wrapDelta.operations.push( new InsertOperation( new Position( root, [ 1, 6 ] ), 1 ) );
  125. wrapDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), 5, new Position( root, [ 1, 6, 0 ] ) ) );
  126. let reversed = wrapDelta.getReversed();
  127. expect( reversed ).to.be.instanceof( UnwrapDelta );
  128. expect( reversed.operations.length ).to.equal( 2 );
  129. expect( reversed.operations[ 0 ] ).to.be.instanceof( MoveOperation );
  130. expect( reversed.operations[ 0 ].sourcePosition.path ).to.deep.equal( [ 1, 1, 0 ] );
  131. expect( reversed.operations[ 0 ].howMany ).to.equal( 5 );
  132. expect( reversed.operations[ 0 ].targetPosition.path ).to.deep.equal( [ 1, 1 ] );
  133. expect( reversed.operations[ 1 ] ).to.be.instanceof( RemoveOperation );
  134. expect( reversed.operations[ 1 ].sourcePosition.path ).to.deep.equal( [ 1, 6 ] );
  135. expect( reversed.operations[ 1 ].howMany ).to.equal( 1 );
  136. } );
  137. } );
  138. describe( '_insertOperation', () => {
  139. it( 'should be null if there are no operations in the delta', () => {
  140. expect( wrapDelta._insertOperation ).to.be.null;
  141. } );
  142. it( 'should be equal to the first operation in the delta', () => {
  143. let insertOperation = new InsertOperation( new Position( root, [ 1, 6 ] ), 1 );
  144. wrapDelta.operations.push( insertOperation );
  145. wrapDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), 5, new Position( root, [ 1, 6, 0 ] ) ) );
  146. expect( wrapDelta._insertOperation ).to.equal( insertOperation );
  147. } );
  148. } );
  149. it( 'should provide proper className', () => {
  150. expect( WrapDelta.className ).to.equal( 'engine.model.delta.WrapDelta' );
  151. } );
  152. } );