diff --git a/src/converters/table-cell-content-post-fixer.js b/src/converters/table-cell-content-post-fixer.js index c8bec539..bd6436bb 100644 --- a/src/converters/table-cell-content-post-fixer.js +++ b/src/converters/table-cell-content-post-fixer.js @@ -97,16 +97,29 @@ 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 . // // @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. + // Temporary solution. See https://github.com/ckeditor/ckeditor5/issues/1464. + 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; } diff --git a/tests/converters/table-cell-content-post-fixer.js b/tests/converters/table-cell-content-post-fixer.js index 0d8241cc..6e01cd31 100644 --- a/tests/converters/table-cell-content-post-fixer.js +++ b/tests/converters/table-cell-content-post-fixer.js @@ -121,4 +121,104 @@ describe( 'Table cell content post-fixer', () => { '' ) ); } ); + + it( 'should wrap in paragraph $text nodes placed directly in tableCell (on table cell modification) ', () => { + setModelData( model, + '' + + '' + + '' + + '' + + '
' + ); + + // Remove paragraph from table cell & insert: $text$text$text. + model.change( writer => { + writer.remove( writer.createRangeIn( root.getNodeByPath( [ 0, 0, 0 ] ) ) ); + + const paragraph = writer.createElement( 'paragraph' ); + + writer.insertText( 'foo', root.getNodeByPath( [ 0, 0, 0 ] ) ); + writer.insert( paragraph, root.getNodeByPath( [ 0, 0, 0 ] ), 'end' ); + writer.insertText( 'bar', paragraph ); + writer.insertText( 'baz', root.getNodeByPath( [ 0, 0, 0 ] ), 'end' ); + } ); + + expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formatTable( + '' + + '' + + '' + + 'foo' + + 'bar' + + 'baz' + + '' + + '' + + '
' + ) ); + } ); + + it( 'should wrap in paragraph $text nodes placed directly in tableCell (on inserting table cell)', () => { + setModelData( model, + '' + + '' + + '' + + '' + + '
' + ); + + // Insert new tableCell with $text. + model.change( writer => { + const tableCell = writer.createElement( 'tableCell' ); + + writer.insertText( 'foo', tableCell ); + writer.insert( tableCell, writer.createPositionAt( root.getNodeByPath( [ 0, 0 ] ), 'end' ) ); + } ); + + expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formatTable( + '' + + '' + + '' + + '' + + '' + + '' + + 'foo' + + '' + + '' + + '
' + ) ); + } ); + + it( 'should wrap in paragraph $text nodes placed directly in tableCell (on inserting table rows)', () => { + setModelData( model, + '' + + '' + + '' + + '' + + '
' + ); + + // Insert new tableRow with tableCell with $text. + model.change( writer => { + const tableRow = writer.createElement( 'tableRow' ); + const tableCell = writer.createElement( 'tableCell' ); + + writer.insertText( 'foo', tableCell ); + writer.insert( tableCell, tableRow ); + writer.insert( tableRow, writer.createPositionAt( root.getNodeByPath( [ 0 ] ), 'end' ) ); + } ); + + expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formatTable( + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + 'foo' + + '' + + '' + + '
' + ) ); + } ); } );