From 723d58c7b18705286a79c9cbdc78790045a7a485 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Go=C5=82aszewski?= Date: Mon, 1 Apr 2019 16:34:48 +0200 Subject: [PATCH 1/2] Add test for alignment attribute conversion in data pipeline. --- tests/converters/downcast.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/converters/downcast.js b/tests/converters/downcast.js index d2ce2b42..d80b6bb2 100644 --- a/tests/converters/downcast.js +++ b/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( [ + [ '00' ] + ] ) ); + + expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable( + '
' + + '' + + '' + + '' + + '' + + '' + + '' + + '

00

' + + '
' + ) ); + } ); + it( 'should be possible to overwrite', () => { editor.conversion.elementToElement( { model: 'tableRow', view: 'tr', converterPriority: 'high' } ); editor.conversion.elementToElement( { model: 'tableCell', view: 'td', converterPriority: 'high' } ); From ed4a61c18ed083e484bbe01b5c71f00dac83ee08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Go=C5=82aszewski?= Date: Mon, 1 Apr 2019 16:53:05 +0200 Subject: [PATCH 2/2] Single paragraphs with attributes inside tableCell will be properly converted in data pipeline. --- src/converters/downcast.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/converters/downcast.js b/src/converters/downcast.js index 109a1a57..f9c70b05 100644 --- a/src/converters/downcast.js +++ b/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; +}