deltafactory.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 Delta from '/ckeditor5/engine/treemodel/delta/delta.js';
  8. import InsertDelta from '/ckeditor5/engine/treemodel/delta/insertdelta.js';
  9. import AttributeOperation from '/ckeditor5/engine/treemodel/operation/attributeoperation.js';
  10. import InsertOperation from '/ckeditor5/engine/treemodel/operation/insertoperation.js';
  11. import MoveOperation from '/ckeditor5/engine/treemodel/operation/moveoperation.js';
  12. import NoOperation from '/ckeditor5/engine/treemodel/operation/nooperation.js';
  13. import ReinsertOperation from '/ckeditor5/engine/treemodel/operation/reinsertoperation.js';
  14. import RemoveOperation from '/ckeditor5/engine/treemodel/operation/removeoperation.js';
  15. import RootAttributeOperation from '/ckeditor5/engine/treemodel/operation/rootattributeoperation.js';
  16. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  17. import { default as DeltaFactory, registerDeserializer } from '/ckeditor5/engine/treemodel/delta/deltafactory.js';
  18. import Document from '/ckeditor5/engine/treemodel/document.js';
  19. import Position from '/ckeditor5/engine/treemodel/position.js';
  20. import Range from '/ckeditor5/engine/treemodel/range.js';
  21. import { jsonParseStringify } from '/tests/engine/treemodel/_utils/utils.js';
  22. class FooDelta extends Delta {
  23. static get className() {
  24. return 'tets.delta.foo';
  25. }
  26. }
  27. class BarDelta extends Delta {
  28. static get className() {
  29. return 'tets.delta.bar';
  30. }
  31. }
  32. class FooBarDelta extends Delta {
  33. static get className() {
  34. return 'tets.delta.foobar';
  35. }
  36. }
  37. describe( 'DeltaFactory', () => {
  38. describe( 'fromJSON', () => {
  39. let delta, root, doc;
  40. before( () => {
  41. registerDeserializer( FooBarDelta.className, FooDelta );
  42. } );
  43. beforeEach( () => {
  44. delta = new FooBarDelta();
  45. doc = new Document();
  46. root = doc.createRoot( 'root' );
  47. } );
  48. it( 'should throw error for unregistered delta', () => {
  49. expect( () => {
  50. DeltaFactory.fromJSON( jsonParseStringify( new BarDelta() ), {} );
  51. } ).to.throw( CKEditorError, /^delta-fromjson-no-deserializer/ );
  52. } );
  53. it( 'should create delta with AttributeOperation', () => {
  54. delta.addOperation( new AttributeOperation(
  55. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  56. 'foo',
  57. true,
  58. null,
  59. doc.version
  60. ) );
  61. let deserialized = DeltaFactory.fromJSON( jsonParseStringify( delta ), doc );
  62. expect( deserialized ).to.deep.equal( delta );
  63. } );
  64. it( 'should create delta with InsertOperation', () => {
  65. delta.addOperation( new InsertOperation(
  66. new Position( root, [ 0 ] ),
  67. 'x',
  68. doc.version
  69. ) );
  70. let deserialized = DeltaFactory.fromJSON( jsonParseStringify( delta ), doc );
  71. expect( deserialized ).to.deep.equal( delta );
  72. } );
  73. it( 'should create delta with MoveOperation', () => {
  74. delta.addOperation( new MoveOperation(
  75. new Position( root, [ 0, 0 ] ),
  76. 1,
  77. new Position( root, [ 1, 0 ] ),
  78. doc.version
  79. ) );
  80. let deserialized = DeltaFactory.fromJSON( jsonParseStringify( delta ), doc );
  81. expect( deserialized ).to.deep.equal( delta );
  82. } );
  83. it( 'should create delta with NoOperation', () => {
  84. delta.addOperation( new NoOperation( 0 ) );
  85. let deserialized = DeltaFactory.fromJSON( jsonParseStringify( delta ), doc );
  86. expect( deserialized ).to.deep.equal( delta );
  87. } );
  88. it( 'should create delta with ReinsertOperation', () => {
  89. delta.addOperation( new ReinsertOperation(
  90. new Position( doc.graveyard, [ 0 ] ),
  91. 2,
  92. new Position( root, [ 0 ] ),
  93. doc.version
  94. ) );
  95. let deserialized = DeltaFactory.fromJSON( jsonParseStringify( delta ), doc );
  96. expect( deserialized ).to.deep.equal( delta );
  97. } );
  98. it( 'should create delta with RemoveOperation', () => {
  99. delta.addOperation( new RemoveOperation(
  100. new Position( root, [ 2 ] ),
  101. 2,
  102. doc.version
  103. ) );
  104. let deserialized = DeltaFactory.fromJSON( jsonParseStringify( delta ), doc );
  105. expect( deserialized ).to.deep.equal( delta );
  106. } );
  107. it( 'should create delta with RootAttributeOperation', () => {
  108. delta.addOperation( new RootAttributeOperation( root, 'key', null, 'newValue', doc.version ) );
  109. let deserialized = DeltaFactory.fromJSON( jsonParseStringify( delta ), doc );
  110. expect( deserialized ).to.deep.equal( delta );
  111. } );
  112. it( 'should create InsertDelta instance from serialized JSON object', () => {
  113. let insertDelta = new InsertDelta();
  114. let serialized = jsonParseStringify( insertDelta );
  115. let deserialized = DeltaFactory.fromJSON( serialized, doc );
  116. expect( deserialized ).to.be.instanceOf( InsertDelta );
  117. expect( deserialized.operations ).to.have.length( 0 );
  118. } );
  119. } );
  120. describe( 'registerDeserializer', () => {
  121. it( 'should add delta deserializer', ( done ) => {
  122. registerDeserializer( 'foo', deserializer );
  123. DeltaFactory.fromJSON( { __className: 'foo', operations: [] } );
  124. function deserializer() {
  125. done();
  126. }
  127. } );
  128. } );
  129. } );