Procházet zdrojové kódy

Add check for $text node in table cell content in post-fixer.

Maciej Gołaszewski před 7 roky
rodič
revize
1f286c516d

+ 14 - 2
packages/ckeditor5-table/src/converters/table-cell-content-post-fixer.js

@@ -97,16 +97,28 @@ function fixTableRow( tableRow, writer ) {
 	return wasFixed;
 }
 
-// Fixes all table cell content by adding paragraph to a table cell without any child.
+// Fixes all table cell content by:
+// - adding paragraph to a table cell without any child.
+// - wrapping direct $text in <paragraph>.
 //
 // @param {module:engine/model/element~Element} table
 // @param {module:engine/model/writer~Writer} writer
+// @returns {Boolean}
 function fixTableCellContent( tableCell, writer ) {
+	// Insert paragraph to an empty table cell.
 	if ( tableCell.childCount == 0 ) {
 		writer.insertElement( 'paragraph', tableCell );
 
 		return true;
 	}
 
-	return false;
+	// Check table cell children for directly placed $text nodes.
+	const textNodes = Array.from( tableCell.getChildren() ).filter( child => child.is( 'text' ) );
+
+	for ( const child of textNodes ) {
+		writer.wrap( writer.createRangeOn( child ), 'paragraph' );
+	}
+
+	// Return true when there were text nodes to fix.
+	return !!textNodes.length;
 }

+ 33 - 0
packages/ckeditor5-table/tests/converters/table-cell-content-post-fixer.js

@@ -121,4 +121,37 @@ describe( 'Table cell content post-fixer', () => {
 			'</table>'
 		) );
 	} );
+
+	it( 'should wrap in paragraph $text nodes placed directly in tableCell ', () => {
+		setModelData( model,
+			'<table>' +
+				'<tableRow>' +
+					'<tableCell><paragraph></paragraph></tableCell>' +
+				'</tableRow>' +
+			'</table>'
+		);
+
+		// Remove paragraph from table cell & insert: $text<paragraph>$text</paragraph>$text.
+		model.change( writer => {
+			writer.remove( writer.createRangeIn( root.getNodeByPath( [ 0, 0, 0 ] ) ) );
+			writer.insertText( 'foo', writer.createPositionAt( root.getNodeByPath( [ 0, 0, 0 ] ), 0 ) );
+
+			const paragraph = writer.createElement( 'paragraph' );
+			writer.insertText( 'bar', writer.createPositionAt( paragraph, 0 ) );
+			writer.insert( paragraph, writer.createPositionAt( root.getNodeByPath( [ 0, 0, 0 ] ), 'end' ) );
+			writer.insertText( 'baz', writer.createPositionAt( root.getNodeByPath( [ 0, 0, 0 ] ), 'end' ) );
+		} );
+
+		expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formatTable(
+			'<table>' +
+				'<tableRow>' +
+					'<tableCell>' +
+						'<paragraph>foo</paragraph>' +
+						'<paragraph>bar</paragraph>' +
+						'<paragraph>baz</paragraph>' +
+					'</tableCell>' +
+				'</tableRow>' +
+			'</table>'
+		) );
+	} );
 } );