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

Commit

Permalink
Fix: model.Range#getTransformedByDelta should not crash for `MoveDe…
Browse files Browse the repository at this point in the history
…lta` which moves 0 nodes.
  • Loading branch information
scofalik committed Mar 14, 2018
1 parent 0b58a3d commit 929e028
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/model/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,13 +399,24 @@ export default class Range {
for ( const operation of delta.operations ) {
if ( supportedTypes.has( operation.type ) ) {
for ( let i = 0; i < ranges.length; i++ ) {
const result = ranges[ i ]._getTransformedByDocumentChange(
operation.type,
delta.type,
operation.targetPosition || operation.position,
operation.howMany || operation.nodes.maxOffset,
operation.sourcePosition
);
let result;

if ( operation.type == 'insert' ) {
result = ranges[ i ]._getTransformedByDocumentChange(
operation.type,
delta.type,
operation.position,
operation.nodes.maxOffset
);
} else {
result = ranges[ i ]._getTransformedByDocumentChange(
operation.type,
delta.type,
operation.targetPosition,
operation.howMany,
operation.sourcePosition
);
}

ranges.splice( i, 1, ...result );

Expand Down
12 changes: 12 additions & 0 deletions tests/model/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,18 @@ describe( 'Range', () => {
expect( transformed[ 0 ].start.path ).to.deep.equal( [ 0, 1 ] );
expect( transformed[ 0 ].end.path ).to.deep.equal( [ 2, 1 ] );
} );

// #1358
it( 'should not crash and not transform the range if move delta moves 0 nodes', () => {
const range = new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 4 ] ) );
const delta = getMoveDelta( new Position( root, [ 0, 1 ] ), 0, new Position( root, [ 0, 3 ] ), 1 );

const transformed = range.getTransformedByDelta( delta );

expect( transformed.length ).to.equal( 1 );
expect( transformed[ 0 ].start.path ).to.deep.equal( [ 0, 2 ] );
expect( transformed[ 0 ].end.path ).to.deep.equal( [ 0, 4 ] );
} );
} );

describe( 'by RemoveDelta', () => {
Expand Down

0 comments on commit 929e028

Please sign in to comment.