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

Other (clipboard): Added extra safety check in case insertContent() doesn't return a range.

Marek Lewandowski 5 лет назад
Родитель
Сommit
15d495fa2e

+ 3 - 1
packages/ckeditor5-clipboard/src/clipboard.js

@@ -114,7 +114,8 @@ export default class Clipboard extends Plugin {
 				model.change( writer => {
 					const insertedRange = model.insertContent( modelFragment );
 
-					if ( isPlainText ) {
+					// The insertedRange needs to be verified, as the return value might easily be discarded like in #7887.
+					if ( isPlainText && insertedRange ) {
 						for ( const item of insertedRange.getItems() ) {
 							writer.setAttributes( initialAttributes, item );
 						}
@@ -176,6 +177,7 @@ export default class Clipboard extends Plugin {
  * It can be modified by the event listeners. Read more about the clipboard pipelines in
  * {@glink framework/guides/deep-dive/clipboard "Clipboard" deep dive}.
  * @param {module:clipboard/datatransfer~DataTransfer} data.dataTransfer Data transfer instance.
+ * @param {Boolean} data.asPlainText If set to `true` content is pasted as plain text.
  */
 
 /**

+ 24 - 1
packages/ckeditor5-clipboard/tests/clipboard.js

@@ -173,7 +173,7 @@ describe( 'Clipboard feature', () => {
 
 		it( 'inserts content to the editor', () => {
 			const dataTransferMock = createDataTransfer( { 'text/html': '<p>x</p>', 'text/plain': 'y' } );
-			const spy = sinon.stub( editor.model, 'insertContent' ).returns( editor.model.document.selection.getFirstRange() );
+			const spy = sinon.stub( editor.model, 'insertContent' );
 
 			viewDocument.fire( 'paste', {
 				dataTransfer: dataTransferMock,
@@ -499,6 +499,29 @@ describe( 'Clipboard feature', () => {
 					'foo<softBreak></softBreak>bar[]' +
 					'<$text bold="true">text.</$text></paragraph>' );
 			} );
+
+			it( 'should work if insertContent event is cancelled', () => {
+				// (#7887).
+				setModelData( model, '<paragraph><$text bold="true">Bolded []text.</$text></paragraph>' );
+
+				const dataTransferMock = createDataTransfer( {
+					'text/html': 'foo',
+					'text/plain': 'foo'
+				} );
+
+				model.on( 'insertContent', event => {
+					event.stop();
+				}, { priority: 'high' } );
+
+				viewDocument.fire( 'clipboardInput', {
+					dataTransfer: dataTransferMock,
+					asPlainText: false,
+					stopPropagation() {},
+					preventDefault() {}
+				} );
+
+				expect( getModelData( model ) ).to.equal( '<paragraph><$text bold="true">Bolded []text.</$text></paragraph>' );
+			} );
 		} );
 
 		function createDataTransfer( data ) {