Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

T/ckeditor5/1620: Fix downcast conversion in data pipeline when converting single paragraph with attributes in table cell. #180

Merged
merged 2 commits into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/converters/downcast.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,20 +384,19 @@ function createViewTableCellElement( tableWalkerValue, tableAttributes, insertPo

const tableCell = tableWalkerValue.cell;

const isSingleParagraph = tableCell.childCount === 1 && tableCell.getChild( 0 ).name === 'paragraph';
const firstChild = tableCell.getChild( 0 );
const isSingleParagraph = tableCell.childCount === 1 && firstChild.name === 'paragraph';

conversionApi.writer.insert( insertPosition, cellElement );

if ( isSingleParagraph ) {
if ( isSingleParagraph && !hasAnyAttribute( firstChild ) ) {
const innerParagraph = tableCell.getChild( 0 );
const paragraphInsertPosition = conversionApi.writer.createPositionAt( cellElement, 'end' );

conversionApi.consumable.consume( innerParagraph, 'insert' );

if ( options.asWidget ) {
const containerName = [ ...innerParagraph.getAttributeKeys() ].length ? 'p' : 'span';

const fakeParagraph = conversionApi.writer.createContainerElement( containerName );
const fakeParagraph = conversionApi.writer.createContainerElement( 'span' );

conversionApi.mapper.bindElements( innerParagraph, fakeParagraph );
conversionApi.writer.insert( paragraphInsertPosition, fakeParagraph );
Expand Down Expand Up @@ -554,3 +553,11 @@ function getViewTable( viewFigure ) {
}
}
}

// Checks if element has any attribute set.
//
// @param {module:engine/model/element~Element element
// @returns {Boolean}
function hasAnyAttribute( element ) {
return !![ ...element.getAttributeKeys() ].length;
}
29 changes: 29 additions & 0 deletions tests/converters/downcast.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,35 @@ describe( 'downcast converters', () => {
) );
} );

it( 'should create table with block content (attribute on paragraph)', () => {
editor.conversion.attributeToAttribute(
{
model: { key: 'alignment', values: [ 'right', 'center', 'justify' ] },
view: {
right: { key: 'style', value: { 'text-align': 'right' } },
center: { key: 'style', value: { 'text-align': 'center' } },
justify: { key: 'style', value: { 'text-align': 'justify' } }
}
}
);

setModelData( model, modelTable( [
[ '<paragraph alignment="right">00</paragraph>' ]
] ) );

expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
'<figure class="table">' +
'<table>' +
'<tbody>' +
'<tr>' +
'<td><p style="text-align:right">00</p></td>' +
'</tr>' +
'</tbody>' +
'</table>' +
'</figure>'
) );
} );

it( 'should be possible to overwrite', () => {
editor.conversion.elementToElement( { model: 'tableRow', view: 'tr', converterPriority: 'high' } );
editor.conversion.elementToElement( { model: 'tableCell', view: 'td', converterPriority: 'high' } );
Expand Down