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

Internal: Improved serialization of WrapOperation. #1505

Merged
merged 2 commits into from
Aug 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/model/operation/wrapoperation.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export default class WrapOperation extends Operation {
* the first {@link module:engine/model/item~Item model item} to wrap.
* @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.
* @param {module:engine/model/element~Element|module:engine/model/position~Position} elementOrPosition Wrapper
* element 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 );
} );
} );
} );