8
0
Просмотр исходного кода

Merge pull request #180 from ckeditor/t/ckeditor5/1620

Fix: Single paragraphs with attributes inside tableCell will be properly converted in data pipeline.. Closes ckeditor/ckeditor5#1620.
Piotrek Koszuliński 6 лет назад
Родитель
Сommit
b987e7c379

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

@@ -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 );
@@ -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 - 0
packages/ckeditor5-table/tests/converters/downcast.js

@@ -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' } );