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

Use conversion.attributeToAttribute() helper for alignment property.

Maciej Gołaszewski 6 лет назад
Родитель
Сommit
cc8ec65c17

+ 16 - 39
packages/ckeditor5-table/src/tableproperties/tablepropertiesediting.js

@@ -22,7 +22,6 @@ import TableHeightCommand from './commands/tableheightcommand';
 import TableAlignmentCommand from './commands/tablealignmentcommand';
 
 // RegExp used for matching margin style in converters.
-const MARGIN_REG_EXP = /^(auto|0(%|[a-z]{2,4})?)$/;
 const ALIGN_VALUES_REG_EXP = /^(left|right|center)$/;
 
 /**
@@ -111,48 +110,11 @@ function enableAlignmentProperty( schema, conversion ) {
 	schema.extend( 'table', {
 		allowAttributes: [ 'alignment' ]
 	} );
-	conversion.for( 'upcast' )
-		.attributeToAttribute( {
-			view: {
-				styles: {
-					'margin-right': MARGIN_REG_EXP,
-					'margin-left': MARGIN_REG_EXP
-				}
-			},
-			model: {
-				name: 'table',
-				key: 'alignment',
-				value: viewElement => {
-					// At this point we only have auto or 0 value (with a unit).
-					if ( viewElement.getStyle( 'margin-right' ) != 'auto' ) {
-						return 'right';
-					}
-
-					if ( viewElement.getStyle( 'margin-left' ) != 'auto' ) {
-						return 'left';
-					}
 
-					return 'center';
-				}
-			}
-		} )
-		// Support for backwards compatibility and pasting from other sources.
+	conversion
 		.attributeToAttribute( {
-			view: {
-				attributes: {
-					align: ALIGN_VALUES_REG_EXP
-				}
-			},
 			model: {
 				name: 'table',
-				key: 'alignment',
-				value: viewElement => viewElement.getAttribute( 'align' )
-			}
-		} );
-
-	conversion.for( 'downcast' )
-		.attributeToAttribute( {
-			model: {
 				key: 'alignment',
 				values: [ 'left', 'center', 'right' ]
 			},
@@ -181,6 +143,21 @@ function enableAlignmentProperty( schema, conversion ) {
 			},
 			converterPriority: 'high'
 		} );
+
+	conversion.for( 'upcast' )
+		// Support for backwards compatibility and pasting from other sources.
+		.attributeToAttribute( {
+			view: {
+				attributes: {
+					align: ALIGN_VALUES_REG_EXP
+				}
+			},
+			model: {
+				name: 'table',
+				key: 'alignment',
+				value: viewElement => viewElement.getAttribute( 'align' )
+			}
+		} );
 }
 
 // Enables conversion for an attribute for simple view-model mappings.

+ 4 - 4
packages/ckeditor5-table/tests/tableproperties/commands/tablealignmentcommand.js

@@ -102,7 +102,7 @@ describe( 'table properties', () => {
 
 						command.execute( { value: 'right' } );
 
-						assertTableStyle( editor, 'margin-left:auto;margin-right:0;' );
+						assertTableStyle( editor, null, 'float:left;margin-right:0;' );
 					} );
 
 					it( 'should change selected table alignment to a passed value', () => {
@@ -110,7 +110,7 @@ describe( 'table properties', () => {
 
 						command.execute( { value: 'right' } );
 
-						assertTableStyle( editor, 'margin-left:auto;margin-right:0;' );
+						assertTableStyle( editor, null, 'float:left;margin-right:0;' );
 					} );
 
 					it( 'should remove alignment from a selected table if no value is passed', () => {
@@ -128,7 +128,7 @@ describe( 'table properties', () => {
 
 						command.execute( { value: 'right' } );
 
-						assertTableStyle( editor, 'margin-left:auto;margin-right:0;' );
+						assertTableStyle( editor, null, 'float:left;margin-right:0;' );
 					} );
 
 					it( 'should change selected table alignment to a passed value', () => {
@@ -136,7 +136,7 @@ describe( 'table properties', () => {
 
 						command.execute( { value: 'right' } );
 
-						assertTableStyle( editor, 'margin-left:auto;margin-right:0;' );
+						assertTableStyle( editor, null, 'float:left;margin-right:0;' );
 					} );
 
 					it( 'should remove alignment from a selected table if no value is passed', () => {

+ 7 - 21
packages/ckeditor5-table/tests/tableproperties/tablepropertiesediting.js

@@ -574,21 +574,21 @@ describe( 'table properties', () => {
 			} );
 		} );
 
-		describe.only( 'alignment', () => {
+		describe( 'alignment', () => {
 			it( 'should set proper schema rules', () => {
 				expect( model.schema.checkAttribute( [ '$root', 'table' ], 'alignment' ) ).to.be.true;
 			} );
 
 			describe( 'upcast conversion', () => {
-				it( 'should upcast style="margin-left:auto;margin-right:0" to right value', () => {
-					editor.setData( '<table style="margin-left:auto;margin-right:0"><tr><td>foo</td></tr></table>' );
+				it( 'should upcast style="float:left;margin-right:0" to right value', () => {
+					editor.setData( '<table style="float:left;margin-right:0"><tr><td>foo</td></tr></table>' );
 					const table = model.document.getRoot().getNodeByPath( [ 0 ] );
 
 					expect( table.getAttribute( 'alignment' ) ).to.equal( 'right' );
 				} );
 
-				it( 'should upcast style="margin-left:0;margin-right:auto" to left value', () => {
-					editor.setData( '<table style="margin-left:0;margin-right:auto"><tr><td>foo</td></tr></table>' );
+				it( 'should upcast style="margin-left:0;float:right;" to left value', () => {
+					editor.setData( '<table style="margin-left:0;float:right;"><tr><td>foo</td></tr></table>' );
 					const table = model.document.getRoot().getNodeByPath( [ 0 ] );
 
 					expect( table.getAttribute( 'alignment' ) ).to.equal( 'left' );
@@ -601,22 +601,8 @@ describe( 'table properties', () => {
 					expect( table.getAttribute( 'alignment' ) ).to.equal( 'center' );
 				} );
 
-				it( 'should upcast style="margin-left:auto;margin-right:0pt" to right value', () => {
-					editor.setData( '<table style="margin-left:auto;margin-right:0pt"><tr><td>foo</td></tr></table>' );
-					const table = model.document.getRoot().getNodeByPath( [ 0 ] );
-
-					expect( table.getAttribute( 'alignment' ) ).to.equal( 'right' );
-				} );
-
-				it( 'should upcast style="margin-left:auto;margin-right:0%" to right value', () => {
-					editor.setData( '<table style="margin-left:auto;margin-right:0%"><tr><td>foo</td></tr></table>' );
-					const table = model.document.getRoot().getNodeByPath( [ 0 ] );
-
-					expect( table.getAttribute( 'alignment' ) ).to.equal( 'right' );
-				} );
-
-				it( 'should not upcast style="margin-left:auto;margin-right:0.23pt" to right value', () => {
-					editor.setData( '<table style="margin-left:auto;margin-right:0.23pt"><tr><td>foo</td></tr></table>' );
+				it( 'should not upcast style="float:left;margin-right:23px" to right value (non-zero margin)', () => {
+					editor.setData( '<table style="float:left;margin-right:23px"><tr><td>foo</td></tr></table>' );
 					const table = model.document.getRoot().getNodeByPath( [ 0 ] );
 
 					expect( table.hasAttribute( 'alignment' ) ).to.be.false;