Procházet zdrojové kódy

Add empty tbody/thead section cleanup also when downcasting heading rows.

Maciej Gołaszewski před 5 roky
rodič
revize
d1d6facec5

+ 5 - 8
packages/ckeditor5-table/src/converters/downcast.js

@@ -209,9 +209,6 @@ export function downcastTableHeadingRowsChange( options = {} ) {
 					renameViewTableCell( tableCell, 'th', conversionApi, asWidget );
 				}
 			}
-
-			// Cleanup: this will remove any empty section from the view which may happen when moving all rows from a table section.
-			removeTableSectionIfEmpty( 'tbody', viewTable, conversionApi );
 		}
 		// The head section has shrunk so move rows from <thead> to <tbody>.
 		else {
@@ -234,11 +231,12 @@ export function downcastTableHeadingRowsChange( options = {} ) {
 			for ( const tableWalkerValue of tableWalker ) {
 				renameViewTableCellIfRequired( tableWalkerValue, tableAttributes, conversionApi, asWidget );
 			}
-
-			// Cleanup: this will remove any empty section from the view which may happen when moving all rows from a table section.
-			removeTableSectionIfEmpty( 'thead', viewTable, conversionApi );
 		}
 
+		// Cleanup: Ensure that thead & tbody sections are removed if left empty after moving rows. See #6437, #6391.
+		removeTableSectionIfEmpty( 'thead', viewTable, conversionApi );
+		removeTableSectionIfEmpty( 'tbody', viewTable, conversionApi );
+
 		function isBetween( index, lower, upper ) {
 			return index > lower && index < upper;
 		}
@@ -308,8 +306,7 @@ export function downcastRemoveRow() {
 			mapper.unbindViewElement( child );
 		}
 
-		// Cleanup: Ensure that thead & tbody sections are removed if left empty. See ckeditor/ckeditor5#6437.
-		// This happens if removing a row is associated with changing table's headingRows attribute (removing row from a heading section).
+		// Cleanup: Ensure that thead & tbody sections are removed if left empty after removing rows. See #6437, #6391.
 		removeTableSectionIfEmpty( 'thead', viewTable, conversionApi );
 		removeTableSectionIfEmpty( 'tbody', viewTable, conversionApi );
 	}, { priority: 'higher' } );

+ 38 - 0
packages/ckeditor5-table/tests/converters/downcast.js

@@ -1310,6 +1310,7 @@ describe( 'downcast converters', () => {
 				);
 			} );
 
+			// Might be a misuse of API or other operation - must work either wey. See #6437, #6391.
 			it( 'should react to removed row form the end of a heading rows (no body rows)', () => {
 				setModelData( model, modelTable( [
 					[ '00[]', '01' ],
@@ -1343,6 +1344,43 @@ describe( 'downcast converters', () => {
 				);
 			} );
 
+			// Might be a misuse of API or other operation - must work either wey. See #6437, #6391.
+			it( 'should react to removed row form the end of a heading rows (no body rows, using enqueueChange())', () => {
+				setModelData( model, modelTable( [
+					[ '00[]', '01' ],
+					[ '10', '11' ]
+				], { headingRows: 2 } ) );
+
+				const table = root.getChild( 0 );
+
+				model.change( writer => {
+					// Removing row from a heading section changes requires changing heading rows attribute.
+					writer.remove( table.getChild( 1 ) );
+
+					model.enqueueChange( writer => {
+						writer.setAttribute( 'headingRows', 1, table );
+					} );
+				} );
+
+				assertEqualMarkup( getViewData( view, { withoutSelection: true } ),
+					'<figure class="ck-widget ck-widget_with-selection-handle table" contenteditable="false">' +
+						'<div class="ck ck-widget__selection-handle"></div>' +
+						'<table>' +
+							'<thead>' +
+								'<tr>' +
+									'<th class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
+										'<span style="display:inline-block">00</span>' +
+									'</th>' +
+									'<th class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
+										'<span style="display:inline-block">01</span>' +
+									'</th>' +
+								'</tr>' +
+							'</thead>' +
+						'</table>' +
+					'</figure>'
+				);
+			} );
+
 			it( 'should remove empty thead if a last row was removed from a heading rows (has heading and body)', () => {
 				setModelData( model, modelTable( [
 					[ '00[]', '01' ],