wrapdelta.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 Text from '/ckeditor5/engine/model/text.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, 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. let 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. let notFlatRange = new Range( new Position( root, [ 3 ] ), new Position( root, [ 6, 2 ] ) );
  45. expect( () => {
  46. doc.batch().wrap( notFlatRange, 'p' );
  47. } ).to.throwCKEditorError( /^batch-wrap-range-not-flat/ );
  48. } );
  49. it( 'should throw if element to wrap with has children', () => {
  50. let p = new Element( 'p', [], new Text( 'a' ) );
  51. expect( () => {
  52. doc.batch().wrap( range, p );
  53. } ).to.throwCKEditorError( /^batch-wrap-element-not-empty/ );
  54. } );
  55. it( 'should throw if element to wrap with has children', () => {
  56. let p = new Element( 'p' );
  57. root.insertChildren( 0, p );
  58. expect( () => {
  59. doc.batch().wrap( range, p );
  60. } ).to.throwCKEditorError( /^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( 'range', () => {
  90. it( 'should be equal to null if there are no operations in delta', () => {
  91. expect( wrapDelta.range ).to.be.null;
  92. } );
  93. it( 'should be equal to wrapped range', () => {
  94. wrapDelta.operations.push( new InsertOperation( new Position( root, [ 1, 6 ] ), [], 1 ) );
  95. wrapDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), 5, new Position( root, [ 1, 6, 0 ] ) ) );
  96. expect( wrapDelta.range.start.isEqual( new Position( root, [ 1, 1 ] ) ) ).to.be.true;
  97. expect( wrapDelta.range.end.isEqual( new Position( root, [ 1, 6 ] ) ) ).to.be.true;
  98. } );
  99. } );
  100. describe( 'howMany', () => {
  101. it( 'should be equal to 0 if there are no operations in delta', () => {
  102. expect( wrapDelta.howMany ).to.equal( 0 );
  103. } );
  104. it( 'should be equal to the number of wrapped elements', () => {
  105. let howMany = 5;
  106. wrapDelta.operations.push( new InsertOperation( new Position( root, [ 1, 6 ] ), [], 1 ) );
  107. wrapDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), howMany, new Position( root, [ 1, 6, 0 ] ) ) );
  108. expect( wrapDelta.howMany ).to.equal( 5 );
  109. } );
  110. } );
  111. describe( 'getReversed', () => {
  112. it( 'should return empty UnwrapDelta if there are no operations in delta', () => {
  113. let reversed = wrapDelta.getReversed();
  114. expect( reversed ).to.be.instanceof( UnwrapDelta );
  115. expect( reversed.operations.length ).to.equal( 0 );
  116. } );
  117. it( 'should return correct UnwrapDelta', () => {
  118. wrapDelta.operations.push( new InsertOperation( new Position( root, [ 1, 6 ] ), new Element( 'p' ), 1 ) );
  119. wrapDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), 5, new Position( root, [ 1, 6, 0 ] ) ) );
  120. let reversed = wrapDelta.getReversed();
  121. expect( reversed ).to.be.instanceof( UnwrapDelta );
  122. expect( reversed.operations.length ).to.equal( 2 );
  123. expect( reversed.operations[ 0 ] ).to.be.instanceof( MoveOperation );
  124. expect( reversed.operations[ 0 ].sourcePosition.path ).to.deep.equal( [ 1, 1, 0 ] );
  125. expect( reversed.operations[ 0 ].howMany ).to.equal( 5 );
  126. expect( reversed.operations[ 0 ].targetPosition.path ).to.deep.equal( [ 1, 1 ] );
  127. expect( reversed.operations[ 1 ] ).to.be.instanceof( RemoveOperation );
  128. expect( reversed.operations[ 1 ].sourcePosition.path ).to.deep.equal( [ 1, 6 ] );
  129. expect( reversed.operations[ 1 ].howMany ).to.equal( 1 );
  130. } );
  131. } );
  132. describe( '_insertOperation', () => {
  133. it( 'should be null if there are no operations in the delta', () => {
  134. expect( wrapDelta._insertOperation ).to.be.null;
  135. } );
  136. it( 'should be equal to the first operation in the delta', () => {
  137. let insertOperation = new InsertOperation( new Position( root, [ 1, 6 ] ), [], 1 );
  138. wrapDelta.operations.push( insertOperation );
  139. wrapDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), 5, new Position( root, [ 1, 6, 0 ] ) ) );
  140. expect( wrapDelta._insertOperation ).to.equal( insertOperation );
  141. } );
  142. } );
  143. it( 'should provide proper className', () => {
  144. expect( WrapDelta.className ).to.equal( 'engine.model.delta.WrapDelta' );
  145. } );
  146. } );