浏览代码

Updated table layout post-fixer to properly fix empty rows.

Kuba Niegowski 5 年之前
父节点
当前提交
996f2af124

+ 11 - 8
packages/ckeditor5-table/src/converters/table-layout-post-fixer.js

@@ -290,14 +290,16 @@ function fixTableRowsSizes( table, writer ) {
 	let wasFixed = false;
 
 	const rowsLengths = getRowsLengths( table );
-	const tableSize = rowsLengths[ 0 ];
 
-	const isValid = Object.values( rowsLengths ).every( length => length === tableSize );
+	// Verify if all the rows have the same number of columns.
+	const tableSize = rowsLengths[ 0 ];
+	const isValid = rowsLengths.every( length => length === tableSize );
 
-	if ( !isValid ) {
-		const maxColumns = Object.values( rowsLengths ).reduce( ( prev, current ) => current > prev ? current : prev, 0 );
+	if ( !isValid || !tableSize ) {
+		// Find the maximum number of columns (but it can't be less than one).
+		const maxColumns = rowsLengths.reduce( ( prev, current ) => current > prev ? current : prev, 1 );
 
-		for ( const [ rowIndex, size ] of Object.entries( rowsLengths ) ) {
+		for ( const [ rowIndex, size ] of rowsLengths.entries() ) {
 			const columnsToInsert = maxColumns - size;
 
 			if ( columnsToInsert ) {
@@ -346,12 +348,13 @@ function findCellsToTrim( table ) {
 	return cellsToTrim;
 }
 
-// Returns an object with lengths of rows assigned to the corresponding row index.
+// Returns an array with lengths of rows assigned to the corresponding row index.
 //
 // @param {module:engine/model/element~Element} table
-// @returns {Object}
+// @returns {Array.<Number>}
 function getRowsLengths( table ) {
-	const lengths = {};
+	// TableWalker will not provide items for the empty rows, we need to pre-fill this array.
+	const lengths = new Array( table.childCount ).fill( 0 );
 
 	for ( const { row } of new TableWalker( table, { includeSpanned: true } ) ) {
 		if ( !lengths[ row ] ) {

+ 4 - 2
packages/ckeditor5-table/tests/converters/table-layout-post-fixer.js

@@ -36,7 +36,8 @@ describe( 'Table layout post-fixer', () => {
 			const parsed = parse( modelTable( [
 				[ '00' ],
 				[ '10', '11', '12' ],
-				[ '20', '21' ]
+				[ '20', '21' ],
+				[]
 			] ), model.schema );
 
 			model.change( writer => {
@@ -47,7 +48,8 @@ describe( 'Table layout post-fixer', () => {
 			assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
 				[ '00', '', '' ],
 				[ '10', '11', '12' ],
-				[ '20', '21', '' ]
+				[ '20', '21', '' ],
+				[ '', '', '' ]
 			] ) );
 		} );