8
0
Pārlūkot izejas kodu

Fix range de-intersecting when one of the ranges is bigger then others.

Maciej Gołaszewski 6 gadi atpakaļ
vecāks
revīzija
00a3863971

+ 5 - 1
packages/ckeditor5-engine/src/model/utils/selection-post-fixer.js

@@ -263,7 +263,11 @@ function mergeIntersectingRanges( ranges ) {
 		const previousRange = nonIntersectingRanges.pop();
 
 		if ( range.isIntersecting( previousRange ) ) {
-			const merged = new Range( previousRange.start, range.end );
+			// Get the sum of two ranges.
+			const start = previousRange.start.isAfter( range.start ) ? range.start : previousRange.start;
+			const end = previousRange.end.isAfter( range.end ) ? previousRange.end : range.end;
+
+			const merged = new Range( start, end );
 			nonIntersectingRanges.push( merged );
 		} else {
 			nonIntersectingRanges.push( previousRange );

+ 45 - 1
packages/ckeditor5-engine/tests/model/utils/selection-post-fixer.js

@@ -10,7 +10,7 @@ import { injectSelectionPostFixer } from '../../../src/model/utils/selection-pos
 import { getData as getModelData, setData as setModelData } from '../../../src/dev-utils/model';
 import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 
-describe.only( 'Selection post-fixer', () => {
+describe( 'Selection post-fixer', () => {
 	describe( 'injectSelectionPostFixer()', () => {
 		it( 'is a function', () => {
 			expect( injectSelectionPostFixer ).to.be.a( 'function' );
@@ -707,6 +707,50 @@ describe.only( 'Selection post-fixer', () => {
 					'</table>'
 				);
 			} );
+
+			it( 'should fix partially wrong selection (last range is post-fixed on whole table)', () => {
+				setModelData( model,
+					'<table>' +
+						'<tableRow>' +
+							'[<tableCell><paragraph>aaa</paragraph></tableCell>]' +
+							'<tableCell><paragraph>bbb</paragraph></tableCell>' +
+							'[<tableCell><paragraph>ccc</paragraph></tableCell>' +
+						'</tableRow>]' +
+					'</table>'
+				);
+
+				assertEqualMarkup( getModelData( model ),
+					'[<table>' +
+						'<tableRow>' +
+							'<tableCell><paragraph>aaa</paragraph></tableCell>' +
+							'<tableCell><paragraph>bbb</paragraph></tableCell>' +
+							'<tableCell><paragraph>ccc</paragraph></tableCell>' +
+						'</tableRow>' +
+					'</table>]'
+				);
+			} );
+
+			it( 'should fix partially wrong selection (first range is post-fixed on whole table)', () => {
+				setModelData( model,
+					'<table>' +
+						'[<tableRow>' +
+							'<tableCell><paragraph>aaa</paragraph></tableCell>]' +
+							'<tableCell><paragraph>bbb</paragraph></tableCell>' +
+							'[<tableCell><paragraph>ccc</paragraph></tableCell>]' +
+						'</tableRow>' +
+					'</table>'
+				);
+
+				assertEqualMarkup( getModelData( model ),
+					'[<table>' +
+						'<tableRow>' +
+							'<tableCell><paragraph>aaa</paragraph></tableCell>' +
+							'<tableCell><paragraph>bbb</paragraph></tableCell>' +
+							'<tableCell><paragraph>ccc</paragraph></tableCell>' +
+						'</tableRow>' +
+					'</table>]'
+				);
+			} );
 		} );
 
 		describe( 'non-collapsed selection - image scenarios', () => {