Explorar el Código

Tests: Add basic converter tests.

Maciej Gołaszewski hace 7 años
padre
commit
ee77207583

+ 6 - 27
packages/ckeditor5-table/src/converters.js

@@ -9,21 +9,8 @@
 
 import Position from '@ckeditor/ckeditor5-engine/src/view/position';
 
-export function createTableCell( viewElement, modelWriter ) {
-	const attributes = {};
-
-	if ( viewElement.name === 'th' ) {
-		attributes.isHeading = true;
-	}
-
-	return modelWriter.createElement( 'tableCell', attributes );
-}
-
 export function createTable( viewElement, modelWriter ) {
-	const attributes = {
-		headingRows: 0,
-		headingColumns: 0
-	};
+	const attributes = {};
 
 	const header = _getChildHeader( viewElement );
 
@@ -36,17 +23,13 @@ export function createTable( viewElement, modelWriter ) {
 
 export function downcastTableCell( dispatcher ) {
 	dispatcher.on( 'insert:tableCell', ( evt, data, conversionApi ) => {
-		const viewElementName = data.item.getAttribute( 'isHeading' ) ? 'th' : 'td';
-		const tableCellElement = conversionApi.writer.createContainerElement( viewElementName, {} );
-
-		if ( !tableCellElement ) {
-			return;
-		}
-
 		if ( !conversionApi.consumable.consume( data.item, 'insert' ) ) {
 			return;
 		}
 
+		const viewElementName = data.item.getAttribute( 'isHeading' ) ? 'th' : 'td';
+		const tableCellElement = conversionApi.writer.createContainerElement( viewElementName, {} );
+
 		const viewPosition = conversionApi.mapper.toViewPosition( data.range.start );
 
 		conversionApi.mapper.bindElements( data.item, tableCellElement );
@@ -56,18 +39,14 @@ export function downcastTableCell( dispatcher ) {
 
 export function downcastTable( dispatcher ) {
 	dispatcher.on( 'insert:table', ( evt, data, conversionApi ) => {
-		const tableElement = conversionApi.writer.createContainerElement( 'table' );
-
-		if ( !tableElement ) {
-			return;
-		}
-
 		const table = data.item;
 
 		if ( !conversionApi.consumable.consume( table, 'insert' ) ) {
 			return;
 		}
 
+		const tableElement = conversionApi.writer.createContainerElement( 'table' );
+
 		const headingRows = table.getAttribute( 'headingRows' );
 
 		const tableRows = [ ...table.getChildren() ];

+ 3 - 3
packages/ckeditor5-table/src/tableediting.js

@@ -9,7 +9,7 @@
 
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
-import { createTableCell, createTable, downcastTableCell, downcastTable } from './converters';
+import { createTable, downcastTableCell, downcastTable } from './converters';
 import InsertTableCommand from './inserttablecommand';
 
 export default class TablesEditing extends Plugin {
@@ -51,8 +51,8 @@ export default class TablesEditing extends Plugin {
 		conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableRow', view: 'tr' } ) );
 
 		// Table cell conversion.
-		conversion.for( 'upcast' ).add( upcastElementToElement( { model: createTableCell, view: 'td' } ) );
-		conversion.for( 'upcast' ).add( upcastElementToElement( { model: createTableCell, view: 'th' } ) );
+		conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'td' } ) );
+		conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'th' } ) );
 		conversion.for( 'downcast' ).add( downcastTableCell );
 
 		conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );

+ 209 - 0
packages/ckeditor5-table/tests/converters.js

@@ -0,0 +1,209 @@
+/**
+ * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
+
+import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
+
+import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
+import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
+import { createTable, downcastTable, downcastTableCell } from '../src/converters';
+
+describe( 'Table converters', () => {
+	let editor, model, viewDocument;
+
+	beforeEach( () => {
+		return VirtualTestEditor.create()
+			.then( newEditor => {
+				editor = newEditor;
+				model = editor.model;
+				viewDocument = editor.editing.view;
+
+				const conversion = editor.conversion;
+				const schema = model.schema;
+
+				schema.register( 'table', {
+					allowWhere: '$block',
+					allowAttributes: [ 'headingRows' ],
+					isBlock: true,
+					isObject: true
+				} );
+
+				schema.register( 'tableRow', {
+					allowIn: 'table',
+					allowAttributes: [],
+					isBlock: true,
+					isLimit: true
+				} );
+
+				schema.register( 'tableCell', {
+					allowIn: 'tableRow',
+					allowContentOf: '$block',
+					allowAttributes: [ 'colspan', 'rowspan' ],
+					isBlock: true,
+					isLimit: true
+				} );
+
+				conversion.for( 'upcast' ).add( upcastElementToElement( { model: createTable, view: 'table' } ) );
+				conversion.for( 'downcast' ).add( downcastTable );
+
+				// Table row upcast only since downcast conversion is done in `downcastTable()`.
+				conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableRow', view: 'tr' } ) );
+
+				// Table cell conversion.
+				conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'td' } ) );
+				conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'th' } ) );
+				conversion.for( 'downcast' ).add( downcastTableCell );
+			} );
+	} );
+
+	describe( 'createTable', () => {
+		function expectModel( data ) {
+			expect( getModelData( model, { withoutSelection: true } ) ).to.equal( data );
+		}
+
+		beforeEach( () => {
+			// Since this part of test tests only view->model conversion editing pipeline is not necessary
+			// so defining model->view converters won't be necessary.
+			editor.editing.destroy();
+		} );
+
+		it( 'should create table model from table without thead', () => {
+			editor.setData(
+				'<table>' +
+				'<tr><td>1</td></tr>' +
+				'</table>'
+			);
+
+			expectModel(
+				'<table>' +
+				'<tableRow><tableCell>1</tableCell></tableRow>' +
+				'</table>'
+			);
+		} );
+
+		it( 'should create table model from table with one thead with one row', () => {
+			editor.setData(
+				'<table>' +
+				'<thead><tr><td>1</td></tr></thead>' +
+				'</table>'
+			);
+
+			expectModel(
+				'<table headingRows="1">' +
+				'<tableRow><tableCell>1</tableCell></tableRow>' +
+				'</table>'
+			);
+		} );
+
+		it( 'should create table model from table with one thead with more then on row', () => {
+			editor.setData(
+				'<table>' +
+				'<thead>' +
+				'<tr><td>1</td></tr>' +
+				'<tr><td>2</td></tr>' +
+				'<tr><td>3</td></tr>' +
+				'</thead>' +
+				'</table>'
+			);
+
+			expectModel(
+				'<table headingRows="3">' +
+				'<tableRow><tableCell>1</tableCell></tableRow>' +
+				'<tableRow><tableCell>2</tableCell></tableRow>' +
+				'<tableRow><tableCell>3</tableCell></tableRow>' +
+				'</table>'
+			);
+		} );
+
+		it( 'should create table model from table with two theads with one row', () => {
+			editor.setData(
+				'<table>' +
+				'<thead><tr><td>1</td></tr></thead>' +
+				'<thead><tr><td>2</td></tr></thead>' +
+				'</table>'
+			);
+
+			expectModel(
+				'<table headingRows="1">' +
+				'<tableRow><tableCell>1</tableCell></tableRow>' +
+				'<tableRow><tableCell>2</tableCell></tableRow>' +
+				'</table>'
+			);
+		} );
+
+		it.skip( 'should create table model from table with thead after the tbody', () => {
+			editor.setData(
+				'<table>' +
+				'<tbody><tr><td>2</td></tr></tbody>' +
+				'<thead><tr><td>1</td></tr></thead>' +
+				'</table>'
+			);
+
+			expectModel(
+				'<table headingRows="1">' +
+				'<tableRow><tableCell>1</tableCell></tableRow>' +
+				'<tableRow><tableCell>2</tableCell></tableRow>' +
+				'</table>'
+			);
+		} );
+	} );
+
+	describe( 'downcastTable', () => {
+		it( 'should create table with tbody', () => {
+			setModelData( model,
+				'<table>' +
+				'<tableRow><tableCell></tableCell></tableRow>' +
+				'</table>'
+			);
+
+			expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
+				'<table>' +
+				'<tbody>' +
+				'<tr><td></td></tr>' +
+				'</tbody>' +
+				'</table>'
+			);
+		} );
+
+		it( 'should create table with tbody and thead', () => {
+			setModelData( model,
+				'<table headingRows="1">' +
+				'<tableRow><tableCell>1</tableCell></tableRow>' +
+				'<tableRow><tableCell>2</tableCell></tableRow>' +
+				'</table>'
+			);
+
+			expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
+				'<table>' +
+				'<thead>' +
+				'<tr><td>1</td></tr>' +
+				'</thead>' +
+				'<tbody>' +
+				'<tr><td>2</td></tr>' +
+				'</tbody>' +
+				'</table>'
+			);
+		} );
+
+		it( 'should create table with thead', () => {
+			setModelData( model,
+				'<table headingRows="2">' +
+				'<tableRow><tableCell>1</tableCell></tableRow>' +
+				'<tableRow><tableCell>2</tableCell></tableRow>' +
+				'</table>'
+			);
+
+			expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
+				'<table>' +
+				'<thead>' +
+				'<tr><td>1</td></tr>' +
+				'<tr><td>2</td></tr>' +
+				'</thead>' +
+				'</table>'
+			);
+		} );
+	} );
+} );