delta.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel, delta */
  6. 'use strict';
  7. import count from '/ckeditor5/utils/count.js';
  8. import Delta from '/ckeditor5/engine/treemodel/delta/delta.js';
  9. import Operation from '/ckeditor5/engine/treemodel/operation/operation.js';
  10. import AttributeOperation from '/ckeditor5/engine/treemodel/operation/attributeoperation.js';
  11. import InsertOperation from '/ckeditor5/engine/treemodel/operation/insertoperation.js';
  12. import MoveOperation from '/ckeditor5/engine/treemodel/operation/moveoperation.js';
  13. import NoOperation from '/ckeditor5/engine/treemodel/operation/nooperation.js';
  14. import ReinsertOperation from '/ckeditor5/engine/treemodel/operation/reinsertoperation.js';
  15. import RemoveOperation from '/ckeditor5/engine/treemodel/operation/removeoperation.js';
  16. import RootAttributeOperation from '/ckeditor5/engine/treemodel/operation/rootattributeoperation.js';
  17. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  18. import { registerDeserializer } from '/ckeditor5/engine/treemodel/delta/delta.js';
  19. import Document from '/ckeditor5/engine/treemodel/document.js';
  20. import Position from '/ckeditor5/engine/treemodel/position.js';
  21. import Range from '/ckeditor5/engine/treemodel/range.js';
  22. import treeModelTestUtils from '/tests/engine/treemodel/_utils/utils.js';
  23. // Some test examples of operations.
  24. class FooOperation extends Operation {
  25. constructor( string, baseVersion ) {
  26. super( baseVersion );
  27. this.string = string;
  28. }
  29. getReversed() {
  30. /* jshint ignore:start */
  31. return new BarOperation( this.string, this.baseVersion );
  32. /* jshint ignore:end */
  33. }
  34. }
  35. class BarOperation extends FooOperation {
  36. getReversed() {
  37. return new FooOperation( this.string, this.baseVersion );
  38. }
  39. }
  40. class FooDelta extends Delta {
  41. static get className() {
  42. return 'tets.delta.foo';
  43. }
  44. }
  45. class BarDelta extends Delta {
  46. static get className() {
  47. return 'tets.delta.bar';
  48. }
  49. }
  50. class FooBarDelta extends Delta {
  51. static get className() {
  52. return 'tets.delta.foobar';
  53. }
  54. }
  55. describe( 'Delta', () => {
  56. describe( 'constructor', () => {
  57. it( 'should create an delta with empty properties', () => {
  58. const delta = new Delta();
  59. expect( delta ).to.have.property( 'batch' ).that.is.null;
  60. expect( delta ).to.have.property( 'operations' ).that.a( 'array' ).and.have.length( 0 );
  61. } );
  62. } );
  63. describe( 'addOperation', () => {
  64. it( 'should add operation to the delta', () => {
  65. const delta = new Delta();
  66. const operation = {};
  67. delta.addOperation( operation );
  68. expect( delta.operations ).to.have.length( 1 );
  69. expect( delta.operations[ 0 ] ).to.equal( operation );
  70. } );
  71. it( 'should add delta property to the operation', () => {
  72. const delta = new Delta();
  73. const operation = {};
  74. delta.addOperation( operation );
  75. expect( operation.delta ).to.equal( delta );
  76. } );
  77. } );
  78. describe( 'iterator', () => {
  79. it( 'should iterate over delta operations', () => {
  80. const delta = new Delta();
  81. delta.addOperation( {} );
  82. delta.addOperation( {} );
  83. delta.addOperation( {} );
  84. const totalNumber = count( delta.operations );
  85. expect( totalNumber ).to.equal( 3 );
  86. } );
  87. } );
  88. describe( 'getReversed', () => {
  89. it( 'should return empty Delta if there are no operations in delta', () => {
  90. const delta = new Delta();
  91. let reversed = delta.getReversed();
  92. expect( reversed ).to.be.instanceof( Delta );
  93. expect( reversed.operations.length ).to.equal( 0 );
  94. } );
  95. it( 'should return Delta with all operations reversed and their order reversed', () => {
  96. const delta = new Delta();
  97. delta.addOperation( new FooOperation( 'a', 1 ) );
  98. delta.addOperation( new BarOperation( 'b', 2 ) );
  99. let reversed = delta.getReversed();
  100. expect( reversed ).to.be.instanceof( Delta );
  101. expect( reversed.operations.length ).to.equal( 2 );
  102. expect( reversed.operations[ 0 ] ).to.be.instanceOf( FooOperation );
  103. expect( reversed.operations[ 0 ].string ).to.equal( 'b' );
  104. expect( reversed.operations[ 1 ] ).to.be.instanceOf( BarOperation );
  105. expect( reversed.operations[ 1 ].string ).to.equal( 'a' );
  106. } );
  107. } );
  108. describe( 'toJSON', () => {
  109. let delta, root, doc;
  110. before( () => {
  111. registerDeserializer( FooDelta.className, FooDelta );
  112. } );
  113. beforeEach( () => {
  114. delta = new FooDelta();
  115. doc = new Document();
  116. root = doc.createRoot( 'root' );
  117. } );
  118. it( 'should return JSON representation for empty delta', () => {
  119. expect( treeModelTestUtils.jsonParseStringify( delta ) ).to.deep.equal( {
  120. __className: FooDelta.className,
  121. operations: []
  122. } );
  123. } );
  124. it( 'should create delta with AttributeOperation', () => {
  125. let operation = new AttributeOperation(
  126. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  127. 'foo',
  128. true,
  129. null,
  130. doc.version
  131. );
  132. delta.addOperation( operation );
  133. expect( treeModelTestUtils.jsonParseStringify( delta ) ).to.deep.equal( {
  134. __className: FooDelta.className,
  135. operations: [ treeModelTestUtils.jsonParseStringify( operation ) ]
  136. } );
  137. } );
  138. it( 'should create delta with InsertOperation', () => {
  139. let operation = new InsertOperation(
  140. new Position( root, [ 0 ] ),
  141. 'x',
  142. doc.version
  143. );
  144. delta.addOperation( operation );
  145. expect( treeModelTestUtils.jsonParseStringify( delta ) ).to.deep.equal( {
  146. __className: FooDelta.className,
  147. operations: [ treeModelTestUtils.jsonParseStringify( operation ) ]
  148. } );
  149. } );
  150. it( 'should create delta with MoveOperation', () => {
  151. let operation = new MoveOperation(
  152. new Position( root, [ 0, 0 ] ),
  153. 1,
  154. new Position( root, [ 1, 0 ] ),
  155. doc.version
  156. );
  157. delta.addOperation( operation );
  158. expect( treeModelTestUtils.jsonParseStringify( delta ) ).to.deep.equal( {
  159. __className: FooDelta.className,
  160. operations: [ treeModelTestUtils.jsonParseStringify( operation ) ]
  161. } );
  162. } );
  163. it( 'should create delta with NoOperation', () => {
  164. let operation = new NoOperation( 0 );
  165. delta.addOperation( operation );
  166. expect( treeModelTestUtils.jsonParseStringify( delta ) ).to.deep.equal( {
  167. __className: FooDelta.className,
  168. operations: [ treeModelTestUtils.jsonParseStringify( operation ) ]
  169. } );
  170. } );
  171. it( 'should create delta with ReinsertOperation', () => {
  172. let operation = new ReinsertOperation(
  173. new Position( doc.graveyard, [ 0 ] ),
  174. 2,
  175. new Position( root, [ 0 ] ),
  176. doc.version
  177. );
  178. delta.addOperation( operation );
  179. expect( treeModelTestUtils.jsonParseStringify( delta ) ).to.deep.equal( {
  180. __className: FooDelta.className,
  181. operations: [ treeModelTestUtils.jsonParseStringify( operation ) ]
  182. } );
  183. } );
  184. it( 'should create delta with RemoveOperation', () => {
  185. let operation = new RemoveOperation(
  186. new Position( root, [ 2 ] ),
  187. 2,
  188. doc.version
  189. );
  190. delta.addOperation( operation );
  191. expect( treeModelTestUtils.jsonParseStringify( delta ) ).to.deep.equal( {
  192. __className: FooDelta.className,
  193. operations: [ treeModelTestUtils.jsonParseStringify( operation ) ]
  194. } );
  195. } );
  196. it( 'should create delta with RootAttributeOperation', () => {
  197. let operation = new RootAttributeOperation( root, 'key', null, 'newValue', doc.version );
  198. delta.addOperation( operation );
  199. expect( treeModelTestUtils.jsonParseStringify( delta ) ).to.deep.equal( {
  200. __className: FooDelta.className,
  201. operations: [ treeModelTestUtils.jsonParseStringify( operation ) ]
  202. } );
  203. } );
  204. it( 'should remove batch reference', () => {
  205. delta.batch = { foo: 'bar' };
  206. expect( treeModelTestUtils.jsonParseStringify( delta ) ).to.deep.equal( {
  207. __className: FooDelta.className,
  208. operations: []
  209. } );
  210. } );
  211. } );
  212. describe( 'fromJSON', () => {
  213. let delta, root, doc;
  214. before( () => {
  215. registerDeserializer( FooBarDelta.className, FooDelta );
  216. } );
  217. beforeEach( () => {
  218. delta = new FooBarDelta();
  219. doc = new Document();
  220. root = doc.createRoot( 'root' );
  221. } );
  222. it( 'should throw error for unregistered delta', () => {
  223. expect( () => {
  224. Delta.fromJSON( treeModelTestUtils.jsonParseStringify( new BarDelta() ), {} );
  225. } ).to.throw( CKEditorError, /^delta-fromjson-no-deserializer/ );
  226. } );
  227. it( 'should create delta with AttributeOperation', () => {
  228. delta.addOperation( new AttributeOperation(
  229. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  230. 'foo',
  231. true,
  232. null,
  233. doc.version
  234. ) );
  235. let deserialized = Delta.fromJSON( treeModelTestUtils.jsonParseStringify( delta ), doc );
  236. expect( deserialized ).to.deep.equal( delta );
  237. } );
  238. it( 'should create delta with InsertOperation', () => {
  239. delta.addOperation( new InsertOperation(
  240. new Position( root, [ 0 ] ),
  241. 'x',
  242. doc.version
  243. ) );
  244. let deserialized = Delta.fromJSON( treeModelTestUtils.jsonParseStringify( delta ), doc );
  245. expect( deserialized ).to.deep.equal( delta );
  246. } );
  247. it( 'should create delta with MoveOperation', () => {
  248. delta.addOperation( new MoveOperation(
  249. new Position( root, [ 0, 0 ] ),
  250. 1,
  251. new Position( root, [ 1, 0 ] ),
  252. doc.version
  253. ) );
  254. let deserialized = Delta.fromJSON( treeModelTestUtils.jsonParseStringify( delta ), doc );
  255. expect( deserialized ).to.deep.equal( delta );
  256. } );
  257. it( 'should create delta with NoOperation', () => {
  258. delta.addOperation( new NoOperation( 0 ) );
  259. let deserialized = Delta.fromJSON( treeModelTestUtils.jsonParseStringify( delta ), doc );
  260. expect( deserialized ).to.deep.equal( delta );
  261. } );
  262. it( 'should create delta with ReinsertOperation', () => {
  263. delta.addOperation( new ReinsertOperation(
  264. new Position( doc.graveyard, [ 0 ] ),
  265. 2,
  266. new Position( root, [ 0 ] ),
  267. doc.version
  268. ) );
  269. let deserialized = Delta.fromJSON( treeModelTestUtils.jsonParseStringify( delta ), doc );
  270. expect( deserialized ).to.deep.equal( delta );
  271. } );
  272. it( 'should create delta with RemoveOperation', () => {
  273. delta.addOperation( new RemoveOperation(
  274. new Position( root, [ 2 ] ),
  275. 2,
  276. doc.version
  277. ) );
  278. let deserialized = Delta.fromJSON( treeModelTestUtils.jsonParseStringify( delta ), doc );
  279. expect( deserialized ).to.deep.equal( delta );
  280. } );
  281. it( 'should create delta with RootAttributeOperation', () => {
  282. delta.addOperation( new RootAttributeOperation( root, 'key', null, 'newValue', doc.version ) );
  283. let deserialized = Delta.fromJSON( treeModelTestUtils.jsonParseStringify( delta ), doc );
  284. expect( deserialized ).to.deep.equal( delta );
  285. } );
  286. } );
  287. describe( 'registerDeserializer', () => {
  288. it( 'should add delta deserializer', ( done ) => {
  289. registerDeserializer( 'foo', deserializer );
  290. Delta.fromJSON( { __className: 'foo', operations: [] } );
  291. function deserializer() {
  292. done();
  293. }
  294. } );
  295. it( 'should throw error for adding already defined deserializer', () => {
  296. registerDeserializer( 'BAR', FooDelta );
  297. expect( () => {
  298. registerDeserializer( 'BAR', FooDelta );
  299. } ).to.throw( CKEditorError, /^delta-register-deserializer-defined/ );
  300. } );
  301. } );
  302. } );