wrapdelta.js 6.8 KB

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