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 #1143 from ckeditor/t/1142
Browse files Browse the repository at this point in the history
Fix: `model.Range` now will be correctly transformed if it was at the end of the split element. Instead of sticking to the old element, it will be moved to the end of the new element. Closes #1142.
  • Loading branch information
Reinmar authored Sep 15, 2017
2 parents 97a4f4b + 6b6506b commit 1be7ed1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/model/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,16 @@ export default class Range {
return [ new Range( targetPosition.getShiftedBy( offset ) ) ];
}
//
// Edge case for split delta.
//
if ( deltaType == 'split' && this.isCollapsed && this.end.isEqual( sourceRange.end ) ) {
// Collapsed range is at the end of split element.
// Without fix, the range would end up at the end of split (old) element instead of at the end of new element.
// That would happen because this range is not technically inside moved range. Last step below shows the fix.
// <p>foobar[]</p> -> <p>foobar[]</p><p></p> -> <p>foo[]</p><p>bar</p> -> <p>foo</p><p>bar[]</p>
return [ new Range( targetPosition.getShiftedBy( howMany ) ) ];
}
//
// Other edge cases:
//
// In all examples `[]` is `this` and `{}` is `sourceRange`, while `^` is move target position.
Expand Down
13 changes: 13 additions & 0 deletions tests/model/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,19 @@ describe( 'Range', () => {
expect( transformed[ 0 ].start.path ).to.deep.equal( [ 0, 3 ] );
expect( transformed[ 0 ].end.path ).to.deep.equal( [ 1, 2 ] );
} );

it( 'split element which has collapsed range at the end', () => {
range.start = new Position( root, [ 0, 6 ] );
range.end = new Position( root, [ 0, 6 ] );

const delta = getSplitDelta( new Position( root, [ 0, 3 ] ), new Element( 'p' ), 3, 1 );

const transformed = range.getTransformedByDelta( delta );

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

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

0 comments on commit 1be7ed1

Please sign in to comment.