Piotrek Koszuliński 5 年 前
コミット
7215506ff6

+ 6 - 10
packages/ckeditor5-engine/src/model/utils/deletecontent.js

@@ -48,7 +48,10 @@ import DocumentSelection from '../documentselection';
  * For example `<paragraph>x</paragraph>[<image src="foo.jpg"></image>]` will become:
  *
  * * `<paragraph>x</paragraph><paragraph>[]</paragraph>` with the option disabled (`doNotAutoparagraph == false`)
- * * `<paragraph>x[]</paragraph>` with the option enabled (`doNotAutoparagraph == true`).
+ * * `<paragraph>x</paragraph>[]` with the option enabled (`doNotAutoparagraph == true`).
+ *
+ * If you use this option you need to make sure to handle invalid selections yourself or leave
+ * them to the selection post-fixer (may not always work).
  *
  * **Note:** if there is no valid position for the selection, the paragraph will always be created:
  *
@@ -109,16 +112,9 @@ export default function deleteContent( model, selection, options = {} ) {
 
 		// 4. Add a paragraph to set selection in it.
 		// Check if a text is allowed in the new container. If not, try to create a new paragraph (if it's allowed here).
+		// If autoparagraphing is off, we assume that you know what you do so we leave the selection wherever it was.
 		if ( !options.doNotAutoparagraph && shouldAutoparagraph( schema, startPos ) ) {
-			// If auto-paragraphing is off, find the closest valid selection range and collapse the selection there.
-			// If there is no valid selection range, create paragraph anyway and set selection there.
-			const validSelectionRange = schema.getNearestSelectionRange( startPos );
-
-			if ( validSelectionRange ) {
-				collapseSelectionAt( writer, selection, validSelectionRange );
-			} else {
-				insertParagraph( writer, startPos, selection );
-			}
+			insertParagraph( writer, startPos, selection );
 		}
 
 		endPos.detach();

+ 8 - 4
packages/ckeditor5-engine/tests/model/utils/deletecontent.js

@@ -10,6 +10,7 @@ import Selection from '../../../src/model/selection';
 import Element from '../../../src/model/element';
 import deleteContent from '../../../src/model/utils/deletecontent';
 import { setData, getData } from '../../../src/dev-utils/model';
+import { stringify } from '../../../src/dev-utils/view';
 
 describe( 'DataController utils', () => {
 	let model, doc;
@@ -444,7 +445,7 @@ describe( 'DataController utils', () => {
 					'<paragraph><pchild>[]ar</pchild></paragraph><paragraph>x</paragraph>'
 				);
 
-				it( 'merges elements when left end deep nested (3rd level)', () => {
+				it( 'merges elements when right end deep nested (3rd level)', () => {
 					const root = doc.getRoot();
 
 					// We need to use the raw API due to https://github.com/ckeditor/ckeditor5-engine/issues/905.
@@ -615,7 +616,7 @@ describe( 'DataController utils', () => {
 					.to.equal( 'x[]z' );
 			} );
 
-			it( 'creates a paragraph when text is not allowed (custom selection)', () => {
+			it( 'moves the (custom) selection to the nearest paragraph', () => {
 				setData(
 					model,
 					'<paragraph>[x]</paragraph><paragraph>yyy</paragraph><paragraph>z</paragraph>',
@@ -633,6 +634,9 @@ describe( 'DataController utils', () => {
 
 				expect( getData( model, { rootName: 'bodyRoot' } ) )
 					.to.equal( '<paragraph>[x]</paragraph><paragraph></paragraph><paragraph>z</paragraph>' );
+
+				expect( stringify( root, selection ) )
+					.to.equal( '<$root><paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph></$root>' );
 			} );
 
 			it( 'creates a paragraph when text is not allowed (block widget selected)', () => {
@@ -725,7 +729,7 @@ describe( 'DataController utils', () => {
 					.to.equal( '<paragraph>x[]</paragraph><paragraph>z</paragraph>' );
 			} );
 
-			it( 'creates paragraph when after deletion there is no valid selection range', () => {
+			it( 'does not create a paragraph when after deletion there is no valid selection range (empty root)', () => {
 				setData(
 					model,
 					'[<blockWidget></blockWidget>]',
@@ -735,7 +739,7 @@ describe( 'DataController utils', () => {
 				deleteContent( model, doc.selection, { doNotAutoparagraph: true } );
 
 				expect( getData( model, { rootName: 'bodyRoot' } ) )
-					.to.equal( '<paragraph>[]</paragraph>' );
+					.to.equal( '[]' );
 			} );
 		} );
 

+ 2 - 2
packages/ckeditor5-engine/tests/model/utils/insertcontent.js

@@ -1275,11 +1275,11 @@ describe( 'DataController utils', () => {
 			it( 'should not remove empty elements when not-allowed element is paste', () => {
 				setData( model, '<wrapper><limit><paragraph>[]</paragraph></limit></wrapper>' );
 
-				// pasted content is forbidden in current selection
+				// Pasted content is forbidden in current selection.
 				const affectedRange = insertHelper( '<wrapper><limit><paragraph>foo</paragraph></limit></wrapper>' );
 
 				expect( getData( model ) ).to.equal( '<wrapper><limit>[]<paragraph></paragraph></limit></wrapper>' );
-				expect( stringify( root, affectedRange ) ).to.equal( '<wrapper><limit><paragraph>[]</paragraph></limit></wrapper>' );
+				expect( stringify( root, affectedRange ) ).to.equal( '<wrapper><limit>[]<paragraph></paragraph></limit></wrapper>' );
 			} );
 
 			it( 'should correctly paste allowed nodes', () => {

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

@@ -1242,10 +1242,10 @@ describe( 'Selection post-fixer', () => {
 				} );
 
 				expect( getModelData( model ) ).to.equal(
-					'<paragraph>foo[]</paragraph>' +
+					'<paragraph>foo</paragraph>' +
 					'<table>' +
 						'<tableRow>' +
-							'<tableCell><paragraph>aaa</paragraph></tableCell>' +
+							'[<tableCell><paragraph>aaa</paragraph></tableCell>]' +
 							'<tableCell><paragraph>bbb</paragraph></tableCell>' +
 						'</tableRow>' +
 					'</table>' +