deltafactory.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 Delta from '/ckeditor5/engine/model/delta/delta.js';
  7. import InsertDelta from '/ckeditor5/engine/model/delta/insertdelta.js';
  8. import AttributeOperation from '/ckeditor5/engine/model/operation/attributeoperation.js';
  9. import InsertOperation from '/ckeditor5/engine/model/operation/insertoperation.js';
  10. import MoveOperation from '/ckeditor5/engine/model/operation/moveoperation.js';
  11. import NoOperation from '/ckeditor5/engine/model/operation/nooperation.js';
  12. import ReinsertOperation from '/ckeditor5/engine/model/operation/reinsertoperation.js';
  13. import RemoveOperation from '/ckeditor5/engine/model/operation/removeoperation.js';
  14. import RootAttributeOperation from '/ckeditor5/engine/model/operation/rootattributeoperation.js';
  15. import CKEditorError from '/ckeditor5/utils/ckeditorerror.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. class FooDelta extends Delta {
  22. static get className() {
  23. return 'tets.delta.foo';
  24. }
  25. }
  26. class BarDelta extends Delta {
  27. static get className() {
  28. return 'tets.delta.bar';
  29. }
  30. }
  31. describe( 'DeltaFactory', () => {
  32. describe( 'fromJSON', () => {
  33. let delta, root, doc;
  34. before( () => {
  35. DeltaFactory.register( FooDelta );
  36. } );
  37. beforeEach( () => {
  38. delta = new FooDelta();
  39. doc = new Document();
  40. root = doc.createRoot();
  41. } );
  42. it( 'should throw error for unregistered delta', () => {
  43. expect( () => {
  44. DeltaFactory.fromJSON( jsonParseStringify( new BarDelta() ), {} );
  45. } ).to.throw( CKEditorError, /^delta-fromjson-no-deserializer/ );
  46. } );
  47. it( 'should create delta with AttributeOperation', () => {
  48. delta.addOperation( new AttributeOperation(
  49. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  50. 'foo',
  51. true,
  52. null,
  53. doc.version
  54. ) );
  55. let deserialized = DeltaFactory.fromJSON( jsonParseStringify( delta ), doc );
  56. expect( deserialized ).to.deep.equal( delta );
  57. } );
  58. it( 'should create delta with InsertOperation', () => {
  59. delta.addOperation( new InsertOperation(
  60. new Position( root, [ 0 ] ),
  61. 'x',
  62. doc.version
  63. ) );
  64. let deserialized = DeltaFactory.fromJSON( jsonParseStringify( delta ), doc );
  65. expect( deserialized ).to.deep.equal( delta );
  66. } );
  67. it( 'should create delta with MoveOperation', () => {
  68. delta.addOperation( new MoveOperation(
  69. new Position( root, [ 0, 0 ] ),
  70. 1,
  71. new Position( root, [ 1, 0 ] ),
  72. doc.version
  73. ) );
  74. let deserialized = DeltaFactory.fromJSON( jsonParseStringify( delta ), doc );
  75. expect( deserialized ).to.deep.equal( delta );
  76. } );
  77. it( 'should create delta with NoOperation', () => {
  78. delta.addOperation( new NoOperation( 0 ) );
  79. let deserialized = DeltaFactory.fromJSON( jsonParseStringify( delta ), doc );
  80. expect( deserialized ).to.deep.equal( delta );
  81. } );
  82. it( 'should create delta with ReinsertOperation', () => {
  83. delta.addOperation( new ReinsertOperation(
  84. new Position( doc.graveyard, [ 0 ] ),
  85. 2,
  86. new Position( root, [ 0 ] ),
  87. doc.version
  88. ) );
  89. let deserialized = DeltaFactory.fromJSON( jsonParseStringify( delta ), doc );
  90. expect( deserialized ).to.deep.equal( delta );
  91. } );
  92. it( 'should create delta with RemoveOperation', () => {
  93. delta.addOperation( new RemoveOperation(
  94. new Position( root, [ 2 ] ),
  95. 2,
  96. doc.version
  97. ) );
  98. let 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. let 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. let insertDelta = new InsertDelta();
  108. let serialized = jsonParseStringify( insertDelta );
  109. let 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. } );