Procházet zdrojové kódy

operation/transform.js changed ranges sorting.

Szymon Cofalik před 10 roky
rodič
revize
7f6aca3278

+ 7 - 1
packages/ckeditor5-utils/src/document/operation/transform.js

@@ -296,7 +296,13 @@ CKEDITOR.define( [
 				// To keep them in the right order, we need to move them starting from the last one.
 				// [|ABC||DEF||GHI|] ==> [^], [|ABC||DEF|] ==> [^GHI], [|ABC|] ==> [^DEFGHI], [] ==> [ABCDEFGHI]
 				// To achieve this, we sort ranges by their starting position in descending order.
-				ranges.sort( ( a, b ) => a.start.isBefore( b.start ) ? 1 : -1 );
+				ranges.sort( ( r1, r2 ) => {
+					if ( r1.start.root != r2.start.root ) {
+						return r1.start.root == a.sourcePosition.root ? 1 : -1;
+					}
+
+					return r1.start.isBefore( r2.start ) ? 1 : -1;
+				} );
 
 				// Map transformed range(s) to operations and return them.
 				return ranges.map( ( range ) => {

+ 2 - 2
packages/ckeditor5-utils/src/document/position.js

@@ -8,8 +8,8 @@
 CKEDITOR.define( [ 'document/rootelement', 'utils', 'ckeditorerror' ], ( RootElement, utils, CKEditorError ) => {
 	const SAME = 0;
 	const AFTER = 1;
-	const BEFORE = -1;
-	const DIFFERENT = -2;
+	const BEFORE = 2;
+	const DIFFERENT = 3;
 
 	/**
 	 * Position in the tree. Position is always located before or after a node.

+ 6 - 4
packages/ckeditor5-utils/tests/document/operation/transform.js

@@ -1967,9 +1967,12 @@ describe( 'transform', () => {
 			} );
 
 			it( 'range intersects on left with transforming range and is less important: split into two operations', () => {
+				// Get more test cases and better code coverage
+				let otherRoot = new RootElement( null );
+
 				let transformBy = new MoveOperation(
 					new Position( [ 2, 2, 3 ], root ),
-					new Position( [ 4, 1, 0 ], root ),
+					new Position( [ 4, 1, 0 ], otherRoot ),
 					2,
 					baseVersion
 				);
@@ -1978,16 +1981,15 @@ describe( 'transform', () => {
 
 				expect( transOp.length ).to.equal( 2 );
 
-				expected.sourcePosition.path = [ 2, 2, 3 ];
-
 				expectOperation( transOp[ 0 ], {
 					type: MoveOperation,
-					sourcePosition: new Position( [ 4, 1, 1 ], root ),
+					sourcePosition: new Position( [ 4, 1, 1 ], otherRoot ),
 					targetPosition: expected.targetPosition,
 					howMany: 1,
 					baseVersion: expected.baseVersion
 				} );
 
+				expected.sourcePosition.path = [ 2, 2, 3 ];
 				expected.howMany = 1;
 				expected.baseVersion++;