delta.js 7.7 KB

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