| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467 |
- /**
- * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /* bender-tags: model, operation */
- 'use strict';
- import transformations from '/ckeditor5/engine/model/delta/basic-transformations.js';
- /*jshint unused: false*/
- import transform from '/ckeditor5/engine/model/delta/transform.js';
- import Element from '/ckeditor5/engine/model/element.js';
- import Position from '/ckeditor5/engine/model/position.js';
- import Range from '/ckeditor5/engine/model/range.js';
- import Delta from '/ckeditor5/engine/model/delta/delta.js';
- import SplitDelta from '/ckeditor5/engine/model/delta/splitdelta.js';
- import InsertOperation from '/ckeditor5/engine/model/operation/insertoperation.js';
- import ReinsertOperation from '/ckeditor5/engine/model/operation/reinsertoperation.js';
- import MoveOperation from '/ckeditor5/engine/model/operation/moveoperation.js';
- import NoOperation from '/ckeditor5/engine/model/operation/nooperation.js';
- import { getNodesAndText, jsonParseStringify } from '/tests/engine/model/_utils/utils.js';
- import {
- applyDelta,
- expectDelta,
- getFilledDocument,
- getSplitDelta,
- getWrapDelta,
- getUnwrapDelta
- } from '/tests/engine/model/delta/transform/_utils/utils.js';
- describe( 'transform', () => {
- let doc, root, gy, baseVersion;
- beforeEach( () => {
- doc = getFilledDocument();
- root = doc.getRoot( 'root' );
- gy = doc.graveyard;
- baseVersion = doc.version;
- } );
- describe( 'SplitDelta by', () => {
- let splitDelta, splitPosition;
- beforeEach( () => {
- splitPosition = new Position( root, [ 3, 3, 3, 3 ] );
- splitDelta = getSplitDelta( splitPosition, new Element( 'p' ), 9, baseVersion );
- } );
- describe( 'SplitDelta', () => {
- it( 'split in same parent and offset', () => {
- let splitDeltaB = getSplitDelta( splitPosition, new Element( 'p' ), 9, baseVersion );
- let transformed = transform( splitDelta, splitDeltaB );
- baseVersion = splitDeltaB.operations.length;
- expect( transformed.length ).to.equal( 1 );
- expectDelta( transformed[ 0 ], {
- type: Delta,
- operations: [
- {
- type: NoOperation,
- baseVersion: baseVersion
- }
- ]
- } );
- // Test if deltas do what they should after applying transformed delta.
- applyDelta( splitDeltaB, doc );
- applyDelta( transformed[ 0 ], doc );
- let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 0 ] ), 5 ) );
- // Incoming split delta is discarded. Only one new element is created after applying both split deltas.
- // There are no empty P elements.
- expect( nodesAndText ).to.equal( 'XXXXXabcdXPabcPPfoobarxyzP' );
- } );
- it( 'split in same parent, incoming delta splits closer', () => {
- let splitDeltaB = getSplitDelta( new Position( root, [ 3, 3, 3, 5 ] ), new Element( 'p' ), 7, baseVersion );
- let transformed = transform( splitDelta, splitDeltaB );
- baseVersion = splitDeltaB.operations.length;
- expect( transformed.length ).to.equal( 1 );
- expectDelta( transformed[ 0 ], {
- type: SplitDelta,
- operations: [
- {
- type: InsertOperation,
- position: new Position( root, [ 3, 3, 4 ] ),
- baseVersion: baseVersion
- },
- {
- type: MoveOperation,
- sourcePosition: new Position( root, [ 3, 3, 3, 3 ] ),
- howMany: 2,
- targetPosition: new Position( root, [ 3, 3, 4, 0 ] ),
- baseVersion: baseVersion + 1
- }
- ]
- } );
- // Test if deltas do what they should after applying transformed delta.
- applyDelta( splitDeltaB, doc );
- applyDelta( transformed[ 0 ], doc );
- let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 0 ] ), 6 ) );
- // P element is correctly split, there are three P elements, letters in P elements are in correct order.
- expect( nodesAndText ).to.equal( 'XXXXXabcdXPabcPPfoPPobarxyzP' );
- } );
- it( 'split in same parent, incoming delta splits closer, split deltas have reinsert operations', () => {
- let reOp = new ReinsertOperation(
- new Position( gy, [ 1 ] ),
- 1,
- Position.createFromPosition( splitDelta.operations[ 0 ].position ),
- splitDelta.operations[ 0 ].baseVersion
- );
- splitDelta.operations[ 0 ] = reOp;
- let splitDeltaB = getSplitDelta( new Position( root, [ 3, 3, 3, 5 ] ), new Element( 'p' ), 7, baseVersion );
- reOp = new ReinsertOperation(
- new Position( gy, [ 0 ] ),
- 1,
- Position.createFromPosition( splitDeltaB.operations[ 0 ].position ),
- splitDeltaB.operations[ 0 ].baseVersion
- );
- splitDeltaB.operations[ 0 ] = reOp;
- let transformed = transform( splitDelta, splitDeltaB );
- baseVersion = splitDeltaB.operations.length;
- expect( transformed.length ).to.equal( 1 );
- expectDelta( transformed[ 0 ], {
- type: SplitDelta,
- operations: [
- {
- type: ReinsertOperation,
- sourcePosition: new Position( gy, [ 0 ] ),
- howMany: 1,
- targetPosition: new Position( root, [ 3, 3, 4 ] ),
- baseVersion: baseVersion
- },
- {
- type: MoveOperation,
- sourcePosition: new Position( root, [ 3, 3, 3, 3 ] ),
- howMany: 2,
- targetPosition: new Position( root, [ 3, 3, 4, 0 ] ),
- baseVersion: baseVersion + 1
- }
- ]
- } );
- } );
- it( 'split in same parent, incoming delta splits further', () => {
- let splitDeltaB = getSplitDelta( new Position( root, [ 3, 3, 3, 1 ] ), new Element( 'p' ), 11, baseVersion );
- let transformed = transform( splitDelta, splitDeltaB );
- baseVersion = splitDeltaB.operations.length;
- expect( transformed.length ).to.equal( 1 );
- expectDelta( transformed[ 0 ], {
- type: SplitDelta,
- operations: [
- {
- type: InsertOperation,
- position: new Position( root, [ 3, 3, 5 ] ),
- baseVersion: baseVersion
- },
- {
- type: MoveOperation,
- sourcePosition: new Position( root, [ 3, 3, 4, 2 ] ),
- howMany: 9,
- targetPosition: new Position( root, [ 3, 3, 5, 0 ] ),
- baseVersion: baseVersion + 1
- }
- ]
- } );
- // Test if deltas do what they should after applying transformed delta.
- applyDelta( splitDeltaB, doc );
- applyDelta( transformed[ 0 ], doc );
- let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 0 ] ), 6 ) );
- // P element is correctly split, there are three P elements, letters in P elements are in correct order.
- expect( nodesAndText ).to.equal( 'XXXXXabcdXPaPPbcPPfoobarxyzP' );
- } );
- it( 'split in same parent, incoming delta splits further, split deltas have reinsert operations', () => {
- let reOp = new ReinsertOperation(
- new Position( gy, [ 1 ] ),
- 1,
- Position.createFromPosition( splitDelta.operations[ 0 ].position ),
- splitDelta.operations[ 0 ].baseVersion
- );
- splitDelta.operations[ 0 ] = reOp;
- let splitDeltaB = getSplitDelta( new Position( root, [ 3, 3, 3, 1 ] ), new Element( 'p' ), 11, baseVersion );
- reOp = new ReinsertOperation(
- new Position( gy, [ 0 ] ),
- 1,
- Position.createFromPosition( splitDeltaB.operations[ 0 ].position ),
- splitDeltaB.operations[ 0 ].baseVersion
- );
- splitDeltaB.operations[ 0 ] = reOp;
- let transformed = transform( splitDelta, splitDeltaB );
- baseVersion = splitDeltaB.operations.length;
- expect( transformed.length ).to.equal( 1 );
- expectDelta( transformed[ 0 ], {
- type: SplitDelta,
- operations: [
- {
- type: ReinsertOperation,
- sourcePosition: new Position( gy, [ 0 ] ),
- howMany: 1,
- targetPosition: new Position( root, [ 3, 3, 5 ] ),
- baseVersion: baseVersion
- },
- {
- type: MoveOperation,
- sourcePosition: new Position( root, [ 3, 3, 4, 2 ] ),
- howMany: 9,
- targetPosition: new Position( root, [ 3, 3, 5, 0 ] ),
- baseVersion: baseVersion + 1
- }
- ]
- } );
- } );
- it( 'split in split parent', () => {
- let splitDeltaB = getSplitDelta( new Position( root, [ 3, 3, 3 ] ), new Element( 'div' ), 1, baseVersion );
- let transformed = transform( splitDelta, splitDeltaB );
- baseVersion = splitDeltaB.operations.length;
- expect( transformed.length ).to.equal( 1 );
- expectDelta( transformed[ 0 ], {
- type: SplitDelta,
- operations: [
- {
- type: InsertOperation,
- position: new Position( root, [ 3, 4, 1 ] ),
- baseVersion: baseVersion
- },
- {
- type: MoveOperation,
- sourcePosition: new Position( root, [ 3, 4, 0, 3 ] ),
- howMany: 9,
- targetPosition: new Position( root, [ 3, 4, 1, 0 ] ),
- baseVersion: baseVersion + 1
- }
- ]
- } );
- // Test if deltas do what they should after applying transformed delta.
- applyDelta( splitDeltaB, doc );
- applyDelta( transformed[ 0 ], doc );
- let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3 ] ), 2 ) );
- // DIV and P elements are correctly split.
- expect( nodesAndText ).to.equal( 'DIVXXXXXabcdXDIVDIVPabcPPfoobarxyzPDIV' );
- } );
- } );
- describe( 'UnwrapDelta', () => {
- it( 'split position directly in unwrapped node', () => {
- let unwrapDelta = getUnwrapDelta( new Position( root, [ 3, 3, 3 ] ), 12, baseVersion );
- let transformed = transform( splitDelta, unwrapDelta );
- baseVersion = unwrapDelta.operations.length;
- expect( transformed.length ).to.equal( 1 );
- expectDelta( transformed[ 0 ], {
- type: Delta,
- operations: [
- {
- type: NoOperation,
- baseVersion: baseVersion
- }
- ]
- } );
- // Test if deltas do what they should after applying transformed delta.
- applyDelta( unwrapDelta, doc );
- applyDelta( transformed[ 0 ], doc );
- let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3 ] ), 1 ) );
- // UnwrapDelta is applied. SplitDelta is discarded.
- expect( nodesAndText ).to.equal( 'DIVXXXXXabcdXabcfoobarxyzDIV' );
- } );
- it( 'split position indirectly in unwrapped node', () => {
- let unwrapDelta = getUnwrapDelta( new Position( root, [ 3, 3 ] ), 4, baseVersion );
- let transformed = transform( splitDelta, unwrapDelta );
- expect( transformed.length ).to.equal( 1 );
- baseVersion = unwrapDelta.operations.length;
- expectDelta( transformed[ 0 ], {
- type: SplitDelta,
- operations: [
- {
- type: InsertOperation,
- position: new Position( root, [ 3, 7 ] ),
- baseVersion: baseVersion
- },
- {
- type: MoveOperation,
- sourcePosition: new Position( root, [ 3, 6, 3 ] ),
- howMany: 9,
- targetPosition: new Position( root, [ 3, 7, 0 ] ),
- baseVersion: baseVersion + 1
- }
- ]
- } );
- // Test if deltas do what they should after applying transformed delta.
- applyDelta( unwrapDelta, doc );
- applyDelta( transformed[ 0 ], doc );
- let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3 ] ), 1 ) );
- // UnwrapDelta and SplitDelta are correctly applied.
- expect( nodesAndText ).to.equal( 'DIVXXXXXaXXXXXXabcdXPabcPPfoobarxyzPDIV' );
- } );
- } );
- describe( 'WrapDelta', () => {
- it( 'split position is between wrapped nodes', () => {
- let wrapRange = new Range( new Position( root, [ 3, 3, 3, 1 ] ), new Position( root, [ 3, 3, 3, 5 ] ) );
- let wrapElement = new Element( 'E' );
- let wrapDelta = getWrapDelta( wrapRange, wrapElement, baseVersion );
- let transformed = transform( splitDelta, wrapDelta );
- baseVersion = wrapDelta.operations.length;
- expect( transformed.length ).to.equal( 1 );
- expectDelta( transformed[ 0 ], {
- type: Delta,
- operations: [
- {
- type: NoOperation,
- baseVersion: baseVersion
- }
- ]
- } );
- // Test if deltas do what they should after applying transformed delta.
- applyDelta( wrapDelta, doc );
- applyDelta( transformed[ 0 ], doc );
- let 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', () => {
- let wrapRange = new Range( new Position( root, [ 3, 3, 3, 5 ] ), new Position( root, [ 3, 3, 3, 7 ] ) );
- let wrapElement = new Element( 'E' );
- let wrapDelta = getWrapDelta( wrapRange, wrapElement, baseVersion );
- let transformed = transform( splitDelta, wrapDelta );
- expect( transformed.length ).to.equal( 1 );
- baseVersion = wrapDelta.operations.length;
- expectDelta( transformed[ 0 ], {
- type: SplitDelta,
- operations: [
- {
- type: InsertOperation,
- position: new Position( root, [ 3, 3, 4 ] ),
- baseVersion: baseVersion
- },
- {
- type: MoveOperation,
- sourcePosition: new Position( root, [ 3, 3, 3, 3 ] ),
- howMany: 8,
- targetPosition: new Position( root, [ 3, 3, 4, 0 ] ),
- baseVersion: baseVersion + 1
- }
- ]
- } );
- // Test if deltas do what they should after applying transformed delta.
- applyDelta( wrapDelta, doc );
- applyDelta( transformed[ 0 ], doc );
- let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 3 ] ), 2 ) );
- // WrapDelta and SplitDelta are correctly applied.
- expect( nodesAndText ).to.equal( 'PabcPPfoEobEarxyzP' );
- } );
- it( 'split position is inside wrapped node', () => {
- let wrapRange = new Range( new Position( root, [ 3, 3, 2 ] ), new Position( root, [ 3, 3, 4 ] ) );
- let wrapElement = new Element( 'E' );
- let wrapDelta = getWrapDelta( wrapRange, wrapElement, baseVersion );
- let transformed = transform( splitDelta, wrapDelta );
- expect( transformed.length ).to.equal( 1 );
- baseVersion = wrapDelta.operations.length;
- expectDelta( transformed[ 0 ], {
- type: SplitDelta,
- operations: [
- {
- type: InsertOperation,
- position: new Position( root, [ 3, 3, 2, 2 ] ),
- baseVersion: baseVersion
- },
- {
- type: MoveOperation,
- sourcePosition: new Position( root, [ 3, 3, 2, 1, 3 ] ),
- howMany: 9,
- targetPosition: new Position( root, [ 3, 3, 2, 2, 0 ] ),
- baseVersion: baseVersion + 1
- }
- ]
- } );
- // Test if deltas do what they should after applying transformed delta.
- applyDelta( wrapDelta, doc );
- applyDelta( transformed[ 0 ], doc );
- let nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 2 ] ), 1 ) );
- // WrapDelta and SplitDelta are correctly applied.
- expect( nodesAndText ).to.equal( 'EXabcdXPabcPPfoobarxyzPE' );
- } );
- } );
- } );
- } );
|