Преглед на файлове

Add tests for TableSelection.getSelectionAsFragment().

Maciej Gołaszewski преди 6 години
родител
ревизия
cd5c8d0a3d
променени са 2 файла, в които са добавени 25 реда и са изтрити 2 реда
  1. 11 2
      packages/ckeditor5-table/src/tableselection.js
  2. 14 0
      packages/ckeditor5-table/tests/tableselection.js

+ 11 - 2
packages/ckeditor5-table/src/tableselection.js

@@ -110,7 +110,7 @@ export default class TableSelection extends Plugin {
 			data.preventDefault();
 			evt.stop();
 
-			const content = editor.data.toView( this.getSelectedTableAsFragment() );
+			const content = editor.data.toView( this.getSelectionAsFragment() );
 
 			editor.editing.view.document.fire( 'clipboardOutput', { dataTransfer, content, method: evt.name } );
 		}, { priority: 'normal' } );
@@ -206,7 +206,16 @@ export default class TableSelection extends Plugin {
 		this._endElement = undefined;
 	}
 
-	getSelectedTableAsFragment() {
+	/**
+	 * Returns selected table fragment as a document fragment.
+	 *
+	 * @returns {module:engine/model/documentfragment~DocumentFragment|undefined}
+	 */
+	getSelectionAsFragment() {
+		if ( !this.hasMultiCellSelection ) {
+			return;
+		}
+
 		return this.editor.model.change( writer => {
 			const fragment = writer.createDocumentFragment();
 

+ 14 - 0
packages/ckeditor5-table/tests/tableselection.js

@@ -12,6 +12,7 @@ import TableSelection from '../src/tableselection';
 import { assertSelectedCells, modelTable, viewTable } from './_utils/utils';
 import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
+import DocumentFragment from '@ckeditor/ckeditor5-engine/src/model/documentfragment';
 
 describe( 'table selection', () => {
 	let editor, model, tableSelection, modelRoot;
@@ -298,6 +299,19 @@ describe( 'table selection', () => {
 			} );
 		} );
 
+		describe( 'getSelectionAsFragment()', () => {
+			it( 'should return undefined if no table cells are selected', () => {
+				expect( tableSelection.getSelectionAsFragment() ).to.be.undefined;
+			} );
+
+			it( 'should return document fragment for selected table cells', () => {
+				tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
+				tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
+
+				expect( tableSelection.getSelectionAsFragment() ).to.be.instanceOf( DocumentFragment );
+			} );
+		} );
+
 		describe( 'behavior', () => {
 			it( 'should clear selection on external changes', () => {
 				tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );