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

Commit

Permalink
Merge pull request #1361 from ckeditor/t/1358
Browse files Browse the repository at this point in the history
Fix: `model.Range#getTransformedByDelta` should not crash for `MoveDelta` which moves no nodes. Closes #1358.
  • Loading branch information
Piotr Jasiun authored Mar 15, 2018
2 parents 40f9d00 + 929e028 commit ff8ba9e
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 ff8ba9e

Please sign in to comment.