| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- /**
- * @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 Text from '../../../../src/model/text';
- import Position from '../../../../src/model/position';
- import Range from '../../../../src/model/range';
- import AttributeDelta from '../../../../src/model/delta/attributedelta';
- import Delta from '../../../../src/model/delta/delta';
- import AttributeOperation from '../../../../src/model/operation/attributeoperation';
- import NoOperation from '../../../../src/model/operation/nooperation';
- import {
- expectDelta,
- getFilledDocument,
- getAttributeDelta,
- getWeakInsertDelta,
- getSplitDelta
- } from '../../../../tests/model/delta/transform/_utils/utils';
- describe( 'transform', () => {
- let doc, root, baseVersion, context;
- beforeEach( () => {
- doc = getFilledDocument();
- root = doc.getRoot();
- baseVersion = doc.version;
- context = { isStrong: false };
- } );
- describe( 'AttributeDelta by', () => {
- describe( 'WeakInsertDelta', () => {
- it( 'weak insert inside attribute range should "fix" splitting the range', () => {
- const attrRange = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 3, 3, 9 ] ) );
- const attrDelta = getAttributeDelta( attrRange, 'key', 'old', 'new', baseVersion );
- const insertPosition = new Position( root, [ 3, 3, 0 ] );
- const insertDelta = getWeakInsertDelta(
- insertPosition,
- [
- 'a',
- new Text( 'b', { key: 'new' } ),
- new Text( 'c', { key: 'different' } ),
- 'de'
- ],
- baseVersion
- );
- const transformed = transform( attrDelta, insertDelta, context );
- expect( transformed.length ).to.equal( 2 );
- baseVersion = insertDelta.operations.length;
- expectDelta( transformed[ 0 ], {
- type: AttributeDelta,
- operations: [
- {
- type: AttributeOperation,
- range: new Range( new Position( root, [ 3, 3, 5 ] ), new Position( root, [ 3, 3, 8, 9 ] ) ),
- key: 'key',
- oldValue: 'old',
- newValue: 'new',
- baseVersion
- },
- {
- type: AttributeOperation,
- range: new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 3, 0 ] ) ),
- key: 'key',
- oldValue: 'old',
- newValue: 'new',
- baseVersion: baseVersion + 1
- }
- ]
- } );
- expectDelta( transformed[ 1 ], {
- type: AttributeDelta,
- operations: [
- {
- type: AttributeOperation,
- range: new Range( new Position( root, [ 3, 3, 0 ] ), new Position( root, [ 3, 3, 1 ] ) ),
- key: 'key',
- oldValue: null,
- newValue: 'new',
- baseVersion: baseVersion + 2
- },
- {
- type: AttributeOperation,
- range: new Range( new Position( root, [ 3, 3, 2 ] ), new Position( root, [ 3, 3, 3 ] ) ),
- key: 'key',
- oldValue: 'different',
- newValue: 'new',
- baseVersion: baseVersion + 3
- },
- {
- type: AttributeOperation,
- range: new Range( new Position( root, [ 3, 3, 3 ] ), new Position( root, [ 3, 3, 5 ] ) ),
- key: 'key',
- oldValue: null,
- newValue: 'new',
- baseVersion: baseVersion + 4
- }
- ]
- } );
- } );
- it( 'should be normally transformed if weak insert is not in the attribute range', () => {
- const attrRange = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 3, 3, 9 ] ) );
- const attrDelta = getAttributeDelta( attrRange, 'key', 'old', 'new', baseVersion );
- const insertPosition = new Position( root, [ 5 ] );
- const insertDelta = getWeakInsertDelta( insertPosition, 'abc', baseVersion );
- const transformed = transform( attrDelta, insertDelta, context );
- expect( transformed.length ).to.equal( 1 );
- baseVersion = insertDelta.operations.length;
- expectDelta( transformed[ 0 ], {
- type: AttributeDelta,
- operations: [
- {
- type: AttributeOperation,
- range: new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 3, 3, 9 ] ) ),
- key: 'key',
- oldValue: 'old',
- newValue: 'new',
- baseVersion
- }
- ]
- } );
- } );
- } );
- describe( 'SplitDelta', () => {
- it( 'change attribute of split element', () => {
- const attrRange1 = Range.createFromParentsAndOffsets( root, 0, root, 1 );
- const attrRange2 = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 4 ] ) );
- const attrDelta = new AttributeDelta();
- attrDelta.addOperation( new AttributeOperation( attrRange1, 'key', 'old', 'new', baseVersion ) );
- attrDelta.addOperation( new AttributeOperation( attrRange2, 'key', 'old', 'new', baseVersion ) );
- const splitPosition = new Position( root, [ 3, 3, 3, 3 ] );
- const splitDelta = getSplitDelta( splitPosition, new Element( 'p' ), 9, baseVersion );
- const transformed = transform( attrDelta, splitDelta, context );
- expect( transformed.length ).to.equal( 2 );
- baseVersion = splitDelta.operations.length;
- expectDelta( transformed[ 0 ], {
- type: AttributeDelta,
- operations: [
- {
- type: AttributeOperation,
- range: new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
- key: 'key',
- oldValue: 'old',
- newValue: 'new',
- baseVersion
- },
- {
- type: AttributeOperation,
- range: new Range( new Position( root, [ 3, 3, 5 ] ), new Position( root, [ 3, 4 ] ) ),
- key: 'key',
- oldValue: 'old',
- newValue: 'new',
- baseVersion: baseVersion + 1
- },
- {
- type: AttributeOperation,
- range: new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 3, 4 ] ) ),
- key: 'key',
- oldValue: 'old',
- newValue: 'new',
- baseVersion: baseVersion + 2
- },
- {
- type: AttributeOperation,
- range: new Range( new Position( root, [ 3, 3, 4, 0 ] ), new Position( root, [ 3, 3, 4, 9 ] ) ),
- key: 'key',
- oldValue: 'old',
- newValue: 'new',
- baseVersion: baseVersion + 3
- }
- ]
- } );
- expectDelta( transformed[ 1 ], {
- type: AttributeDelta,
- operations: [
- {
- type: AttributeOperation,
- range: new Range( new Position( root, [ 3, 3, 4 ] ), new Position( root, [ 3, 3, 4, 0 ] ) ),
- key: 'key',
- oldValue: null,
- newValue: 'new',
- baseVersion: baseVersion + 4
- }
- ]
- } );
- } );
- // A different case.
- it( 'change attribute of split element #2', () => {
- const attrRange = new Range( new Position( root, [ 3, 3, 3 ] ), new Position( root, [ 3, 3, 3, 0 ] ) );
- const attrDelta = new AttributeDelta();
- attrDelta.addOperation( new AttributeOperation( attrRange, 'foo', null, 'bar', baseVersion ) );
- const splitPosition = new Position( root, [ 3, 3, 3, 3 ] );
- const splitDelta = getSplitDelta( splitPosition, new Element( 'p', { foo: 'old' } ), 9, baseVersion );
- const transformed = transform( attrDelta, splitDelta, context );
- expect( transformed.length ).to.equal( 2 );
- baseVersion = splitDelta.operations.length;
- expectDelta( transformed[ 0 ], {
- type: AttributeDelta,
- operations: [
- {
- type: AttributeOperation,
- range: new Range( new Position( root, [ 3, 3, 3 ] ), new Position( root, [ 3, 3, 3, 0 ] ) ),
- key: 'foo',
- oldValue: null,
- newValue: 'bar',
- baseVersion
- }
- ]
- } );
- expectDelta( transformed[ 1 ], {
- type: AttributeDelta,
- operations: [
- {
- type: AttributeOperation,
- range: new Range( new Position( root, [ 3, 3, 4 ] ), new Position( root, [ 3, 3, 4, 0 ] ) ),
- key: 'foo',
- oldValue: 'old',
- newValue: 'bar',
- baseVersion: baseVersion + 1
- }
- ]
- } );
- } );
- } );
- describe( 'AttributeDelta', () => {
- it( 'should be converted to no delta if all operations are NoOperations', () => {
- const rangeA = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
- const rangeB = new Range( new Position( root, [ 4 ] ), new Position( root, [ 7 ] ) );
- const attrDeltaA = new AttributeDelta();
- attrDeltaA.addOperation( new AttributeOperation( rangeA, 'key', 'old', 'new', 0 ) );
- attrDeltaA.addOperation( new AttributeOperation( rangeB, 'key', null, 'new', 1 ) );
- const attrDeltaB = new AttributeDelta();
- attrDeltaB.addOperation( new AttributeOperation( rangeA, 'key', 'old', 'other', 0 ) );
- attrDeltaB.addOperation( new AttributeOperation( rangeB, 'key', null, 'other', 1 ) );
- const transformed = transform( attrDeltaA, attrDeltaB, context );
- expect( transformed.length ).to.equal( 1 );
- expectDelta( transformed[ 0 ], {
- type: Delta,
- operations: [
- {
- type: NoOperation,
- baseVersion: 2
- },
- {
- type: NoOperation,
- baseVersion: 3
- }
- ]
- } );
- } );
- it( 'should put AttributeOperation as first operation in the delta', () => {
- const rangeA = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
- const rangeB = new Range( new Position( root, [ 4 ] ), new Position( root, [ 7 ] ) );
- const attrDeltaA = new AttributeDelta();
- attrDeltaA.addOperation( new AttributeOperation( rangeA, 'key', 'old', 'new', 0 ) );
- attrDeltaA.addOperation( new AttributeOperation( rangeB, 'key', null, 'new', 1 ) );
- const attrDeltaB = new AttributeDelta();
- attrDeltaB.addOperation( new AttributeOperation( rangeA, 'key', 'old', 'other', 0 ) );
- const transformed = transform( attrDeltaA, attrDeltaB, context );
- expect( transformed.length ).to.equal( 1 );
- expectDelta( transformed[ 0 ], {
- type: AttributeDelta,
- operations: [
- {
- type: AttributeOperation,
- range: rangeB,
- key: 'key',
- oldValue: null,
- newValue: 'new',
- baseVersion: 1
- },
- {
- type: NoOperation,
- baseVersion: 2
- }
- ]
- } );
- } );
- } );
- } );
- } );
|