deltafactory.js 4.7 KB

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