浏览代码

Other: Remove custom upcast converter for tableRow.

Maciej Gołaszewski 7 年之前
父节点
当前提交
428b942185

+ 15 - 19
packages/ckeditor5-table/src/converters/upcasttable.js

@@ -55,8 +55,8 @@ export default function upcastTable() {
 				conversionApi.writer.insertElement( 'tableCell', ModelPosition.createAt( row, 'end' ) );
 			}
 
-			// Upcast table rows as we need to insert them to table in proper order (heading rows first).
-			upcastTableRows( rows, table, conversionApi );
+			// Upcast table rows in proper order (heading rows first).
+			rows.forEach( row => conversionApi.convertItem( row, ModelPosition.createAt( table, 'end' ) ) );
 
 			// Set conversion result range.
 			data.modelRange = new ModelRange(
@@ -99,9 +99,22 @@ function scanTable( viewTable ) {
 		headingColumns: 0
 	};
 
+	// The <tbody> and <thead> sections in the DOM doesn't have to be in order <thead> -> <tbody> and there might be more then one of them.
+	// As the model doesn't have those sections rows from different sections must be sorted.
+	// Ie below is a valid HTML table:
+	//
+	//		<table>
+	//			<tbody><tr><td>2</td></tr></tbody>
+	//			<thead><tr><td>1</td></tr></thead>
+	//			<tbody><tr><td>3</td></tr></tbody>
+	//		</table>
+	//
+	// But browsers will render rows in order as : 1 as heading and 2 & 3 as (body).
 	const headRows = [];
 	const bodyRows = [];
 
+	// Currently the editor does not support more then one <thead> section.
+	// Only the first <thead> from the view will be used as heading rows and others will be converted to body rows.
 	let firstTheadElement;
 
 	for ( const tableChild of Array.from( viewTable.getChildren() ) ) {
@@ -137,23 +150,6 @@ function scanTable( viewTable ) {
 	return tableMeta;
 }
 
-// Converts table rows and extracts table metadata.
-//
-// @param {Array.<module:engine/view/element~Element>} viewRows
-// @param {module:engine/model/element~Element} modelTable
-// @param {module:engine/conversion/upcastdispatcher~ViewConversionApi} conversionApi
-function upcastTableRows( viewRows, modelTable, conversionApi ) {
-	for ( const viewRow of viewRows ) {
-		const modelRow = conversionApi.writer.createElement( 'tableRow' );
-
-		conversionApi.writer.insert( modelRow, ModelPosition.createAt( modelTable, 'end' ) );
-		conversionApi.consumable.consume( viewRow, { name: true } );
-
-		const childrenCursor = ModelPosition.createAt( modelRow );
-		conversionApi.convertChildren( viewRow, childrenCursor );
-	}
-}
-
 // Scans <tr> and it's children for metadata:
 // - For heading row:
 //     - either add this row to heading or body rows.

+ 2 - 1
packages/ckeditor5-table/src/tableediting.js

@@ -78,7 +78,8 @@ export default class TablesEditing extends Plugin {
 		conversion.for( 'editingDowncast' ).add( downcastInsertRow( { asWidget: true } ) );
 		conversion.for( 'dataDowncast' ).add( downcastInsertRow() );
 
-		// Remove row conversion.
+		// Table row conversion.
+		conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableRow', view: 'tr' } ) );
 		conversion.for( 'downcast' ).add( downcastRemoveRow() );
 
 		// Table cell conversion.

+ 19 - 0
packages/ckeditor5-table/tests/manual/table.html

@@ -195,4 +195,23 @@
 			<td>c</td>
 		</tr>
 	</table>
+
+	<p>Table with thead section between two tbody sections</p>
+	<table>
+		<tbody>
+		<tr>
+			<td>2</td>
+		</tr>
+		</tbody>
+		<thead>
+		<tr>
+			<td>1</td>
+		</tr>
+		</thead>
+		<tbody>
+		<tr>
+			<td>3</td>
+		</tr>
+		</tbody>
+	</table>
 </div>