8
0
Просмотр исходного кода

Merge pull request #1820 from ckeditor/i/6154

Other: Allow selection on object elements. Closes ckeditor/ckeditor5#6154.
Piotrek Koszuliński 6 лет назад
Родитель
Сommit
8cfcb11b6a

+ 34 - 20
packages/ckeditor5-engine/src/model/utils/selection-post-fixer.js

@@ -92,20 +92,7 @@ function selectionPostFixer( writer, model ) {
 
 	// If any of ranges were corrected update the selection.
 	if ( wasFixed ) {
-		// The above algorithm might create ranges that intersects each other when selection contains more then one range.
-		// This is case happens mostly on Firefox which creates multiple ranges for selected table.
-		let fixedRanges = ranges;
-
-		// Fixing selection with many ranges usually breaks the selection in Firefox. As only Firefox supports multiple selection ranges
-		// we simply create one continuous range from fixed selection ranges (even if they are not adjacent).
-		if ( ranges.length > 1 ) {
-			const selectionStart = ranges[ 0 ].start;
-			const selectionEnd = ranges[ ranges.length - 1 ].end;
-
-			fixedRanges = [ new Range( selectionStart, selectionEnd ) ];
-		}
-
-		writer.setSelection( fixedRanges, { backward: selection.isBackward } );
+		writer.setSelection( mergeIntersectingRanges( ranges ), { backward: selection.isBackward } );
 	}
 }
 
@@ -140,6 +127,10 @@ function tryFixingCollapsedRange( range, schema ) {
 		return null;
 	}
 
+	if ( !nearestSelectionRange.isCollapsed ) {
+		return nearestSelectionRange;
+	}
+
 	const fixedPosition = nearestSelectionRange.start;
 
 	// Fixed position is the same as original - no need to return corrected range.
@@ -147,11 +138,6 @@ function tryFixingCollapsedRange( range, schema ) {
 		return null;
 	}
 
-	// Check single node selection (happens in tables).
-	if ( fixedPosition.nodeAfter && schema.isLimit( fixedPosition.nodeAfter ) ) {
-		return new Range( fixedPosition, Position._createAfter( fixedPosition.nodeAfter ) );
-	}
-
 	return new Range( fixedPosition );
 }
 
@@ -263,6 +249,35 @@ function checkSelectionOnNonLimitElements( start, end, schema ) {
 	return startIsOnBlock || endIsOnBlock;
 }
 
+// Returns a minimal non-intersecting array of ranges.
+//
+// @param {Array.<module:engine/model/range~Range>} ranges
+// @returns {Array.<module:engine/model/range~Range>}
+function mergeIntersectingRanges( ranges ) {
+	const nonIntersectingRanges = [];
+
+	// First range will always be fine.
+	nonIntersectingRanges.push( ranges.shift() );
+
+	for ( const range of ranges ) {
+		const previousRange = nonIntersectingRanges.pop();
+
+		if ( range.isIntersecting( previousRange ) ) {
+			// 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 );
+			nonIntersectingRanges.push( range );
+		}
+	}
+
+	return nonIntersectingRanges;
+}
+
 // Checks if node exists and if it's an object.
 //
 // @param {module:engine/model/node~Node} node
