delta.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import count from '@ckeditor/ckeditor5-utils/src/count';
  6. import Delta from '../../../src/model/delta/delta';
  7. import Operation from '../../../src/model/operation/operation';
  8. import AttributeOperation from '../../../src/model/operation/attributeoperation';
  9. import InsertOperation from '../../../src/model/operation/insertoperation';
  10. import MoveOperation from '../../../src/model/operation/moveoperation';
  11. import NoOperation from '../../../src/model/operation/nooperation';
  12. import ReinsertOperation from '../../../src/model/operation/reinsertoperation';
  13. import RemoveOperation from '../../../src/model/operation/removeoperation';
  14. import RootAttributeOperation from '../../../src/model/operation/rootattributeoperation';
  15. import DeltaFactory from '../../../src/model/delta/deltafactory';
  16. import Document from '../../../src/model/document';
  17. import Position from '../../../src/model/position';
  18. import Range from '../../../src/model/range';
  19. import { jsonParseStringify } from '../../../tests/model/_utils/utils';
  20. // Some test examples of operations.
  21. class FooOperation extends Operation {
  22. constructor( string, baseVersion ) {
  23. super( baseVersion );
  24. this.string = string;
  25. }
  26. getReversed() {
  27. return new BarOperation( this.string, this.baseVersion );
  28. }
  29. }
  30. class BarOperation extends FooOperation {
  31. getReversed() {
  32. return new FooOperation( this.string, this.baseVersion );
  33. }
  34. }
  35. class FooDelta extends Delta {
  36. static get className() {
  37. return 'tets.delta.foo';
  38. }
  39. }
  40. describe( 'Delta', () => {
  41. describe( 'constructor()', () => {
  42. it( 'should create an delta with empty properties', () => {
  43. const delta = new Delta();
  44. expect( delta ).to.have.property( 'batch' ).that.is.null;
  45. expect( delta ).to.have.property( 'operations' ).that.a( 'array' ).and.have.length( 0 );
  46. } );
  47. } );
  48. describe( 'baseVersion', () => {
  49. it( 'should return baseVersion of first operation in the delta', () => {
  50. const delta = new Delta();
  51. delta.addOperation( { baseVersion: 0 } );
  52. delta.addOperation( { baseVersion: 1 } );
  53. delta.addOperation( { baseVersion: 2 } );
  54. expect( delta.baseVersion ).to.equal( 0 );
  55. } );
  56. it( 'should change baseVersion of it\'s operations', () => {
  57. const delta = new Delta();
  58. delta.addOperation( { baseVersion: 0 } );
  59. delta.addOperation( { baseVersion: 1 } );
  60. delta.addOperation( { baseVersion: 2 } );
  61. delta.baseVersion = 10;
  62. expect( delta.operations[ 0 ].baseVersion ).to.equal( 10 );
  63. expect( delta.operations[ 1 ].baseVersion ).to.equal( 11 );
  64. expect( delta.operations[ 2 ].baseVersion ).to.equal( 12 );
  65. } );
  66. } );
  67. describe( 'addOperation', () => {
  68. it( 'should add operation to the delta', () => {
  69. const delta = new Delta();
  70. const operation = {};
  71. delta.addOperation( operation );
  72. expect( delta.operations ).to.have.length( 1 );
  73. expect( delta.operations[ 0 ] ).to.equal( operation );
  74. } );
  75. it( 'should add delta property to the operation', () => {
  76. const delta = new Delta();
  77. const operation = {};
  78. delta.addOperation( operation );
  79. expect( operation.delta ).to.equal( delta );
  80. } );
  81. } );
  82. describe( 'iterator', () => {
  83. it( 'should iterate over delta operations', () => {
  84. const delta = new Delta();
  85. delta.addOperation( {} );
  86. delta.addOperation( {} );
  87. delta.addOperation( {} );
  88. const totalNumber = count( delta.operations );
  89. expect( totalNumber ).to.equal( 3 );
  90. } );
  91. } );
  92. describe( 'getReversed', () => {
  93. it( 'should return empty Delta if there are no operations in delta', () => {
  94. const delta = new Delta();
  95. const reversed = delta.getReversed();
  96. expect( reversed ).to.be.instanceof( Delta );
  97. expect( reversed.operations.length ).to.equal( 0 );
  98. } );
  99. it( 'should return Delta with all operations reversed and their order reversed', () => {
  100. const delta = new Delta();
  101. delta.addOperation( new FooOperation( 'a', 1 ) );
  102. delta.addOperation( new BarOperation( 'b', 2 ) );
  103. const reversed = delta.getReversed();
  104. expect( reversed ).to.be.instanceof( Delta );
  105. expect( reversed.operations.length ).to.equal( 2 );
  106. expect( reversed.operations[ 0 ] ).to.be.instanceOf( FooOperation );
  107. expect( reversed.operations[ 0 ].string ).to.equal( 'b' );
  108. expect( reversed.operations[ 1 ] ).to.be.instanceOf( BarOperation );
  109. expect( reversed.operations[ 1 ].string ).to.equal( 'a' );
  110. } );
  111. } );
  112. describe( 'toJSON', () => {
  113. let delta, root, doc;
  114. before( () => {
  115. DeltaFactory.register( FooDelta );
  116. } );
  117. beforeEach( () => {
  118. delta = new FooDelta();
  119. doc = new Document();
  120. root = doc.createRoot();
  121. } );
  122. it( 'should return JSON representation for empty delta', () => {
  123. expect( jsonParseStringify( delta ) ).to.deep.equal( {
  124. __className: FooDelta.className,
  125. operations: []
  126. } );
  127. } );
  128. it( 'should create delta with AttributeOperation', () => {
  129. const operation = new AttributeOperation(
  130. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  131. 'foo',
  132. true,
  133. null,
  134. doc.version
  135. );
  136. delta.addOperation( operation );
  137. expect( jsonParseStringify( delta ) ).to.deep.equal( {
  138. __className: FooDelta.className,
  139. operations: [ jsonParseStringify( operation ) ]
  140. } );
  141. } );
  142. it( 'should create delta with InsertOperation', () => {
  143. const operation = new InsertOperation(
  144. new Position( root, [ 0 ] ),
  145. 'x',
  146. doc.version
  147. );
  148. delta.addOperation( operation );
  149. expect( jsonParseStringify( delta ) ).to.deep.equal( {
  150. __className: FooDelta.className,
  151. operations: [ jsonParseStringify( operation ) ]
  152. } );
  153. } );
  154. it( 'should create delta with MoveOperation', () => {
  155. const operation = new MoveOperation(
  156. new Position( root, [ 0, 0 ] ),
  157. 1,
  158. new Position( root, [ 1, 0 ] ),
  159. doc.version
  160. );
  161. delta.addOperation( operation );
  162. expect( jsonParseStringify( delta ) ).to.deep.equal( {
  163. __className: FooDelta.className,
  164. operations: [ jsonParseStringify( operation ) ]
  165. } );
  166. } );
  167. it( 'should create delta with NoOperation', () => {
  168. const operation = new NoOperation( 0 );
  169. delta.addOperation( operation );
  170. expect( jsonParseStringify( delta ) ).to.deep.equal( {
  171. __className: FooDelta.className,
  172. operations: [ jsonParseStringify( operation ) ]
  173. } );
  174. } );
  175. it( 'should create delta with ReinsertOperation', () => {
  176. const operation = new ReinsertOperation(
  177. new Position( doc.graveyard, [ 0 ] ),
  178. 2,
  179. new Position( root, [ 0 ] ),
  180. doc.version
  181. );
  182. delta.addOperation( operation );
  183. expect( jsonParseStringify( delta ) ).to.deep.equal( {
  184. __className: FooDelta.className,
  185. operations: [ jsonParseStringify( operation ) ]
  186. } );
  187. } );
  188. it( 'should create delta with RemoveOperation', () => {
  189. const operation = new RemoveOperation(
  190. new Position( root, [ 2 ] ),
  191. 2,
  192. new Position( doc.graveyard, [ 0 ] ),
  193. doc.version
  194. );
  195. delta.addOperation( operation );
  196. expect( jsonParseStringify( delta ) ).to.deep.equal( {
  197. __className: FooDelta.className,
  198. operations: [ jsonParseStringify( operation ) ]
  199. } );
  200. } );
  201. it( 'should create delta with RootAttributeOperation', () => {
  202. const operation = new RootAttributeOperation( root, 'key', null, 'newValue', doc.version );
  203. delta.addOperation( operation );
  204. expect( jsonParseStringify( delta ) ).to.deep.equal( {
  205. __className: FooDelta.className,
  206. operations: [ jsonParseStringify( operation ) ]
  207. } );
  208. } );
  209. it( 'should remove batch reference', () => {
  210. delta.batch = { foo: 'bar' };
  211. expect( jsonParseStringify( delta ) ).to.deep.equal( {
  212. __className: FooDelta.className,
  213. operations: []
  214. } );
  215. } );
  216. } );
  217. } );