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

Commit 1be7ed1

Browse files
authored
Merge pull request #1143 from ckeditor/t/1142
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.
2 parents 97a4f4b + 6b6506b commit 1be7ed1

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/model/range.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,16 @@ export default class Range {
504504
return [ new Range( targetPosition.getShiftedBy( offset ) ) ];
505505
}
506506
//
507+
// Edge case for split delta.
508+
//
509+
if ( deltaType == 'split' && this.isCollapsed && this.end.isEqual( sourceRange.end ) ) {
510+
// Collapsed range is at the end of split element.
511+
// Without fix, the range would end up at the end of split (old) element instead of at the end of new element.
512+
// That would happen because this range is not technically inside moved range. Last step below shows the fix.
513+
// <p>foobar[]</p> -> <p>foobar[]</p><p></p> -> <p>foo[]</p><p>bar</p> -> <p>foo</p><p>bar[]</p>
514+
return [ new Range( targetPosition.getShiftedBy( howMany ) ) ];
515+
}
516+
//
507517
// Other edge cases:
508518
//
509519
// In all examples `[]` is `this` and `{}` is `sourceRange`, while `^` is move target position.

tests/model/range.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,19 @@ describe( 'Range', () => {
10271027
expect( transformed[ 0 ].start.path ).to.deep.equal( [ 0, 3 ] );
10281028
expect( transformed[ 0 ].end.path ).to.deep.equal( [ 1, 2 ] );
10291029
} );
1030+
1031+
it( 'split element which has collapsed range at the end', () => {
1032+
range.start = new Position( root, [ 0, 6 ] );
1033+
range.end = new Position( root, [ 0, 6 ] );
1034+
1035+
const delta = getSplitDelta( new Position( root, [ 0, 3 ] ), new Element( 'p' ), 3, 1 );
1036+
1037+
const transformed = range.getTransformedByDelta( delta );
1038+
1039+
expect( transformed.length ).to.equal( 1 );
1040+
expect( transformed[ 0 ].start.path ).to.deep.equal( [ 1, 3 ] );
1041+
expect( transformed[ 0 ].end.path ).to.deep.equal( [ 1, 3 ] );
1042+
} );
10301043
} );
10311044

10321045
describe( 'by MergeDelta', () => {

0 commit comments

Comments
 (0)