Explorar el Código

Tests for FontFamily improvements.

Kamil Piechaczek hace 5 años
padre
commit
67d8a00f37

+ 12 - 4
packages/ckeditor5-font/src/fontfamily/fontfamilyediting.js

@@ -73,7 +73,7 @@ export default class FontFamilyEditing extends Plugin {
 
 		// Set-up the two-way conversion.
 		if ( editor.config.get( 'fontFamily.disableValueMatching' ) ) {
-			this._setupGenericConverters();
+			this._prepareAnyValueConverters();
 		} else {
 			editor.conversion.attributeToElement( definition );
 		}
@@ -82,14 +82,19 @@ export default class FontFamilyEditing extends Plugin {
 	}
 
 	/**
+	 * Those converters enable keeping any value found as `style="font-family: *"` as a value of an attribute on a text even
+	 * if it isn't defined in the plugin configuration.
+	 *
 	 * @private
 	 */
-	_setupGenericConverters() {
+	_prepareAnyValueConverters() {
 		const editor = this.editor;
 
 		editor.conversion.for( 'downcast' ).attributeToElement( {
 			model: FONT_FAMILY,
-			view: ( attributeValue, writer ) => writer.createAttributeElement( 'span', { style: 'font-family:' + attributeValue } )
+			view: ( attributeValue, writer ) => {
+				return writer.createAttributeElement( 'span', { style: 'font-family:' + attributeValue }, { priority: 7 } );
+			}
 		} );
 
 		editor.conversion.for( 'upcast' ).attributeToAttribute( {
@@ -98,7 +103,10 @@ export default class FontFamilyEditing extends Plugin {
 				value: viewElement => viewElement.getStyle( 'font-family' )
 			},
 			view: {
-				name: 'span'
+				name: 'span',
+				styles: {
+					'font-family': /.*/
+				}
 			}
 		} );
 	}

+ 58 - 0
packages/ckeditor5-font/tests/fontfamily/fontfamilyediting.js

@@ -66,6 +66,64 @@ describe( 'FontFamilyEditing', () => {
 					'Trebuchet MS, Helvetica, sans-serif',
 					'Verdana, Geneva, sans-serif'
 				] );
+
+				expect( editor.config.get( 'fontFamily.disableValueMatching' ) ).to.equal( false );
+			} );
+		} );
+
+		describe( 'disableValueMatching=true', () => {
+			beforeEach( () => {
+				return VirtualTestEditor
+					.create( {
+						plugins: [ FontFamilyEditing, Paragraph ],
+						fontFamily: {
+							options: [
+								'Arial',
+							],
+							disableValueMatching: true
+						}
+					} )
+					.then( newEditor => {
+						editor = newEditor;
+
+						doc = editor.model;
+					} );
+			} );
+
+			describe( 'editing pipeline conversion', () => {
+				it( 'should convert unknown fontFamily attribute values', () => {
+					setModelData( doc, '<paragraph>f<$text fontFamily="foo-bar">o</$text>o</paragraph>' );
+
+					expect( editor.getData() ).to.equal( '<p>f<span style="font-family:foo-bar;">o</span>o</p>' );
+				} );
+
+				it( 'should convert defined fontFamily attribute values', () => {
+					setModelData( doc, '<paragraph>f<$text fontFamily="Arial">o</$text>o</paragraph>' );
+
+					expect( editor.getData() ).to.equal( '<p>f<span style="font-family:Arial;">o</span>o</p>' );
+				} );
+			} );
+
+			describe( 'data pipeline conversions', () => {
+				it( 'should convert from an element with defined style when with other styles', () => {
+					const data = '<p>f<span style="font-family: Other;font-size: 18px">o</span>o</p>';
+
+					editor.setData( data );
+
+					expect( getModelData( doc ) ).to.equal( '<paragraph>[]f<$text fontFamily="Other">o</$text>o</paragraph>' );
+
+					expect( editor.getData() ).to.equal( '<p>f<span style="font-family:Other;">o</span>o</p>' );
+				} );
+
+				it( 'should convert from a complex definition', () => {
+					const data = '<p>f<span style="font-family:Arial,sans-serif;">o</span>o</p>';
+
+					editor.setData( data );
+
+					expect( getModelData( doc ) ).to.equal( '<paragraph>[]f<$text fontFamily="Arial,sans-serif">o</$text>o</paragraph>' );
+
+					expect( editor.getData() ).to.equal( data );
+				} );
 			} );
 		} );
 	} );