wrapdelta.js 7.1 KB

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