| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- /**
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import transformations from '../../../../src/model/delta/basic-transformations';
- /*jshint unused: false*/
- import { transformDeltaSets } from '../../../../src/model/delta/transform';
- import Document from '../../../../src/model/document';
- import Element from '../../../../src/model/element';
- import Text from '../../../../src/model/text';
- import Position from '../../../../src/model/position';
- import Delta from '../../../../src/model/delta/delta';
- import InsertDelta from '../../../../src/model/delta/insertdelta';
- import RemoveDelta from '../../../../src/model/delta/removedelta';
- import SplitDelta from '../../../../src/model/delta/splitdelta';
- import NoOperation from '../../../../src/model/operation/nooperation';
- import MoveOperation from '../../../../src/model/operation/moveoperation';
- import RemoveOperation from '../../../../src/model/operation/removeoperation';
- import InsertOperation from '../../../../src/model/operation/insertoperation';
- import {
- expectDelta,
- getInsertDelta,
- getSplitDelta,
- getRemoveDelta
- } from '../../../../tests/model/delta/transform/_utils/utils';
- describe( 'transform', () => {
- let doc, root, gy, baseVersion;
- beforeEach( () => {
- doc = new Document();
- root = doc.createRoot();
- root.appendChildren( new Element( 'p', null, new Text( 'foobar' ) ) );
- gy = doc.graveyard;
- baseVersion = doc.version;
- } );
- describe( 'transformDeltaSets', () => {
- it( 'should transform two deltas', () => {
- const insertDelta = getInsertDelta( new Position( root, [ 0, 4 ] ), new Text( 'xxx' ), baseVersion );
- const removeDelta = getRemoveDelta( new Position( root, [ 0, 0 ] ), 2, baseVersion );
- const { deltasA, deltasB } = transformDeltaSets( [ insertDelta ], [ removeDelta ] );
- expect( deltasA.length ).to.equal( 1 );
- expect( deltasB.length ).to.equal( 1 );
- expectDelta( deltasA[ 0 ], {
- type: InsertDelta,
- operations: [
- {
- type: InsertOperation,
- position: new Position( root, [ 0, 2 ] ),
- baseVersion: 1
- }
- ]
- } );
- expectDelta( deltasB[ 0 ], {
- type: RemoveDelta,
- operations: [
- {
- type: RemoveOperation,
- sourcePosition: new Position( root, [ 0, 0 ] ),
- howMany: 2,
- baseVersion: 1
- }
- ]
- } );
- } );
- it( 'should transform two deltas - reverse', () => {
- const insertDelta = getInsertDelta( new Position( root, [ 0, 4 ] ), new Text( 'xxx' ), baseVersion );
- const removeDelta = getRemoveDelta( new Position( root, [ 0, 0 ] ), 2, baseVersion );
- const { deltasA, deltasB } = transformDeltaSets( [ removeDelta ], [ insertDelta ] );
- expect( deltasA.length ).to.equal( 1 );
- expect( deltasB.length ).to.equal( 1 );
- expectDelta( deltasA[ 0 ], {
- type: RemoveDelta,
- operations: [
- {
- type: RemoveOperation,
- sourcePosition: new Position( root, [ 0, 0 ] ),
- howMany: 2,
- baseVersion: 1
- }
- ]
- } );
- expectDelta( deltasB[ 0 ], {
- type: InsertDelta,
- operations: [
- {
- type: InsertOperation,
- position: new Position( root, [ 0, 2 ] ),
- baseVersion: 1
- }
- ]
- } );
- } );
- it( 'should transform two arrays of deltas', () => {
- const splitDelta = getSplitDelta( new Position( root, [ 0, 3 ] ), new Element( 'p' ), 3, baseVersion );
- const insertDeltaX = getInsertDelta( new Position( root, [ 0, 3 ] ), new Text( 'xxx' ), baseVersion + 2 );
- const removeDelta = getRemoveDelta( new Position( root, [ 0, 2 ] ), 2, baseVersion );
- const insertDeltaY = getInsertDelta( new Position( root, [ 0, 4 ] ), new Text( 'yyy' ), baseVersion + 1 );
- const { deltasA, deltasB } = transformDeltaSets( [ splitDelta, insertDeltaX ], [ removeDelta, insertDeltaY ], true );
- expect( deltasA.length ).to.equal( 3 );
- expect( deltasB.length ).to.equal( 2 );
- expectDelta( deltasA[ 0 ], {
- type: SplitDelta,
- operations: [
- {
- type: InsertOperation,
- position: new Position( root, [ 1 ] ),
- baseVersion: 2
- },
- {
- type: MoveOperation,
- sourcePosition: new Position( root, [ 0, 2 ] ),
- howMany: 5,
- targetPosition: new Position( root, [ 1, 0 ] ),
- baseVersion: 3
- }
- ]
- } );
- expectDelta( deltasA[ 1 ], {
- type: InsertDelta,
- operations: [
- {
- type: InsertOperation,
- position: new Position( root, [ 0, 2 ] ),
- baseVersion: 4
- }
- ]
- } );
- expectDelta( deltasA[ 2 ], {
- type: Delta,
- operations: [
- {
- type: NoOperation,
- baseVersion: 5
- }
- ]
- } );
- expectDelta( deltasB[ 0 ], {
- type: RemoveDelta,
- operations: [
- {
- type: RemoveOperation,
- sourcePosition: new Position( root, [ 1, 0 ] ),
- howMany: 1,
- baseVersion: 3
- },
- {
- type: RemoveOperation,
- sourcePosition: new Position( root, [ 0, 2 ] ),
- howMany: 1,
- baseVersion: 4
- }
- ]
- } );
- expectDelta( deltasB[ 1 ], {
- type: InsertDelta,
- operations: [
- {
- type: InsertOperation,
- position: new Position( root, [ 1, 2 ] ),
- baseVersion: 5
- }
- ]
- } );
- } );
- it( 'should transform two arrays of deltas - reverse', () => {
- const splitDelta = getSplitDelta( new Position( root, [ 0, 3 ] ), new Element( 'p' ), 3, baseVersion );
- const insertDeltaX = getInsertDelta( new Position( root, [ 0, 3 ] ), new Text( 'xxx' ), baseVersion + 2 );
- const removeDelta = getRemoveDelta( new Position( root, [ 0, 2 ] ), 2, baseVersion );
- const insertDeltaY = getInsertDelta( new Position( root, [ 0, 4 ] ), new Text( 'yyy' ), baseVersion + 1 );
- const { deltasA, deltasB } = transformDeltaSets( [ removeDelta, insertDeltaY ], [ splitDelta, insertDeltaX ], true );
- expect( deltasA.length ).to.equal( 2 );
- expect( deltasB.length ).to.equal( 3 );
- expectDelta( deltasA[ 0 ], {
- type: RemoveDelta,
- operations: [
- {
- type: RemoveOperation,
- sourcePosition: new Position( root, [ 1, 0 ] ),
- howMany: 1,
- baseVersion: 3
- },
- {
- type: RemoveOperation,
- sourcePosition: new Position( root, [ 0, 2 ] ),
- howMany: 1,
- baseVersion: 4
- }
- ]
- } );
- expectDelta( deltasA[ 1 ], {
- type: InsertDelta,
- operations: [
- {
- type: InsertOperation,
- position: new Position( root, [ 1, 2 ] ),
- baseVersion: 5
- }
- ]
- } );
- expectDelta( deltasB[ 0 ], {
- type: SplitDelta,
- operations: [
- {
- type: InsertOperation,
- position: new Position( root, [ 1 ] ),
- baseVersion: 2
- },
- {
- type: MoveOperation,
- sourcePosition: new Position( root, [ 0, 2 ] ),
- howMany: 5,
- targetPosition: new Position( root, [ 1, 0 ] ),
- baseVersion: 3
- }
- ]
- } );
- expectDelta( deltasB[ 1 ], {
- type: InsertDelta,
- operations: [
- {
- type: InsertOperation,
- position: new Position( root, [ 0, 2 ] ),
- baseVersion: 4
- }
- ]
- } );
- expectDelta( deltasB[ 2 ], {
- type: Delta,
- operations: [
- {
- type: NoOperation,
- baseVersion: 5
- }
- ]
- } );
- } );
- it( 'importance flag - first set is important', () => {
- const insertDeltaA = getInsertDelta( new Position( root, [ 0, 4 ] ), new Text( 'xxx' ), baseVersion );
- const insertDeltaB = getInsertDelta( new Position( root, [ 0, 4 ] ), new Text( 'yyy' ), baseVersion );
- let { deltasA, deltasB } = transformDeltaSets( [ insertDeltaA ], [ insertDeltaB ], true );
- expectDelta( deltasA[ 0 ], {
- type: InsertDelta,
- operations: [
- {
- type: InsertOperation,
- position: new Position( root, [ 0, 4 ] ),
- baseVersion: 1
- }
- ]
- } );
- expectDelta( deltasB[ 0 ], {
- type: InsertDelta,
- operations: [
- {
- type: InsertOperation,
- position: new Position( root, [ 0, 7 ] ),
- baseVersion: 1
- }
- ]
- } );
- } );
- it( 'importance flag - second set is important', () => {
- const insertDeltaA = getInsertDelta( new Position( root, [ 0, 4 ] ), new Text( 'xxx' ), baseVersion );
- const insertDeltaB = getInsertDelta( new Position( root, [ 0, 4 ] ), new Text( 'yyy' ), baseVersion );
- let { deltasA, deltasB } = transformDeltaSets( [ insertDeltaA ], [ insertDeltaB ], false );
- expectDelta( deltasA[ 0 ], {
- type: InsertDelta,
- operations: [
- {
- type: InsertOperation,
- position: new Position( root, [ 0, 7 ] ),
- baseVersion: 1
- }
- ]
- } );
- expectDelta( deltasB[ 0 ], {
- type: InsertDelta,
- operations: [
- {
- type: InsertOperation,
- position: new Position( root, [ 0, 4 ] ),
- baseVersion: 1
- }
- ]
- } );
- } );
- it( 'should not modify original deltas or arrays', () => {
- const insertDeltaA = getInsertDelta( new Position( root, [ 0, 0 ] ), new Text( 'x' ), baseVersion );
- const insertDeltaB = getInsertDelta( new Position( root, [ 1, 0 ] ), new Text( 'y' ), baseVersion );
- const originalDeltasA = [ insertDeltaA ];
- const originalDeltasB = [ insertDeltaB ];
- let { deltasA, deltasB } = transformDeltaSets( originalDeltasA, originalDeltasB, false );
- expect( deltasA ).to.not.equal( originalDeltasA );
- expect( deltasB ).to.not.equal( originalDeltasB );
- expect( deltasA[ 0 ] ).to.not.equal( originalDeltasA[ 0 ] );
- expect( deltasB[ 0 ] ).to.not.equal( originalDeltasB[ 0 ] );
- } );
- } );
- } );
|