wrapdelta.js 6.8 KB

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