浏览代码

Introduce conversion.for( 'downcast' ).attributeToAttribute() helper.

Maciej Gołaszewski 7 年之前
父节点
当前提交
fbeace0d81

+ 60 - 0
packages/ckeditor5-engine/src/conversion/downcast-converters.js

@@ -1204,6 +1204,7 @@ export const helpers = {
 	 *			}
 	 *		} );
 	 *
+	 * @method #attributeToElement
 	 * See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
 	 * to the conversion process.
 	 * @method #attributeToAttribute
@@ -1218,5 +1219,64 @@ export const helpers = {
 	 */
 	attributeToElement( config ) {
 		return this.add( downcastAttributeToElement( config ) );
+	},
+
+	/**
+	 * Model attribute to view attribute conversion helper.
+	 *
+	 * This conversion results in adding an attribute to a view node, basing on an attribute from a model node. For example,
+	 * `<image src='foo.jpg'></image>` is converted to `<img src='foo.jpg'></img>`.
+	 *
+	 *		conversion.for( 'downcast' ).attributeToAttribute( { model: 'source', view: 'src' } );
+	 *
+	 *		conversion.for( 'downcast' ).attributeToAttribute( { model: 'source', view: 'href', converterPriority: 'high' } );
+	 *
+	 *		conversion.for( 'downcast' ).attributeToAttribute( {
+	 *			model: {
+	 *				name: 'image',
+	 *				key: 'source'
+	 *			},
+	 *			view: 'src'
+	 *		} );
+	 *
+	 *		conversion.for( 'downcast' ).attributeToAttribute( {
+	 *			model: {
+	 *				name: 'styled',
+	 *				values: [ 'dark', 'light' ]
+	 *			},
+	 *			view: {
+	 *				dark: {
+	 *					key: 'class',
+	 *					value: [ 'styled', 'styled-dark' ]
+	 *				},
+	 *				light: {
+	 *					key: 'class',
+	 *					value: [ 'styled', 'styled-light' ]
+	 *				}
+	 *			}
+	 *		} );
+	 *
+	 *		conversion.for( 'downcast' ).attributeToAttribute( {
+	 *			model: 'styled',
+	 *			view: modelAttributeValue => ( { key: 'class', value: 'styled-' + modelAttributeValue } )
+	 *		} );
+	 *
+	 * See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
+	 * to the conversion process.
+	 *
+	 * @method #attributeToAttribute
+	 * @param {Object} config Conversion configuration.
+	 * @param {String|Object} config.model The key of the attribute to convert from or a `{ key, values, [ name ] }` object describing
+	 * the attribute key, possible values and, optionally, an element name to convert from.
+	 * @param {String|Object|Function} config.view A view attribute key, or a `{ key, value }` object or a function that takes
+	 * the model attribute value and returns a `{ key, value }` object. If `key` is `'class'`, `value` can be a `String` or an
+	 * array of `String`s. If `key` is `'style'`, `value` is an object with key-value pairs. In other cases, `value` is a `String`.
+	 * If `config.model.values` is set, `config.view` should be an object assigning values from `config.model.values` to
+	 * `{ key, value }` objects or a functions.
+	 * @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
+	 * @returns {Function} Conversion helper.
+	 */
+	attributeToAttribute( config ) {
+		return this.add( downcastAttributeToAttribute( config ) );
 	}
 };

+ 14 - 0
packages/ckeditor5-engine/tests/conversion/conversion.js

@@ -645,6 +645,20 @@ describe( 'Conversion', () => {
 					testDowncast( '<paragraph><$text bold="true">Foo</$text> bar</paragraph>', '<p><strong>Foo</strong> bar</p>' );
 				} );
 			} );
+
+			describe( 'attributeToAttribute()', () => {
+				it( 'adds downcast converter', () => {
+					schema.register( 'image', {
+						inheritAllFrom: '$block',
+						allowAttributes: [ 'source' ]
+					} );
+
+					conversion.for( 'downcast' ).elementToElement( { model: 'image', view: 'img' } );
+					conversion.for( 'downcast' ).attributeToAttribute( { model: 'source', view: 'src' } );
+
+					testDowncast( '<image source="foo.jpg"></image>', '<img src="foo.jpg"></img>' );
+				} );
+			} );
 		} );
 
 		function testDowncast( input, expectedView ) {