Răsfoiți Sursa

Internal: Improved serialization of WrapOperation.

Oskar Wróbel 7 ani în urmă
părinte
comite
78de1d6b9f

+ 6 - 2
packages/ckeditor5-engine/src/model/operation/wrapoperation.js

@@ -31,7 +31,7 @@ export default class WrapOperation extends Operation {
 	 * @param {Number} howMany Offset size of wrapped range. Wrapped range will start at `position.offset` and end at
 	 * @param {Number} howMany Offset size of wrapped range. Wrapped range will start at `position.offset` and end at
 	 * `position.offset + howMany`.
 	 * `position.offset + howMany`.
 	 * @param {module:engine/model/element~Element|module:engine/model/position~Position} elementOrPosition Element to
 	 * @param {module:engine/model/element~Element|module:engine/model/position~Position} elementOrPosition Element to
-	 * wrap with or position in graveyard before the element which should be used as a wrapper.
+	 * wrap or position in graveyard before the element which should be used as a wrapper.
 	 * @param {Number|null} baseVersion Document {@link module:engine/model/document~Document#version} on which operation
 	 * @param {Number|null} baseVersion Document {@link module:engine/model/document~Document#version} on which operation
 	 * can be applied or `null` if the operation operates on detached (non-document) tree.
 	 * can be applied or `null` if the operation operates on detached (non-document) tree.
 	 */
 	 */
@@ -171,8 +171,12 @@ export default class WrapOperation extends Operation {
 
 
 		json.position = this.position.toJSON();
 		json.position = this.position.toJSON();
 
 
-		if ( json.graveyardPosition ) {
+		if ( this.element ) {
+			json.element = this.element.toJSON();
+			delete json.graveyardPosition;
+		} else {
 			json.graveyardPosition = this.graveyardPosition.toJSON();
 			json.graveyardPosition = this.graveyardPosition.toJSON();
+			delete json.element;
 		}
 		}
 
 
 		return json;
 		return json;

+ 88 - 0
packages/ckeditor5-engine/tests/model/operation/wrapoperation.js

@@ -0,0 +1,88 @@
+/**
+ * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import Model from '../../../src/model/model';
+import Element from '../../../src/model/element';
+import WrapOperation from '../../../src/model/operation/wrapoperation';
+import Position from '../../../src/model/position';
+
+describe( 'WrapOperation', () => {
+	let model, doc, root;
+
+	beforeEach( () => {
+		model = new Model();
+		doc = model.document;
+		root = doc.createRoot();
+	} );
+
+	describe( 'type', () => {
+		it( 'should be wrap', () => {
+			const op = new WrapOperation(
+				new Position( root, [ 0 ] ),
+				1,
+				new Position( doc.graveyard, [ 0 ] ),
+				doc.version
+			);
+
+			expect( op.type ).to.equal( 'wrap' );
+		} );
+	} );
+
+	describe( 'toJSON', () => {
+		it( 'should create proper serialized object #1', () => {
+			const op = new WrapOperation(
+				new Position( root, [ 0 ] ),
+				1,
+				new Position( doc.graveyard, [ 0 ] ),
+				doc.version
+			);
+
+			const serialized = op.toJSON();
+
+			expect( serialized ).to.deep.equal( {
+				__className: 'engine.model.operation.WrapOperation',
+				baseVersion: 0,
+				position: op.position.toJSON(),
+				graveyardPosition: op.graveyardPosition.toJSON(),
+				howMany: 1
+			} );
+		} );
+
+		it( 'should create proper serialized object #2', () => {
+			const op = new WrapOperation(
+				new Position( root, [ 0 ] ),
+				1,
+				new Element( 'paragraph' ),
+				doc.version
+			);
+
+			const serialized = op.toJSON();
+
+			expect( serialized ).to.deep.equal( {
+				__className: 'engine.model.operation.WrapOperation',
+				baseVersion: 0,
+				position: op.position.toJSON(),
+				element: op.element.toJSON(),
+				howMany: 1
+			} );
+		} );
+	} );
+
+	describe( 'fromJSON', () => {
+		it( 'should create proper WrapOperation from json object', () => {
+			const op = new WrapOperation(
+				new Position( root, [ 0 ] ),
+				1,
+				new Position( doc.graveyard, [ 0 ] ),
+				doc.version
+			);
+
+			const serialized = op.toJSON();
+			const deserialized = WrapOperation.fromJSON( serialized, doc );
+
+			expect( deserialized ).to.deep.equal( op );
+		} );
+	} );
+} );