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

Commit ff8ba9e

Browse files
author
Piotr Jasiun
authored
Merge pull request #1361 from ckeditor/t/1358
Fix: `model.Range#getTransformedByDelta` should not crash for `MoveDelta` which moves no nodes. Closes #1358.
2 parents 40f9d00 + 929e028 commit ff8ba9e

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

src/model/range.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -399,13 +399,24 @@ export default class Range {
399399
for ( const operation of delta.operations ) {
400400
if ( supportedTypes.has( operation.type ) ) {
401401
for ( let i = 0; i < ranges.length; i++ ) {
402-
const result = ranges[ i ]._getTransformedByDocumentChange(
403-
operation.type,
404-
delta.type,
405-
operation.targetPosition || operation.position,
406-
operation.howMany || operation.nodes.maxOffset,
407-
operation.sourcePosition
408-
);
402+
let result;
403+
404+
if ( operation.type == 'insert' ) {
405+
result = ranges[ i ]._getTransformedByDocumentChange(
406+
operation.type,
407+
delta.type,
408+
operation.position,
409+
operation.nodes.maxOffset
410+
);
411+
} else {
412+
result = ranges[ i ]._getTransformedByDocumentChange(
413+
operation.type,
414+
delta.type,
415+
operation.targetPosition,
416+
operation.howMany,
417+
operation.sourcePosition
418+
);
419+
}
409420

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

tests/model/range.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,18 @@ describe( 'Range', () => {
933933
expect( transformed[ 0 ].start.path ).to.deep.equal( [ 0, 1 ] );
934934
expect( transformed[ 0 ].end.path ).to.deep.equal( [ 2, 1 ] );
935935
} );
936+
937+
// #1358
938+
it( 'should not crash and not transform the range if move delta moves 0 nodes', () => {
939+
const range = new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 4 ] ) );
940+
const delta = getMoveDelta( new Position( root, [ 0, 1 ] ), 0, new Position( root, [ 0, 3 ] ), 1 );
941+
942+
const transformed = range.getTransformedByDelta( delta );
943+
944+
expect( transformed.length ).to.equal( 1 );
945+
expect( transformed[ 0 ].start.path ).to.deep.equal( [ 0, 2 ] );
946+
expect( transformed[ 0 ].end.path ).to.deep.equal( [ 0, 4 ] );
947+
} );
936948
} );
937949

938950
describe( 'by RemoveDelta', () => {

0 commit comments

Comments
 (0)