8
0

delta.js 7.8 KB

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