Browse Source

Added more tests.

Kamil Piechaczek 5 năm trước cách đây
mục cha
commit
3a39285af6

+ 7 - 2
packages/ckeditor5-clipboard/src/clipboard.js

@@ -105,10 +105,15 @@ export default class Clipboard extends Plugin {
 
 				// While pasting plain text, apply selection attributes on the text.
 				if ( isPlainText( modelFragment ) ) {
-					const child = modelFragment.getChild( 0 );
+					let node = modelFragment.getChild( 0 );
+
+					// Pasting a paragraph with text without styles.
+					if ( !node.is( 'text' ) ) {
+						node = node.getChild( 0 );
+					}
 
 					model.change( writer => {
-						writer.setAttributes( modelDocument.selection.getAttributes(), child );
+						writer.setAttributes( modelDocument.selection.getAttributes(), node );
 					} );
 				}
 

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

@@ -350,7 +350,7 @@ describe( 'Clipboard feature', () => {
 			beforeEach( () => {
 				model = editor.model;
 
-				editor.model.schema.extend( '$text', { allowAttributes: 'bold' } );
+				model.schema.extend( '$text', { allowAttributes: 'bold' } );
 			} );
 
 			it( 'should inherit selection attributes (collapsed selection)', () => {
@@ -400,6 +400,57 @@ describe( 'Clipboard feature', () => {
 				expect( getModelData( model ) ).to.equal( '<paragraph><$text bold="true">Bolded foo[]</$text></paragraph>' );
 				expect( insertedNode.getAttribute( 'bold' ) ).to.equal( true );
 			} );
+
+			it( 'should inherit selection attributes while pasting a plain text as text/html', () => {
+				setModelData( model, '<paragraph><$text bold="true">Bolded []text.</$text></paragraph>' );
+
+				const dataTransferMock = createDataTransfer( {
+					'text/html': '<p>foo</p>',
+					'text/plain': 'foo'
+				} );
+
+				viewDocument.fire( 'paste', {
+					dataTransfer: dataTransferMock,
+					stopPropagation() {},
+					preventDefault() {}
+				} );
+
+				expect( getModelData( model ) ).to.equal( '<paragraph><$text bold="true">Bolded foo[]text.</$text></paragraph>' );
+			} );
+
+			it( 'should inherit selection attributes while pasting a plain text as text/html (Chrome style)', () => {
+				setModelData( model, '<paragraph><$text bold="true">Bolded []text.</$text></paragraph>' );
+
+				const dataTransferMock = createDataTransfer( {
+					'text/html': '<meta http-equiv="content-type" content="text/html; charset=utf-8">foo',
+					'text/plain': 'foo'
+				} );
+
+				viewDocument.fire( 'paste', {
+					dataTransfer: dataTransferMock,
+					stopPropagation() {},
+					preventDefault() {}
+				} );
+
+				expect( getModelData( model ) ).to.equal( '<paragraph><$text bold="true">Bolded foo[]text.</$text></paragraph>' );
+			} );
+
+			it( 'should inherit selection attributes while pasting HTML with unsupported attributes', () => {
+				setModelData( model, '<paragraph><$text bold="true">Bolded []text.</$text></paragraph>' );
+
+				const dataTransferMock = createDataTransfer( {
+					'text/html': '<i>foo</i>',
+					'text/plain': 'foo'
+				} );
+
+				viewDocument.fire( 'paste', {
+					dataTransfer: dataTransferMock,
+					stopPropagation() {},
+					preventDefault() {}
+				} );
+
+				expect( getModelData( model ) ).to.equal( '<paragraph><$text bold="true">Bolded foo[]text.</$text></paragraph>' );
+			} );
 		} );
 
 		function createDataTransfer( data ) {