delta.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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( 'addOperation', () => {
  53. it( 'should add operation to the delta', () => {
  54. const delta = new Delta();
  55. const operation = {};
  56. delta.addOperation( operation );
  57. expect( delta.operations ).to.have.length( 1 );
  58. expect( delta.operations[ 0 ] ).to.equal( operation );
  59. } );
  60. it( 'should add delta property to the operation', () => {
  61. const delta = new Delta();
  62. const operation = {};
  63. delta.addOperation( operation );
  64. expect( operation.delta ).to.equal( delta );
  65. } );
  66. } );
  67. describe( 'iterator', () => {
  68. it( 'should iterate over delta operations', () => {
  69. const delta = new Delta();
  70. delta.addOperation( {} );
  71. delta.addOperation( {} );
  72. delta.addOperation( {} );
  73. const totalNumber = count( delta.operations );
  74. expect( totalNumber ).to.equal( 3 );
  75. } );
  76. } );
  77. describe( 'getReversed', () => {
  78. it( 'should return empty Delta if there are no operations in delta', () => {
  79. const delta = new Delta();
  80. let reversed = delta.getReversed();
  81. expect( reversed ).to.be.instanceof( Delta );
  82. expect( reversed.operations.length ).to.equal( 0 );
  83. } );
  84. it( 'should return Delta with all operations reversed and their order reversed', () => {
  85. const delta = new Delta();
  86. delta.addOperation( new FooOperation( 'a', 1 ) );
  87. delta.addOperation( new BarOperation( 'b', 2 ) );
  88. let reversed = delta.getReversed();
  89. expect( reversed ).to.be.instanceof( Delta );
  90. expect( reversed.operations.length ).to.equal( 2 );
  91. expect( reversed.operations[ 0 ] ).to.be.instanceOf( FooOperation );
  92. expect( reversed.operations[ 0 ].string ).to.equal( 'b' );
  93. expect( reversed.operations[ 1 ] ).to.be.instanceOf( BarOperation );
  94. expect( reversed.operations[ 1 ].string ).to.equal( 'a' );
  95. } );
  96. } );
  97. describe( 'toJSON', () => {
  98. let delta, root, doc;
  99. before( () => {
  100. DeltaFactory.register( FooDelta );
  101. } );
  102. beforeEach( () => {
  103. delta = new FooDelta();
  104. doc = new Document();
  105. root = doc.createRoot( 'root' );
  106. } );
  107. it( 'should return JSON representation for empty delta', () => {
  108. expect( jsonParseStringify( delta ) ).to.deep.equal( {
  109. __className: FooDelta.className,
  110. operations: []
  111. } );
  112. } );
  113. it( 'should create delta with AttributeOperation', () => {
  114. let operation = new AttributeOperation(
  115. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  116. 'foo',
  117. true,
  118. null,
  119. doc.version
  120. );
  121. delta.addOperation( operation );
  122. expect( jsonParseStringify( delta ) ).to.deep.equal( {
  123. __className: FooDelta.className,
  124. operations: [ jsonParseStringify( operation ) ]
  125. } );
  126. } );
  127. it( 'should create delta with InsertOperation', () => {
  128. let operation = new InsertOperation(
  129. new Position( root, [ 0 ] ),
  130. 'x',
  131. doc.version
  132. );
  133. delta.addOperation( operation );
  134. expect( jsonParseStringify( delta ) ).to.deep.equal( {
  135. __className: FooDelta.className,
  136. operations: [ jsonParseStringify( operation ) ]
  137. } );
  138. } );
  139. it( 'should create delta with MoveOperation', () => {
  140. let operation = new MoveOperation(
  141. new Position( root, [ 0, 0 ] ),
  142. 1,
  143. new Position( root, [ 1, 0 ] ),
  144. doc.version
  145. );
  146. delta.addOperation( operation );
  147. expect( jsonParseStringify( delta ) ).to.deep.equal( {
  148. __className: FooDelta.className,
  149. operations: [ jsonParseStringify( operation ) ]
  150. } );
  151. } );
  152. it( 'should create delta with NoOperation', () => {
  153. let operation = new NoOperation( 0 );
  154. delta.addOperation( operation );
  155. expect( jsonParseStringify( delta ) ).to.deep.equal( {
  156. __className: FooDelta.className,
  157. operations: [ jsonParseStringify( operation ) ]
  158. } );
  159. } );
  160. it( 'should create delta with ReinsertOperation', () => {
  161. let operation = new ReinsertOperation(
  162. new Position( doc.graveyard, [ 0 ] ),
  163. 2,
  164. new Position( root, [ 0 ] ),
  165. doc.version
  166. );
  167. delta.addOperation( operation );
  168. expect( jsonParseStringify( delta ) ).to.deep.equal( {
  169. __className: FooDelta.className,
  170. operations: [ jsonParseStringify( operation ) ]
  171. } );
  172. } );
  173. it( 'should create delta with RemoveOperation', () => {
  174. let operation = new RemoveOperation(
  175. new Position( root, [ 2 ] ),
  176. 2,
  177. doc.version
  178. );
  179. delta.addOperation( operation );
  180. expect( jsonParseStringify( delta ) ).to.deep.equal( {
  181. __className: FooDelta.className,
  182. operations: [ jsonParseStringify( operation ) ]
  183. } );
  184. } );
  185. it( 'should create delta with RootAttributeOperation', () => {
  186. let operation = new RootAttributeOperation( root, 'key', null, 'newValue', doc.version );
  187. delta.addOperation( operation );
  188. expect( jsonParseStringify( delta ) ).to.deep.equal( {
  189. __className: FooDelta.className,
  190. operations: [ jsonParseStringify( operation ) ]
  191. } );
  192. } );
  193. it( 'should remove batch reference', () => {
  194. delta.batch = { foo: 'bar' };
  195. expect( jsonParseStringify( delta ) ).to.deep.equal( {
  196. __className: FooDelta.className,
  197. operations: []
  198. } );
  199. } );
  200. } );
  201. } );