Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Internal: Improved serialization of WrapOperation.
Browse files Browse the repository at this point in the history
  • Loading branch information
oskarwrobel committed Aug 17, 2018
1 parent 6c1f87e commit e52bffb
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/model/operation/wrapoperation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
* `position.offset + howMany`.
* @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
* can be applied or `null` if the operation operates on detached (non-document) tree.
*/
Expand Down Expand Up @@ -171,8 +171,12 @@ export default class WrapOperation extends Operation {

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();
delete json.element;
}

return json;
Expand Down
88 changes: 88 additions & 0 deletions tests/model/operation/wrapoperation.js
Original file line number Diff line number Diff line change
@@ -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 );
} );
} );
} );

0 comments on commit e52bffb

Please sign in to comment.