@@ -271,4 +286,3 @@ function checkSelectionOnNonLimitElements( start, end, schema ) {
 function isInObject( node, schema ) {
 	return node && schema.isObject( node );
 }
-

+ 288 - 34
packages/ckeditor5-engine/tests/model/utils/selection-post-fixer.js

@@ -8,6 +8,7 @@ import Model from '../../../src/model/model';
 import { injectSelectionPostFixer } from '../../../src/model/utils/selection-post-fixer';
 
 import { getData as getModelData, setData as setModelData } from '../../../src/dev-utils/model';
+import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 
 describe( 'Selection post-fixer', () => {
 	describe( 'injectSelectionPostFixer()', () => {
@@ -40,9 +41,11 @@ describe( 'Selection post-fixer', () => {
 			model.schema.register( 'tableCell', {
 				allowIn: 'tableRow',
 				allowAttributes: [ 'colspan', 'rowspan' ],
-				isLimit: true
+				isObject: true
 			} );
 
+			model.schema.extend( '$block', { allowIn: 'tableCell' } );
+
 			model.schema.register( 'image', {
 				isObject: true,
 				isBlock: true,
@@ -97,7 +100,7 @@ describe( 'Selection post-fixer', () => {
 			expect( getModelData( model ) ).to.equal( '<paragraph>foo[]</paragraph><image></image>' );
 		} );
 
-		describe( 'non-collapsed selection - table scenarios', () => {
+		describe( 'selection - table scenarios', () => {
 			beforeEach( () => {
 				setModelData( model,
 					'<paragraph>[]foo</paragraph>' +
@@ -111,7 +114,7 @@ describe( 'Selection post-fixer', () => {
 				);
 			} );
 
-			it( 'should fix #1 - range start outside table, end on table cell', () => {
+			it( 'should fix #1 (range start outside table, end on table cell)', () => {
 				// <paragraph>f[oo</paragraph><table><tableRow><tableCell></tableCell>]<tableCell>...
 				model.change( writer => {
 					writer.setSelection( writer.createRange(
@@ -132,7 +135,7 @@ describe( 'Selection post-fixer', () => {
 				);
 			} );
 
-			it( 'should fix #2 - range start on table cell, end outside table', () => {
+			it( 'should fix #2 (range start on table cell, end outside table)', () => {
 				// ...<table><tableRow><tableCell></tableCell>[<tableCell></tableCell></tableRow></table><paragraph>b]ar</paragraph>
 				model.change( writer => {
 					writer.setSelection( writer.createRange(
@@ -195,7 +198,7 @@ describe( 'Selection post-fixer', () => {
 				);
 			} );
 
-			it( 'should fix #5', () => {
+			it( 'should fix #5 (collapsed selection between tables)', () => {
 				setModelData( model,
 					'<paragraph>foo</paragraph>' +
 					'<table>' +
@@ -214,7 +217,7 @@ describe( 'Selection post-fixer', () => {
 					'<paragraph>baz</paragraph>'
 				);
 
-				expect( getModelData( model ) ).to.equal(
+				assertEqualMarkup( getModelData( model ),
 					'<paragraph>foo</paragraph>' +
 					'[<table>' +
 						'<tableRow>' +
@@ -325,7 +328,7 @@ describe( 'Selection post-fixer', () => {
 				);
 			} );
 
-			it( 'should not fix #1 - selection over paragraphs outside table', () => {
+			it( 'should not fix #1 (selection over paragraphs outside table)', () => {
 				setModelData( model,
 					'<paragraph>foo</paragraph>' +
 					'<table>' +
@@ -351,7 +354,7 @@ describe( 'Selection post-fixer', () => {
 				);
 			} );
 
-			it( 'should not fix #2 - selection over image in table', () => {
+			it( 'should not fix #2 (selection over image in table)', () => {
 				setModelData( model,
 					'<paragraph>foo</paragraph>' +
 					'<table>' +
@@ -379,7 +382,7 @@ describe( 'Selection post-fixer', () => {
 				);
 			} );
 
-			it( 'should not fix #3 - selection over paragraph & image in table', () => {
+			it( 'should not fix #3 (selection over paragraph & image in table)', () => {
 				setModelData( model,
 					'<paragraph>foo</paragraph>' +
 					'<table>' +
@@ -407,7 +410,7 @@ describe( 'Selection post-fixer', () => {
 				);
 			} );
 
-			it( 'should not fix #4 - selection over image & paragraph in table', () => {
+			it( 'should not fix #4 (selection over image & paragraph in table)', () => {
 				setModelData( model,
 					'<paragraph>foo</paragraph>' +
 					'<table>' +
@@ -435,7 +438,7 @@ describe( 'Selection post-fixer', () => {
 				);
 			} );
 
-			it( 'should not fix #5 - selection over blockQuote in table', () => {
+			it( 'should not fix #5 (selection over blockQuote in table)', () => {
 				model.schema.register( 'blockQuote', {
 					allowWhere: '$block',
 					allowContentOf: '$root'
@@ -571,7 +574,7 @@ describe( 'Selection post-fixer', () => {
 				);
 			} );
 
-			it( 'should fix multiple ranges #4', () => {
+			it( 'should not fix multiple ranges #1 (not overlapping ranges)', () => {
 				model.change( writer => {
 					const ranges = [
 						writer.createRange(
@@ -594,12 +597,242 @@ describe( 'Selection post-fixer', () => {
 				expect( getModelData( model ) ).to.equal(
 					'<paragraph>f[oo</paragraph>' +
 					'<table>' +
+					'<tableRow>' +
+					'<tableCell><paragraph>aaa</paragraph></tableCell>' +
+					'<tableCell><paragraph>bbb</paragraph></tableCell>' +
+					'</tableRow>' +
+					'</table>' +
+					'<paragraph>b]a[r]</paragraph>'
+				);
+			} );
+
+			it( 'should not fix multiple ranges on objects (table selection)', () => {
+				setModelData( model,
+					'<table>' +
+						'<tableRow>' +
+							'[<tableCell><paragraph>a</paragraph></tableCell>]' +
+							'[<tableCell><paragraph>b</paragraph></tableCell>]' +
+						'</tableRow>' +
+						'<tableRow>' +
+							'[<tableCell><paragraph>c</paragraph></tableCell>]' +
+							'<tableCell><paragraph>d</paragraph></tableCell>' +
+						'</tableRow>' +
+					'</table>'
+				);
+
+				assertEqualMarkup( getModelData( model ),
+					'<table>' +
+					'<tableRow>' +
+							'[<tableCell><paragraph>a</paragraph></tableCell>]' +
+							'[<tableCell><paragraph>b</paragraph></tableCell>]' +
+						'</tableRow>' +
+						'<tableRow>' +
+							'[<tableCell><paragraph>c</paragraph></tableCell>]' +
+							'<tableCell><paragraph>d</paragraph></tableCell>' +
+					'</tableRow>' +
+					'</table>'
+				);
+			} );
+
+			it( 'should fix selection on block', () => {
+				model.schema.extend( '$block', { allowIn: 'tableCell' } );
+
+				setModelData( model,
+					'<table>' +
+					'<tableRow><tableCell>[<paragraph>aaa</paragraph>]</tableCell></tableRow>' +
+					'</table>'
+				);
+
+				assertEqualMarkup( getModelData( model ),
+					'<table>' +
+						'<tableRow><tableCell><paragraph>[aaa]</paragraph></tableCell></tableRow>' +
+					'</table>'
+				);
+			} );
+
+			it( 'should allow multi-range selection on non-continues blocks (column selected)', () => {
+				setModelData( model,
+					'<table>' +
+						'<tableRow>' +
+							'[<tableCell><paragraph>A1</paragraph></tableCell>]' +
+							'<tableCell><paragraph>B1</paragraph></tableCell>' +
+						'</tableRow>' +
+						'<tableRow>' +
+							'[<tableCell><paragraph>A2</paragraph></tableCell>]' +
+							'<tableCell><paragraph>B2</paragraph></tableCell>' +
+						'</tableRow>' +
+						'<tableRow>' +
+							'[<tableCell><paragraph>A3</paragraph></tableCell>]' +
+							'<tableCell><paragraph>B3</paragraph></tableCell>' +
+						'</tableRow>' +
+					'</table>'
+				);
+
+				assertEqualMarkup( getModelData( model ),
+					'<table>' +
+						'<tableRow>' +
+							'[<tableCell><paragraph>A1</paragraph></tableCell>]' +
+							'<tableCell><paragraph>B1</paragraph></tableCell>' +
+						'</tableRow>' +
+						'<tableRow>' +
+							'[<tableCell><paragraph>A2</paragraph></tableCell>]' +
+							'<tableCell><paragraph>B2</paragraph></tableCell>' +
+						'</tableRow>' +
+						'<tableRow>' +
+							'[<tableCell><paragraph>A3</paragraph></tableCell>]' +
+							'<tableCell><paragraph>B3</paragraph></tableCell>' +
+						'</tableRow>' +
+					'</table>'
+				);
+			} );
+
+			it( 'should allow multi-range selection on continues blocks (row selected)', () => {
+				setModelData( model,
+					'<table>' +
+						'<tableRow>' +
+							'<tableCell><paragraph>A1</paragraph></tableCell>' +
+							'<tableCell><paragraph>B1</paragraph></tableCell>' +
+							'<tableCell><paragraph>C1</paragraph></tableCell>' +
+						'</tableRow>' +
+						'<tableRow>' +
+							'[<tableCell><paragraph>A2</paragraph></tableCell>]' +
+							'[<tableCell><paragraph>B2</paragraph></tableCell>]' +
+							'[<tableCell><paragraph>C2</paragraph></tableCell>]' +
+						'</tableRow>' +
+						'<tableRow>' +
+							'<tableCell><paragraph>A3</paragraph></tableCell>' +
+							'<tableCell><paragraph>B3</paragraph></tableCell>' +
+							'<tableCell><paragraph>C3</paragraph></tableCell>' +
+						'</tableRow>' +
+					'</table>'
+				);
+
+				assertEqualMarkup( getModelData( model ),
+					'<table>' +
+						'<tableRow>' +
+							'<tableCell><paragraph>A1</paragraph></tableCell>' +
+							'<tableCell><paragraph>B1</paragraph></tableCell>' +
+							'<tableCell><paragraph>C1</paragraph></tableCell>' +
+						'</tableRow>' +
+						'<tableRow>' +
+							'[<tableCell><paragraph>A2</paragraph></tableCell>]' +
+							'[<tableCell><paragraph>B2</paragraph></tableCell>]' +
+							'[<tableCell><paragraph>C2</paragraph></tableCell>]' +
+						'</tableRow>' +
+						'<tableRow>' +
+							'<tableCell><paragraph>A3</paragraph></tableCell>' +
+							'<tableCell><paragraph>B3</paragraph></tableCell>' +
+							'<tableCell><paragraph>C3</paragraph></tableCell>' +
+						'</tableRow>' +
+					'</table>'
+				);
+			} );
+
+			it( 'should allow multi-range selection with mixed continues/non-continues blocks (part of table selected)', () => {
+				setModelData( model,
+					'<table>' +
+						'<tableRow>' +
+							'<tableCell><paragraph>A1</paragraph></tableCell>' +
+							'<tableCell><paragraph>B1</paragraph></tableCell>' +
+							'<tableCell><paragraph>C1</paragraph></tableCell>' +
+						'</tableRow>' +
+						'<tableRow>' +
+							'<tableCell><paragraph>A2</paragraph></tableCell>' +
+							'[<tableCell><paragraph>B2</paragraph></tableCell>]' +
+							'[<tableCell><paragraph>C2</paragraph></tableCell>]' +
+						'</tableRow>' +
+						'<tableRow>' +
+							'<tableCell><paragraph>A3</paragraph></tableCell>' +
+							'[<tableCell><paragraph>B3</paragraph></tableCell>]' +
+							'[<tableCell><paragraph>C3</paragraph></tableCell>]' +
+						'</tableRow>' +
+					'</table>'
+				);
+
+				assertEqualMarkup( getModelData( model ),
+					'<table>' +
+						'<tableRow>' +
+							'<tableCell><paragraph>A1</paragraph></tableCell>' +
+							'<tableCell><paragraph>B1</paragraph></tableCell>' +
+							'<tableCell><paragraph>C1</paragraph></tableCell>' +
+						'</tableRow>' +
+						'<tableRow>' +
+							'<tableCell><paragraph>A2</paragraph></tableCell>' +
+							'[<tableCell><paragraph>B2</paragraph></tableCell>]' +
+							'[<tableCell><paragraph>C2</paragraph></tableCell>]' +
+							'</tableRow>' +
+						'<tableRow>' +
+							'<tableCell><paragraph>A3</paragraph></tableCell>' +
+							'[<tableCell><paragraph>B3</paragraph></tableCell>]' +
+							'[<tableCell><paragraph>C3</paragraph></tableCell>]' +
+						'</tableRow>' +
+					'</table>'
+				);
+			} );
+
+			it( 'should not fix ranges in multi-range selection (each range set differently - but valid)', () => {
+				setModelData( model,
+					'<paragraph>[foo]</paragraph>' +
+						'<table>' +
+						'<tableRow>' +
+							'[<tableCell><paragraph>aaa</paragraph></tableCell>]' +
+							'<tableCell><paragraph>[bbb]</paragraph></tableCell>' +
+						'</tableRow>' +
+					'</table>'
+				);
+
+				assertEqualMarkup( getModelData( model ),
+					'<paragraph>[foo]</paragraph>' +
+					'<table>' +
+						'<tableRow>' +
+							'[<tableCell><paragraph>aaa</paragraph></tableCell>]' +
+							'<tableCell><paragraph>[bbb]</paragraph></tableCell>' +
+						'</tableRow>' +
+					'</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>' +
-					'<paragraph>bar]</paragraph>'
+					'</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>]'
 				);
 			} );
 		} );
@@ -907,7 +1140,7 @@ describe( 'Selection post-fixer', () => {
 				expect( getModelData( model ) ).to.equal( '<paragraph>foob[a]r</paragraph>' );
 			} );
 
-			it( 'should not fix #2', () => {
+			it( 'should not fix #2 (inline widget selected)', () => {
 				setModelData( model,
 					'<paragraph>[<inlineWidget></inlineWidget>]</paragraph>'
 				);
@@ -917,7 +1150,7 @@ describe( 'Selection post-fixer', () => {
 				);
 			} );
 
-			it( 'should not fix #3', () => {
+			it( 'should not fix #3 (text around inline widget)', () => {
 				setModelData( model,
 					'<paragraph>fo[o<inlineWidget></inlineWidget>b]ar</paragraph>'
 				);
@@ -927,7 +1160,7 @@ describe( 'Selection post-fixer', () => {
 				);
 			} );
 
-			it( 'should not fix #4 - object in object', () => {
+			it( 'should not fix #4 (object in object)', () => {
 				model.schema.register( 'div', {
 					allowIn: [ '$root', 'div' ],
 					isObject: true
@@ -989,7 +1222,7 @@ describe( 'Selection post-fixer', () => {
 		describe( 'collapsed selection', () => {
 			beforeEach( () => {
 				setModelData( model,
-					'<paragraph>[]foo</paragraph>' +
+					'<paragraph>foo</paragraph>' +
 					'<table>' +
 						'<tableRow>' +
 							'<tableCell><paragraph>aaa</paragraph></tableCell>' +
@@ -1000,7 +1233,7 @@ describe( 'Selection post-fixer', () => {
 				);
 			} );
 
-			it( 'should fix #1', () => {
+			it( 'should fix #1 (selection in limit element & before limit element)', () => {
 				// <table>[]<tableRow>...
 				model.change( writer => {
 					writer.setSelection(
@@ -1020,7 +1253,7 @@ describe( 'Selection post-fixer', () => {
 				);
 			} );
 
-			it( 'should fix #2', () => {
+			it( 'should fix #2 (selection in limit element & before limit+object element)', () => {
 				// <table><tableRow>[]<tableCell>...
 				model.change( writer => {
 					const row = modelRoot.getChild( 1 ).getChild( 0 );
@@ -1034,7 +1267,7 @@ describe( 'Selection post-fixer', () => {
 					'<paragraph>foo</paragraph>' +
 					'<table>' +
 						'<tableRow>' +
-							'<tableCell><paragraph>[]aaa</paragraph></tableCell>' +
+							'[<tableCell><paragraph>aaa</paragraph></tableCell>]' +
 							'<tableCell><paragraph>bbb</paragraph></tableCell>' +
 						'</tableRow>' +
 					'</table>' +
@@ -1042,23 +1275,44 @@ describe( 'Selection post-fixer', () => {
 				);
 			} );
 
-			it( 'should fix multiple ranges #1', () => {
-				// []<paragraph>foo</paragraph>[]<table>...
-				model.change( writer => {
-					writer.setSelection(
-						[
-							writer.createRange( writer.createPositionAt( modelRoot, 0 ) ),
-							writer.createRange( writer.createPositionAt( modelRoot, 1 ) )
-						]
-					);
-				} );
+			it( 'should fix #3 (selection inside object element and before block element)', () => {
+				setModelData( model,
+					'<paragraph>foo</paragraph>' +
+					'<table>' +
+						'<tableRow>' +
+							'<tableCell>[]<paragraph>aaa</paragraph></tableCell>' +
+						'</tableRow>' +
+					'</table>' +
+					'<paragraph>bar</paragraph>'
+				);
 
-				expect( getModelData( model ) ).to.equal(
-					'<paragraph>[foo]</paragraph>' +
+				assertEqualMarkup( getModelData( model ),
+					'<paragraph>foo</paragraph>' +
+					'<table>' +
+						'<tableRow>' +
+							'<tableCell><paragraph>[]aaa</paragraph></tableCell>' +
+						'</tableRow>' +
+					'</table>' +
+					'<paragraph>bar</paragraph>'
+				);
+			} );
+
+			it( 'should fix multiple ranges outside block element (but not merge them)', () => {
+				setModelData( model,
+					'[]<paragraph>foo</paragraph>[]' +
+					'<table>' +
+						'<tableRow>' +
+							'<tableCell><paragraph>aaa</paragraph></tableCell>' +
+						'</tableRow>' +
+					'</table>' +
+					'<paragraph>bar</paragraph>'
+				);
+
+				assertEqualMarkup( getModelData( model ),
+					'<paragraph>[]foo[]</paragraph>' +
 					'<table>' +
 						'<tableRow>' +
 							'<tableCell><paragraph>aaa</paragraph></tableCell>' +
-							'<tableCell><paragraph>bbb</paragraph></tableCell>' +
 						'</tableRow>' +
 					'</table>' +
 					'<paragraph>bar</paragraph>'