8
0

delta.js 7.6 KB

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