Bladeren bron

Fix: The set table headers command should properly split rowspanned cell at the end of a row.

Maciej Gołaszewski 7 jaren geleden
bovenliggende
commit
bf7cf52952

+ 14 - 10
packages/ckeditor5-table/src/commands/settableheaderscommand.js

@@ -95,6 +95,7 @@ function updateTableAttribute( table, attributeName, newValue, writer ) {
  * Splits table cell vertically.
  *
  * @param {module:engine/model/element} tableCell
+ * @param {Number} headingRows
  * @param writer
  */
 function splitVertically( tableCell, headingRows, writer ) {
@@ -103,15 +104,7 @@ function splitVertically( tableCell, headingRows, writer ) {
 	const rowIndex = tableRow.index;
 
 	const rowspan = parseInt( tableCell.getAttribute( 'rowspan' ) );
-
 	const newRowspan = headingRows - rowIndex;
-	const spanToSet = rowspan - newRowspan;
-
-	if ( newRowspan > 1 ) {
-		writer.setAttribute( 'rowspan', newRowspan, tableCell );
-	} else {
-		writer.removeAttribute( 'rowspan', tableCell );
-	}
 
 	const startRow = table.getChildIndex( tableRow );
 	const endRow = startRow + newRowspan;
@@ -123,11 +116,15 @@ function splitVertically( tableCell, headingRows, writer ) {
 
 	const attributes = {};
 
+	const spanToSet = rowspan - newRowspan;
+
 	if ( spanToSet > 1 ) {
 		attributes.rowspan = spanToSet;
 	}
 
-	for ( const tableWalkerInfo of [ ...tableWalker ] ) {
+	const values = [ ...tableWalker ];
+
+	for ( const tableWalkerInfo of values ) {
 		if ( tableWalkerInfo.cell ) {
 			previousCell = tableWalkerInfo.cell;
 		}
@@ -143,9 +140,16 @@ function splitVertically( tableCell, headingRows, writer ) {
 		if ( columnIndex !== undefined && columnIndex === tableWalkerInfo.column && tableWalkerInfo.row === endRow ) {
 			const insertRow = table.getChild( tableWalkerInfo.row );
 
-			const position = previousCell.parent === insertRow ? Position.createAt( insertRow ) : Position.createAfter( previousCell );
+			const position = previousCell.parent === insertRow ? Position.createAfter( previousCell ) : Position.createAt( insertRow );
 
 			writer.insertElement( 'tableCell', attributes, position );
 		}
 	}
+
+	// Update rowspan attribute after iterating over current table.
+	if ( newRowspan > 1 ) {
+		writer.setAttribute( 'rowspan', newRowspan, tableCell );
+	} else {
+		writer.removeAttribute( 'rowspan', tableCell );
+	}
 }

+ 1 - 1
packages/ckeditor5-table/src/tablewalker.js

@@ -66,7 +66,7 @@ export default class TableWalker {
 	 * @param {Object} [options={}] Object with configuration.
 	 * @param {Number} [options.startRow=0] A row index for which this iterator should start.
 	 * @param {Number} [options.endRow] A row index for which this iterator should end.
-	 * @param {Number} [options.includeSpanned] Also return values for spanned cells.
+	 * @param {Boolean} [options.includeSpanned] Also return values for spanned cells.
 	 */
 	constructor( table, options = {} ) {
 		/**

+ 1 - 2
packages/ckeditor5-table/tests/commands/settableheaderscommand.js

@@ -214,8 +214,7 @@ describe( 'SetTableHeadersCommand', () => {
 			], { headingRows: 1 } ) );
 		} );
 
-		// TODO: fix me
-		it.skip( 'should fix rowspaned cells inside a row', () => {
+		it( 'should fix rowspaned cells inside a row', () => {
 			setData( model, modelTable( [
 				[ '00', { rowspan: 2, contents: '[]01' } ],
 				[ '10' ]

+ 25 - 0
packages/ckeditor5-table/tests/tablewalker.js

@@ -183,6 +183,18 @@ describe( 'TableWalker', () => {
 			], { includeSpanned: true } );
 		} );
 
+		it( 'should output rowspanned cells at the end of a table row', () => {
+			testWalker( [
+				[ '00', { rowspan: 2, contents: '01' } ],
+				[ '10' ]
+			], [
+				{ row: 0, column: 0, data: '00' },
+				{ row: 0, column: 1, data: '01' },
+				{ row: 1, column: 0, data: '10' },
+				{ row: 1, column: 1, data: undefined }
+			], { includeSpanned: true } );
+		} );
+
 		it( 'should work with startRow & endRow options', () => {
 			testWalker( [
 				[ { colspan: 2, rowspan: 3, contents: '00' }, '02' ],
@@ -212,5 +224,18 @@ describe( 'TableWalker', () => {
 				{ row: 0, column: 2, data: '13' }
 			], { endRow: 0 } );
 		} );
+
+		it( 'should output rowspanned cells at the end of a table row', () => {
+			testWalker( [
+				[ '00', { rowspan: 2, contents: '01' } ],
+				[ '10' ],
+				[ '20', '21' ]
+			], [
+				{ row: 0, column: 0, data: '00' },
+				{ row: 0, column: 1, data: '01' },
+				{ row: 1, column: 0, data: '10' },
+				{ row: 1, column: 1, data: undefined }
+			], { startRow: 0, endRow: 1, includeSpanned: true } );
+		} );
 	} );
 } );