From f166ba4b6c361f784b47b65a1028adf44811e811 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Go=C5=82aszewski?= Date: Thu, 13 Feb 2020 09:06:32 +0100 Subject: [PATCH] Convert only top & bottom values for vertical-align. --- .../tablecellpropertiesediting.js | 34 ++++++++++++++++++- .../tablecellpropertiesediting.js | 20 +++++++++-- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/tablecellproperties/tablecellpropertiesediting.js b/src/tablecellproperties/tablecellpropertiesediting.js index 7c36ef7a..27362863 100644 --- a/src/tablecellproperties/tablecellpropertiesediting.js +++ b/src/tablecellproperties/tablecellpropertiesediting.js @@ -92,7 +92,7 @@ export default class TableCellPropertiesEditing extends Plugin { enableProperty( schema, conversion, 'backgroundColor', 'background-color' ); editor.commands.add( 'tableCellBackgroundColor', new TableCellBackgroundColorCommand( editor ) ); - enableProperty( schema, conversion, 'verticalAlignment', 'vertical-align' ); + enableVerticalAlignmentProperty( schema, conversion ); editor.commands.add( 'tableCellVerticalAlignment', new TableCellVerticalAlignmentCommand( editor ) ); } } @@ -150,6 +150,38 @@ function enableHorizontalAlignmentProperty( schema, conversion ) { } ); } +// Enables the `'verticalAlignment'` attribute for table cells. +// +// @param {module:engine/model/schema~Schema} schema +// @param {module:engine/conversion/conversion~Conversion} conversion +function enableVerticalAlignmentProperty( schema, conversion ) { + schema.extend( 'tableCell', { + allowAttributes: [ 'verticalAlignment' ] + } ); + + conversion.attributeToAttribute( { + model: { + name: 'tableCell', + key: 'verticalAlignment', + values: [ 'top', 'bottom' ] + }, + view: { + top: { + key: 'style', + value: { + 'vertical-align': 'top' + } + }, + bottom: { + key: 'style', + value: { + 'vertical-align': 'bottom' + } + } + } + } ); +} + // Enables conversion for an attribute for simple view-model mappings. // // @param {String} modelAttribute diff --git a/tests/tablecellproperties/tablecellpropertiesediting.js b/tests/tablecellproperties/tablecellpropertiesediting.js index 05fb122f..f2678462 100644 --- a/tests/tablecellproperties/tablecellpropertiesediting.js +++ b/tests/tablecellproperties/tablecellpropertiesediting.js @@ -783,12 +783,26 @@ describe( 'table cell properties', () => { } ); describe( 'upcast conversion', () => { - it( 'should upcast vertical-align', () => { + it( 'should upcast "top" vertical-align', () => { editor.setData( '
foo
' ); const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] ); expect( tableCell.getAttribute( 'verticalAlignment' ) ).to.equal( 'top' ); } ); + + it( 'should upcast "bottom" vertical-align', () => { + editor.setData( '
foo
' ); + const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] ); + + expect( tableCell.getAttribute( 'verticalAlignment' ) ).to.equal( 'bottom' ); + } ); + + it( 'should not upcast "middle" vertical-align', () => { + editor.setData( '
foo
' ); + const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] ); + + expect( tableCell.getAttribute( 'verticalAlignment' ) ).to.be.undefined; + } ); } ); describe( 'downcast conversion', () => { @@ -829,9 +843,9 @@ describe( 'table cell properties', () => { } ); it( 'should downcast verticalAlignment', () => { - model.change( writer => writer.setAttribute( 'verticalAlignment', 'middle', tableCell ) ); + model.change( writer => writer.setAttribute( 'verticalAlignment', 'top', tableCell ) ); - assertTableCellStyle( editor, 'vertical-align:middle;' ); + assertTableCellStyle( editor, 'vertical-align:top;' ); } ); } ); } );