wrapdelta.js 6.7 KB

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