Browse Source

Table layout post-fixer updated to remove empty rows.

Kuba Niegowski 5 years ago
parent
commit
a522867203

+ 17 - 3
packages/ckeditor5-table/src/converters/table-layout-post-fixer.js

@@ -290,14 +290,28 @@ function fixTableRowsSizes( table, writer ) {
 	let wasFixed = false;
 
 	const rowsLengths = getRowsLengths( table );
+	const rowsToRemove = [];
+
+	// Find empty rows.
+	for ( const [ rowIndex, size ] of rowsLengths.entries() ) {
+		if ( !size ) {
+			rowsToRemove.push( rowIndex );
+		}
+	}
+
+	// Remove empty rows.
+	for ( const rowIndex of rowsToRemove.reverse() ) {
+		writer.remove( table.getChild( rowIndex ) );
+		rowsLengths.splice( rowIndex, 1 );
+	}
 
 	// Verify if all the rows have the same number of columns.
 	const tableSize = rowsLengths[ 0 ];
 	const isValid = rowsLengths.every( length => length === tableSize );
 
-	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 );
+	if ( !isValid ) {
+		// Find the maximum number of columns.
+		const maxColumns = rowsLengths.reduce( ( prev, current ) => current > prev ? current : prev, 0 );
 
 		for ( const [ rowIndex, size ] of rowsLengths.entries() ) {
 			const columnsToInsert = maxColumns - size;

+ 4 - 0
packages/ckeditor5-table/tests/_utils/utils.js

@@ -500,6 +500,10 @@ function getClassToSet( attributes ) {
 export function createTableAsciiArt( model, table ) {
 	const tableMap = [ ...new TableWalker( table, { includeSpanned: true } ) ];
 
+	if ( !tableMap.length ) {
+		return '';
+	}
+
 	const { row: lastRow, column: lastColumn } = tableMap[ tableMap.length - 1 ];
 	const columns = lastColumn + 1;
 

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

@@ -36,8 +36,7 @@ describe( 'Table layout post-fixer', () => {
 			const parsed = parse( modelTable( [
 				[ '00' ],
 				[ '10', '11', '12' ],
-				[ '20', '21' ],
-				[]
+				[ '20', '21' ]
 			] ), model.schema );
 
 			model.change( writer => {
@@ -48,8 +47,7 @@ describe( 'Table layout post-fixer', () => {
 			assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
 				[ '00', '', '' ],
 				[ '10', '11', '12' ],
-				[ '20', '21', '' ],
-				[ '', '', '' ]
+				[ '20', '21', '' ]
 			] ) );
 		} );
 
@@ -91,6 +89,25 @@ describe( 'Table layout post-fixer', () => {
 			] ) );
 		} );
 
+		it( 'should remove empty rows', () => {
+			const parsed = parse( modelTable( [
+				[ '00', '01' ],
+				[ ],
+				[ '20', '21', '22' ],
+				[ ]
+			] ), model.schema );
+
+			model.change( writer => {
+				writer.remove( writer.createRangeIn( root ) );
+				writer.insert( parsed, root );
+			} );
+
+			assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
+				[ '00', '01', '' ],
+				[ '20', '21', '22' ]
+			] ) );
+		} );
+
 		it( 'should fix the wrong rowspan attribute of a table cell inside the header', () => {
 			const parsed = parse( modelTable( [
 				[ { rowspan: 2, contents: '00' }, { rowspan: 3, contents: '01' }, '02' ],