8
0

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