| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- /**
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import transformations from '../../../../src/model/delta/basic-transformations'; // eslint-disable-line no-unused-vars
- import deltaTransform from '../../../../src/model/delta/transform';
- const transform = deltaTransform.transform;
- import Element from '../../../../src/model/element';
- import Position from '../../../../src/model/position';
- import Range from '../../../../src/model/range';
- import MoveOperation from '../../../../src/model/operation/moveoperation';
- import InsertOperation from '../../../../src/model/operation/insertoperation';
- import MergeDelta from '../../../../src/model/delta/mergedelta';
- import WrapDelta from '../../../../src/model/delta/wrapdelta';
- import { getNodesAndText } from '../../../../tests/model/_utils/utils';
- import {
- applyDelta,
- expectDelta,
- getFilledDocument,
- getSplitDelta,
- getWrapDelta
- } from '../../../../tests/model/delta/transform/_utils/utils';
- describe( 'transform', () => {
- let doc, root, gy, baseVersion;
- beforeEach( () => {
- doc = getFilledDocument();
- root = doc.getRoot();
- gy = doc.graveyard;
- baseVersion = doc.version;
- } );
- describe( 'WrapDelta by', () => {
- let wrapDelta;
- beforeEach( () => {
- const wrapRange = new Range( new Position( root, [ 3, 3, 3, 1 ] ), new Position( root, [ 3, 3, 3, 5 ] ) );
- const wrapElement = new Element( 'E' );
- wrapDelta = getWrapDelta( wrapRange, wrapElement, baseVersion );
- } );
- describe( 'SplitDelta', () => {
- it( 'split position is between wrapped nodes', () => {
- const splitPosition = new Position( root, [ 3, 3, 3, 3 ] );
- const splitDelta = getSplitDelta( splitPosition, new Element( 'p' ), 9, baseVersion );
- const transformed = transform( wrapDelta, splitDelta );
- expect( transformed.length ).to.equal( 2 );
- baseVersion = splitDelta.operations.length;
- expectDelta( transformed[ 0 ], {
- type: MergeDelta,
- operations: [
- {
- type: MoveOperation,
- sourcePosition: new Position( root, [ 3, 3, 4, 0 ] ),
- howMany: 9,
- targetPosition: new Position( root, [ 3, 3, 3, 3 ] ),
- baseVersion
- },
- {
- type: MoveOperation,
- sourcePosition: new Position( root, [ 3, 3, 4 ] ),
- howMany: 1,
- targetPosition: new Position( gy, [ 0 ] ),
- baseVersion: baseVersion + 1
- }
- ]
- } );
- expectDelta( transformed[ 1 ], {
- type: WrapDelta,
- operations: [
- {
- type: InsertOperation,
- position: new Position( root, [ 3, 3, 3, 5 ] ),
- baseVersion: baseVersion + 2
- },
- {
- type: MoveOperation,
- sourcePosition: new Position( root, [ 3, 3, 3, 1 ] ),
- howMany: 4,
- targetPosition: new Position( root, [ 3, 3, 3, 5, 0 ] ),
- baseVersion: baseVersion + 3
- }
- ]
- } );
- // Test if deltas do what they should after applying transformed delta.
- applyDelta( splitDelta, doc );
- applyDelta( transformed[ 0 ], doc );
- applyDelta( transformed[ 1 ], doc );
- const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 3 ] ), 1 ) );
- // WrapDelta is applied. SplitDelta is discarded.
- expect( nodesAndText ).to.equal( 'PaEbcfoEobarxyzP' );
- } );
- it( 'split position is before wrapped nodes', () => {
- const splitPosition = new Position( root, [ 3, 3, 3, 1 ] );
- const splitDelta = getSplitDelta( splitPosition, new Element( 'p' ), 11, baseVersion );
- const transformed = transform( wrapDelta, splitDelta );
- expect( transformed.length ).to.equal( 1 );
- baseVersion = wrapDelta.operations.length;
- expectDelta( transformed[ 0 ], {
- type: WrapDelta,
- operations: [
- {
- type: InsertOperation,
- position: new Position( root, [ 3, 3, 4, 4 ] ),
- baseVersion
- },
- {
- type: MoveOperation,
- sourcePosition: new Position( root, [ 3, 3, 4, 0 ] ),
- howMany: 4,
- targetPosition: new Position( root, [ 3, 3, 4, 4, 0 ] ),
- baseVersion: baseVersion + 1
- }
- ]
- } );
- // Test if deltas do what they should after applying transformed delta.
- applyDelta( splitDelta, doc );
- applyDelta( transformed[ 0 ], doc );
- const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 3 ] ), 2 ) );
- // WrapDelta and SplitDelta are correctly applied.
- expect( nodesAndText ).to.equal( 'PaPPEbcfoEobarxyzP' );
- } );
- it( 'split position is inside wrapped node', () => {
- // For this case, we need different WrapDelta so it is overwritten.
- const wrapRange = new Range( new Position( root, [ 3, 3, 2 ] ), new Position( root, [ 3, 3, 4 ] ) );
- const wrapElement = new Element( 'E' );
- wrapDelta = getWrapDelta( wrapRange, wrapElement, baseVersion );
- const splitPosition = new Position( root, [ 3, 3, 3, 3 ] );
- const splitDelta = getSplitDelta( splitPosition, new Element( 'p' ), 9, baseVersion );
- const transformed = transform( wrapDelta, splitDelta );
- expect( transformed.length ).to.equal( 1 );
- baseVersion = wrapDelta.operations.length;
- expectDelta( transformed[ 0 ], {
- type: WrapDelta,
- operations: [
- {
- type: InsertOperation,
- position: new Position( root, [ 3, 3, 5 ] ),
- baseVersion
- },
- {
- type: MoveOperation,
- sourcePosition: new Position( root, [ 3, 3, 2 ] ),
- howMany: 3,
- targetPosition: new Position( root, [ 3, 3, 5, 0 ] ),
- baseVersion: baseVersion + 1
- }
- ]
- } );
- // Test if deltas do what they should after applying transformed delta.
- applyDelta( splitDelta, doc );
- applyDelta( transformed[ 0 ], doc );
- const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 2 ] ), 1 ) );
- // WrapDelta and SplitDelta are correctly applied.
- expect( nodesAndText ).to.equal( 'EXabcdXPabcPPfoobarxyzPE' );
- } );
- } );
- } );
- } );
|