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

Commit

Permalink
Merge pull request #180 from ckeditor/t/ckeditor5/1620
Browse files Browse the repository at this point in the history
Fix: Single paragraphs with attributes inside tableCell will be properly converted in data pipeline.. Closes ckeditor/ckeditor5#1620.
  • Loading branch information
Reinmar authored Apr 3, 2019
2 parents bff207c + ed4a61c commit 67ec89f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
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

0 comments on commit 67ec89f

Please sign in to comment.