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

Commit 5f020b0

Browse files
authored
Merge pull request #1160 from ckeditor/t/1159
Fix: `model.Range` will now be extended if it was collapsed and it was transformed by insertion. Closes #1159.
2 parents f4c104d + 4d6007a commit 5f020b0

File tree

5 files changed

+29
-5
lines changed

5 files changed

+29
-5
lines changed

src/dev-utils/enableenginedebug.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,10 @@ function enableLoggingTools() {
382382

383383
MergeDelta.prototype.toString = function() {
384384
return getClassName( this ) + `( ${ this.baseVersion } ): ` +
385-
this.position.toString();
385+
( this.position ?
386+
this.position.toString() :
387+
`(move from ${ this.operations[ 0 ].sourcePosition })`
388+
);
386389
};
387390

388391
MoveDelta.prototype.toString = function() {

src/model/operation/transform.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,9 @@ const ot = {
342342
InsertOperation( a, b, context ) {
343343
// Create range from MoveOperation properties and transform it by insertion.
344344
let range = Range.createFromPositionAndShift( a.sourcePosition, a.howMany );
345-
range = range._getTransformedByInsertion( b.position, b.nodes.maxOffset, false, a.isSticky && !context.forceNotSticky )[ 0 ];
345+
const includeB = a.isSticky && !context.forceNotSticky;
346+
347+
range = range._getTransformedByInsertion( b.position, b.nodes.maxOffset, false, includeB )[ 0 ];
346348

347349
// Check whether there is a forced order of nodes or use `context.isStrong` flag for conflict resolving.
348350
const insertBefore = context.insertBefore === undefined ? !context.isStrong : context.insertBefore;

src/model/range.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ export default class Range {
615615
} else {
616616
const range = Range.createFromRange( this );
617617

618-
const insertBeforeStart = range.isCollapsed ? true : !isSticky;
618+
const insertBeforeStart = !isSticky;
619619
const insertBeforeEnd = range.isCollapsed ? true : isSticky;
620620

621621
range.start = range.start._getTransformedByInsertion( insertPosition, howMany, insertBeforeStart );

tests/dev-utils/enableenginedebug.js

+19
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,25 @@ describe( 'debug tools', () => {
397397
expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
398398
} );
399399

400+
it( 'MergeDelta - NoOperation as second operation', () => {
401+
const otherRoot = modelDoc.createRoot( '$root', 'otherRoot' );
402+
const firstEle = new ModelElement( 'paragraph' );
403+
const removedEle = new ModelElement( 'paragraph', null, [ new ModelText( 'foo' ) ] );
404+
405+
otherRoot.appendChildren( [ firstEle, removedEle ] );
406+
407+
const delta = new MergeDelta();
408+
const move = new MoveOperation( ModelPosition.createAt( removedEle, 0 ), 3, ModelPosition.createAt( firstEle, 0 ), 0 );
409+
410+
delta.addOperation( move );
411+
delta.addOperation( new NoOperation( 1 ) );
412+
413+
expect( delta.toString() ).to.equal( 'MergeDelta( 0 ): (move from otherRoot [ 1, 0 ])' );
414+
415+
delta.log();
416+
expect( log.calledWithExactly( delta.toString() ) ).to.be.true;
417+
} );
418+
400419
it( 'MoveDelta', () => {
401420
const delta = new MoveDelta();
402421
const move1 = new MoveOperation( ModelPosition.createAt( modelRoot, 0 ), 1, ModelPosition.createAt( modelRoot, 3 ), 0 );

tests/model/range.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -572,11 +572,11 @@ describe( 'Range', () => {
572572
expect( transformed[ 0 ].end.path ).to.deep.equal( [ 4, 4 ] );
573573
} );
574574

575-
it( 'should move after inserted nodes if the range is collapsed and isSticky is true', () => {
575+
it( 'should expand range after inserted nodes if the range is collapsed and isSticky is true', () => {
576576
const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 2 ] ) );
577577
const transformed = range._getTransformedByInsertion( new Position( root, [ 3, 2 ] ), 3, false, true );
578578

579-
expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 5 ] );
579+
expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
580580
expect( transformed[ 0 ].end.path ).to.deep.equal( [ 3, 5 ] );
581581
} );
582582
} );

0 commit comments

Comments
 (0)